Auto merge of #5664 - ThibsG:GiveCorrectedCode, r=flip1995

Give corrected code

This PR adds corrected code for doc examples.

I did this in several commits to facilitate review.
Don't hesitate to tell me if I forgot some.
Also, sometimes I felt it was not necessary to give corrected code, but I maybe wrong.

fixes: #4829

changelog: Improve documentation examples across multiple lints.
This commit is contained in:
bors 2020-06-02 12:11:28 +00:00
commit 5cb9ef390a
42 changed files with 433 additions and 90 deletions

View file

@ -24,7 +24,11 @@ declare_clippy_lint! {
/// let mut a = 5;
/// let b = 0;
/// // ...
/// // Bad
/// a = a + b;
///
/// // Good
/// a += b;
/// ```
pub ASSIGN_OP_PATTERN,
style,

View file

@ -13,10 +13,24 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// // Bad
/// fn simple_double_parens() -> i32 {
/// ((0))
/// }
///
/// // Good
/// fn simple_no_parens() -> i32 {
/// 0
/// }
///
/// // or
///
/// # fn foo(bar: usize) {}
/// ((0));
/// // Bad
/// foo((0));
/// ((1, 2));
///
/// // Good
/// foo(0);
/// ```
pub DOUBLE_PARENS,
complexity,

View file

@ -27,6 +27,10 @@ declare_clippy_lint! {
/// ```rust
/// fn foo<T: Drop>() {}
/// ```
/// Could be written as:
/// ```rust
/// fn foo<T>() {}
/// ```
pub DROP_BOUNDS,
correctness,
"Bounds of the form `T: Drop` are useless"

View file

@ -22,8 +22,14 @@ declare_clippy_lint! {
/// ```rust
/// # use std::time::Duration;
/// let dur = Duration::new(5, 0);
///
/// // Bad
/// let _micros = dur.subsec_nanos() / 1_000;
/// let _millis = dur.subsec_nanos() / 1_000_000;
///
/// // Good
/// let _micros = dur.subsec_micros();
/// let _millis = dur.subsec_millis();
/// ```
pub DURATION_SUBSEC,
complexity,

View file

@ -25,31 +25,47 @@ declare_clippy_lint! {
/// BattenbergCake,
/// }
/// ```
/// Could be written as:
/// ```rust
/// enum Cake {
/// BlackForest,
/// Hummingbird,
/// Battenberg,
/// }
/// ```
pub ENUM_VARIANT_NAMES,
style,
"enums where all variants share a prefix/postfix"
}
declare_clippy_lint! {
/// **What it does:** Detects enumeration variants that are prefixed or suffixed
/// by the same characters.
/// **What it does:** Detects public enumeration variants that are
/// prefixed or suffixed by the same characters.
///
/// **Why is this bad?** Enumeration variant names should specify their variant,
/// **Why is this bad?** Public enumeration variant names should specify their variant,
/// not repeat the enumeration name.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// enum Cake {
/// pub enum Cake {
/// BlackForestCake,
/// HummingbirdCake,
/// BattenbergCake,
/// }
/// ```
/// Could be written as:
/// ```rust
/// pub enum Cake {
/// BlackForest,
/// Hummingbird,
/// Battenberg,
/// }
/// ```
pub PUB_ENUM_VARIANT_NAMES,
pedantic,
"enums where all variants share a prefix/postfix"
"public enums where all variants share a prefix/postfix"
}
declare_clippy_lint! {
@ -66,6 +82,12 @@ declare_clippy_lint! {
/// struct BlackForestCake;
/// }
/// ```
/// Could be written as:
/// ```rust
/// mod cake {
/// struct BlackForest;
/// }
/// ```
pub MODULE_NAME_REPETITIONS,
pedantic,
"type names prefixed/postfixed with their containing module's name"

View file

@ -39,7 +39,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```ignore
/// // Bad
/// &x == y
///
/// // Good
/// x == *y
/// ```
pub OP_REF,
style,

View file

@ -28,9 +28,16 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// # fn foo(bar: usize) {}
///
/// // Bad
/// let x = Box::new(1);
/// foo(*x);
/// println!("{}", *x);
///
/// // Good
/// let x = 1;
/// foo(x);
/// println!("{}", x);
/// ```
pub BOXED_LOCAL,
perf,

View file

@ -26,7 +26,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust,ignore
/// // Bad
/// xs.map(|x| foo(x))
///
/// // Good
/// xs.map(foo)
/// ```
/// where `foo(_)` is a plain function that takes the exact argument type of
/// `x`.

View file

@ -21,11 +21,20 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// let mut x = 0;
///
/// // Bad
/// let a = {
/// x = 1;
/// 1
/// } + x;
/// // Unclear whether a is 1 or 2.
///
/// // Good
/// let tmp = {
/// x = 1;
/// 1
/// };
/// let a = tmp + x;
/// ```
pub EVAL_ORDER_DEPENDENCE,
complexity,

View file

@ -20,12 +20,31 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// struct Foo(i32);
///
/// // Bad
/// impl From<String> for Foo {
/// fn from(s: String) -> Self {
/// Foo(s.parse().unwrap())
/// }
/// }
/// ```
///
/// ```rust
/// // Good
/// struct Foo(i32);
///
/// use std::convert::TryFrom;
/// impl TryFrom<String> for Foo {
/// type Error = ();
/// fn try_from(s: String) -> Result<Self, Self::Error> {
/// if let Ok(parsed) = s.parse() {
/// Ok(Foo(parsed))
/// } else {
/// Err(())
/// }
/// }
/// }
/// ```
pub FALLIBLE_IMPL_FROM,
nursery,
"Warn on impls of `From<..>` that contain `panic!()` or `unwrap()`"
@ -120,7 +139,7 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
move |diag| {
diag.help(
"`From` is intended for infallible conversions only. \
Use `TryFrom` if there's a possibility for the conversion to fail.");
Use `TryFrom` if there's a possibility for the conversion to fail.");
diag.span_note(fpu.result, "potential failure(s)");
});
}

View file

@ -28,7 +28,6 @@ declare_clippy_lint! {
/// **Example:**
///
/// ```rust
///
/// let a = 3f32;
/// let _ = a.powf(1.0 / 3.0);
/// let _ = (1.0 + a).ln();
@ -38,7 +37,6 @@ declare_clippy_lint! {
/// is better expressed as
///
/// ```rust
///
/// let a = 3f32;
/// let _ = a.cbrt();
/// let _ = a.ln_1p();

View file

@ -25,9 +25,13 @@ declare_clippy_lint! {
///
/// **Examples:**
/// ```rust
///
/// // Bad
/// # let foo = "foo";
/// format!("foo");
/// format!("{}", foo);
///
/// // Good
/// format!("foo");
/// ```
pub USELESS_FORMAT,
complexity,

View file

@ -49,11 +49,11 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ``` rust
/// ```rust
/// fn im_too_long() {
/// println!("");
/// // ... 100 more LoC
/// println!("");
/// println!("");
/// // ... 100 more LoC
/// println!("");
/// }
/// ```
pub TOO_MANY_LINES,
@ -79,10 +79,16 @@ declare_clippy_lint! {
/// `some_argument.get_raw_ptr()`).
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// // Bad
/// pub fn foo(x: *const u8) {
/// println!("{}", unsafe { *x });
/// }
///
/// // Good
/// pub unsafe fn foo(x: *const u8) {
/// println!("{}", unsafe { *x });
/// }
/// ```
pub NOT_UNSAFE_PTR_ARG_DEREF,
correctness,

View file

@ -25,13 +25,6 @@ declare_clippy_lint! {
/// if i != 0 {
/// i -= 1;
/// }
/// ```
/// Use instead:
/// ```rust
/// let end: u32 = 10;
/// let start: u32 = 5;
///
/// let mut i: u32 = end - start;
///
/// // Good
/// i = i.saturating_sub(1);

View file

@ -10,7 +10,6 @@ use crate::utils::{snippet_opt, span_lint_and_sugg};
declare_clippy_lint! {
/// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
///
///
/// **Why is this bad?** Readability -- better to use `> y` instead of `>= y + 1`.
///
/// **Known problems:** None.

View file

@ -15,10 +15,13 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// fn main() {
/// let x = 3 / 2;
/// println!("{}", x);
/// }
/// // Bad
/// let x = 3 / 2;
/// println!("{}", x);
///
/// // Good
/// let x = 3f32 / 2f32;
/// println!("{}", x);
/// ```
pub INTEGER_DIVISION,
restriction,

View file

@ -16,6 +16,7 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// // Bad
/// fn foo() {
/// println!("cake");
/// }
@ -28,6 +29,21 @@ declare_clippy_lint! {
/// foo(); // prints "foo"
/// }
/// ```
///
/// ```rust
/// // Good
/// fn foo() {
/// println!("cake");
/// }
///
/// fn main() {
/// fn foo() {
/// println!("foo");
/// }
/// foo(); // prints "foo"
/// foo(); // prints "foo"
/// }
/// ```
pub ITEMS_AFTER_STATEMENTS,
pedantic,
"blocks where an item comes after a statement"

View file

@ -24,7 +24,11 @@ declare_clippy_lint! {
/// **Example:**
///
/// ```rust
/// // Bad
/// let x: u64 = 61864918973511;
///
/// // Good
/// let x: u64 = 61_864_918_973_511;
/// ```
pub UNREADABLE_LITERAL,
pedantic,
@ -44,7 +48,11 @@ declare_clippy_lint! {
/// **Example:**
///
/// ```rust
/// // Probably mistyped
/// 2_32;
///
/// // Good
/// 2_i32;
/// ```
pub MISTYPED_LITERAL_SUFFIXES,
correctness,
@ -63,7 +71,11 @@ declare_clippy_lint! {
/// **Example:**
///
/// ```rust
/// // Bad
/// let x: u64 = 618_64_9189_73_511;
///
/// // Good
/// let x: u64 = 61_864_918_973_511;
/// ```
pub INCONSISTENT_DIGIT_GROUPING,
style,

View file

@ -36,10 +36,17 @@ declare_clippy_lint! {
/// ```rust
/// # fn bar(stool: &str) {}
/// # let x = Some("abc");
///
/// // Bad
/// match x {
/// Some(ref foo) => bar(foo),
/// _ => (),
/// }
///
/// // Good
/// if let Some(ref foo) = x {
/// bar(foo);
/// }
/// ```
pub SINGLE_MATCH,
style,
@ -97,11 +104,19 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust,ignore
/// // Bad
/// match x {
/// &A(ref y) => foo(y),
/// &B => bar(),
/// _ => frob(&x),
/// }
///
/// // Good
/// match *x {
/// A(ref y) => foo(y),
/// B => bar(),
/// _ => frob(x),
/// }
/// ```
pub MATCH_REF_PATS,
style,
@ -197,10 +212,15 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// let x: Option<()> = None;
///
/// // Bad
/// let r: Option<&()> = match x {
/// None => None,
/// Some(ref v) => Some(v),
/// };
///
/// // Good
/// let r: Option<&()> = x.as_ref();
/// ```
pub MATCH_AS_REF,
complexity,
@ -219,10 +239,18 @@ declare_clippy_lint! {
/// ```rust
/// # enum Foo { A(usize), B(usize) }
/// # let x = Foo::B(1);
///
/// // Bad
/// match x {
/// Foo::A(_) => {},
/// _ => {},
/// }
///
/// // Good
/// match x {
/// Foo::A(_) => {},
/// Foo::B(_) => {},
/// }
/// ```
pub WILDCARD_ENUM_MATCH_ARM,
restriction,
@ -242,16 +270,15 @@ declare_clippy_lint! {
/// ```rust
/// # enum Foo { A, B, C }
/// # let x = Foo::B;
///
/// // Bad
/// match x {
/// Foo::A => {},
/// Foo::B => {},
/// _ => {},
/// }
/// ```
/// Use instead:
/// ```rust
/// # enum Foo { A, B, C }
/// # let x = Foo::B;
///
/// // Good
/// match x {
/// Foo::A => {},
/// Foo::B => {},
@ -273,10 +300,17 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// // Bad
/// match "foo" {
/// "a" => {},
/// "bar" | _ => {},
/// }
///
/// // Good
/// match "foo" {
/// "a" => {},
/// _ => {},
/// }
/// ```
pub WILDCARD_IN_OR_PATTERNS,
complexity,

View file

@ -218,7 +218,12 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// # let x = Ok::<_, ()>(());
/// x.ok().expect("why did I do this again?")
///
/// // Bad
/// x.ok().expect("why did I do this again?");
///
/// // Good
/// x.expect("why did I do this again?");
/// ```
pub OK_EXPECT,
style,
@ -273,8 +278,12 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// # let opt = Some(1);
/// opt.map_or(None, |a| Some(a + 1))
/// # ;
///
/// // Bad
/// opt.map_or(None, |a| Some(a + 1));
///
/// // Good
/// opt.and_then(|a| Some(a + 1));
/// ```
pub OPTION_MAP_OR_NONE,
style,
@ -390,14 +399,19 @@ declare_clippy_lint! {
/// **What it does:** Checks for usage of `_.map(_).flatten(_)`,
///
/// **Why is this bad?** Readability, this can be written more concisely as a
/// single method call.
/// single method call using `_.flat_map(_)`
///
/// **Known problems:**
///
/// **Example:**
/// ```rust
/// let vec = vec![vec![1]];
///
/// // Bad
/// vec.iter().map(|x| x.iter()).flatten();
///
/// // Good
/// vec.iter().flat_map(|x| x.iter());
/// ```
pub MAP_FLATTEN,
pedantic,
@ -417,7 +431,16 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// let vec = vec![1];
///
/// // Bad
/// vec.iter().filter(|x| **x == 0).map(|x| *x * 2);
///
/// // Good
/// vec.iter().filter_map(|x| if *x == 0 {
/// Some(*x * 2)
/// } else {
/// None
/// });
/// ```
pub FILTER_MAP,
pedantic,
@ -634,7 +657,12 @@ declare_clippy_lint! {
/// ```rust
/// # use std::rc::Rc;
/// let x = Rc::new(1);
///
/// // Bad
/// x.clone();
///
/// // Good
/// Rc::clone(&x);
/// ```
pub CLONE_ON_REF_PTR,
restriction,
@ -741,7 +769,12 @@ declare_clippy_lint! {
/// **Known problems:** Does not catch multi-byte unicode characters.
///
/// **Example:**
/// `_.split("x")` could be `_.split('x')`
/// ```rust,ignore
/// // Bad
/// _.split("x");
///
/// // Good
/// _.split('x');
pub SINGLE_CHAR_PATTERN,
perf,
"using a single-character str where a char could be used, e.g., `_.split(\"x\")`"
@ -964,8 +997,8 @@ declare_clippy_lint! {
}
declare_clippy_lint! {
/// **What it does:** Checks for usage of `.chars().last()` or
/// `.chars().next_back()` on a `str` to check if it ends with a given char.
/// **What it does:** Checks for usage of `_.chars().last()` or
/// `_.chars().next_back()` on a `str` to check if it ends with a given char.
///
/// **Why is this bad?** Readability, this can be written more concisely as
/// `_.ends_with(_)`.
@ -975,8 +1008,12 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// # let name = "_";
/// name.chars().last() == Some('_') || name.chars().next_back() == Some('-')
/// # ;
///
/// // Bad
/// name.chars().last() == Some('_') || name.chars().next_back() == Some('-');
///
/// // Good
/// name.ends_with('_') || name.ends_with('-');
/// ```
pub CHARS_LAST_CMP,
style,
@ -1044,17 +1081,15 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// let _ = (0..3).filter_map(|x| if x > 2 { Some(x) } else { None });
/// ```
/// As there is no transformation of the argument this could be written as:
/// ```rust
///
/// // As there is no transformation of the argument this could be written as:
/// let _ = (0..3).filter(|&x| x > 2);
/// ```
///
/// ```rust
/// let _ = (0..4).filter_map(|x| Some(x + 1));
/// ```
/// As there is no conditional check on the argument this could be written as:
/// ```rust
///
/// // As there is no conditional check on the argument this could be written as:
/// let _ = (0..4).map(|x| x + 1);
/// ```
pub UNNECESSARY_FILTER_MAP,
@ -1075,7 +1110,11 @@ declare_clippy_lint! {
/// **Example:**
///
/// ```rust
/// // Bad
/// let _ = (&vec![3, 4, 5]).into_iter();
///
/// // Good
/// let _ = (&vec![3, 4, 5]).iter();
/// ```
pub INTO_ITER_ON_REF,
style,

View file

@ -38,10 +38,16 @@ declare_clippy_lint! {
/// dereferences, e.g., changing `*x` to `x` within the function.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// // Bad
/// fn foo(ref x: u8) -> bool {
/// true
/// }
///
/// // Good
/// fn foo(x: &u8) -> bool {
/// true
/// }
/// ```
pub TOPLEVEL_REF_ARG,
style,
@ -60,7 +66,11 @@ declare_clippy_lint! {
/// ```rust
/// # let x = 1.0;
///
/// // Bad
/// if x == f32::NAN { }
///
/// // Good
/// if x.is_nan() { }
/// ```
pub CMP_NAN,
correctness,
@ -83,8 +93,15 @@ declare_clippy_lint! {
/// ```rust
/// let x = 1.2331f64;
/// let y = 1.2332f64;
///
/// // Bad
/// if y == 1.23f64 { }
/// if y != x {} // where both are floats
///
/// // Good
/// let error = 0.01f64; // Use an epsilon for comparison
/// if (y - 1.23f64).abs() < error { }
/// if (y - x).abs() > error { }
/// ```
pub FLOAT_CMP,
correctness,
@ -191,7 +208,11 @@ declare_clippy_lint! {
/// **Example:**
///
/// ```rust
/// // Bad
/// let a = 0 as *const u32;
///
/// // Good
/// let a = std::ptr::null::<u32>();
/// ```
pub ZERO_PTR,
style,
@ -214,7 +235,13 @@ declare_clippy_lint! {
/// ```rust
/// let x: f64 = 1.0;
/// const ONE: f64 = 1.00;
/// x == ONE; // where both are floats
///
/// // Bad
/// if x == ONE { } // where both are floats
///
/// // Good
/// let error = 0.1f64; // Use an epsilon for comparison
/// if (x - ONE).abs() < error { }
/// ```
pub FLOAT_CMP_CONST,
restriction,

View file

@ -59,7 +59,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// // Bad
/// fn foo(a: i32, _a: i32) {}
///
/// // Good
/// fn bar(a: i32, _b: i32) {}
/// ```
pub DUPLICATE_UNDERSCORE_ARGUMENT,
style,
@ -77,7 +81,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust,ignore
/// (|| 42)()
/// // Bad
/// let a = (|| 42)()
///
/// // Good
/// let a = 42
/// ```
pub REDUNDANT_CLOSURE_CALL,
complexity,
@ -112,7 +120,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// // Bad
/// let y = 0x1a9BAcD;
///
/// // Good
/// let y = 0x1A9BACD;
/// ```
pub MIXED_CASE_HEX_LITERALS,
style,
@ -129,7 +141,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// // Bad
/// let y = 123832i32;
///
/// // Good
/// let y = 123832_i32;
/// ```
pub UNSEPARATED_LITERAL_SUFFIX,
pedantic,
@ -207,9 +223,16 @@ declare_clippy_lint! {
/// ```rust
/// # let v = Some("abc");
///
/// // Bad
/// match v {
/// Some(x) => (),
/// y @ _ => (), // easier written as `y`,
/// y @ _ => (),
/// }
///
/// // Good
/// match v {
/// Some(x) => (),
/// y => (),
/// }
/// ```
pub REDUNDANT_PATTERN,
@ -235,16 +258,13 @@ declare_clippy_lint! {
/// # struct TupleStruct(u32, u32, u32);
/// # let t = TupleStruct(1, 2, 3);
///
/// // Bad
/// match t {
/// TupleStruct(0, .., _) => (),
/// _ => (),
/// }
/// ```
/// can be written as
/// ```rust
/// # struct TupleStruct(u32, u32, u32);
/// # let t = TupleStruct(1, 2, 3);
///
/// // Good
/// match t {
/// TupleStruct(0, ..) => (),
/// _ => (),

View file

@ -16,7 +16,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```ignore
/// // Bad
/// my_vec.push(&mut value)
///
/// // Good
/// my_vec.push(&value)
/// ```
pub UNNECESSARY_MUT_PASSED,
style,

View file

@ -22,9 +22,15 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// # let y = true;
///
/// // Bad
/// # use std::sync::Mutex;
/// # let y = 1;
/// let x = Mutex::new(&y);
///
/// // Good
/// # use std::sync::atomic::AtomicBool;
/// let x = AtomicBool::new(y);
/// ```
pub MUTEX_ATOMIC,
perf,
@ -46,6 +52,10 @@ declare_clippy_lint! {
/// ```rust
/// # use std::sync::Mutex;
/// let x = Mutex::new(0usize);
///
/// // Good
/// # use std::sync::atomic::AtomicUsize;
/// let x = AtomicUsize::new(0usize);
/// ```
pub MUTEX_INTEGER,
nursery,

View file

@ -15,8 +15,7 @@ use rustc_span::Span;
declare_clippy_lint! {
/// **What it does:** Checks for expressions of the form `if c { true } else {
/// false }`
/// (or vice versa) and suggest using the condition directly.
/// false }` (or vice versa) and suggests using the condition directly.
///
/// **Why is this bad?** Redundant code.
///

View file

@ -18,12 +18,16 @@ declare_clippy_lint! {
/// **Why is this bad?** Suggests that the receiver of the expression borrows
/// the expression.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// // Bad
/// let x: &i32 = &&&&&&5;
/// ```
///
/// **Known problems:** None.
/// // Good
/// let x: &i32 = &5;
/// ```
pub NEEDLESS_BORROW,
nursery,
"taking a reference that is going to be automatically dereferenced"

View file

@ -40,9 +40,8 @@ declare_clippy_lint! {
/// assert_eq!(v.len(), 42);
/// }
/// ```
///
/// should be
/// ```rust
/// // should be
/// fn foo(v: &[i32]) {
/// assert_eq!(v.len(), 42);
/// }

View file

@ -21,6 +21,16 @@ declare_clippy_lint! {
/// # z: i32,
/// # }
/// # let zero_point = Point { x: 0, y: 0, z: 0 };
///
/// // Bad
/// Point {
/// x: 1,
/// y: 1,
/// z: 1,
/// ..zero_point
/// };
///
/// // Ok
/// Point {
/// x: 1,
/// y: 1,

View file

@ -47,7 +47,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```ignore
/// // Bad
/// fn foo(&Vec<u32>) { .. }
///
/// // Good
/// fn foo(&[u32]) { .. }
/// ```
pub PTR_ARG,
style,
@ -65,9 +69,15 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```ignore
/// // Bad
/// if x == ptr::null {
/// ..
/// }
///
/// // Good
/// if x.is_null() {
/// ..
/// }
/// ```
pub CMP_NULL,
style,
@ -76,19 +86,16 @@ declare_clippy_lint! {
declare_clippy_lint! {
/// **What it does:** This lint checks for functions that take immutable
/// references and return
/// mutable ones.
/// references and return mutable ones.
///
/// **Why is this bad?** This is trivially unsound, as one can create two
/// mutable references
/// from the same (immutable!) source. This
/// [error](https://github.com/rust-lang/rust/issues/39465)
/// mutable references from the same (immutable!) source.
/// This [error](https://github.com/rust-lang/rust/issues/39465)
/// actually lead to an interim Rust release 1.15.1.
///
/// **Known problems:** To be on the conservative side, if there's at least one
/// mutable reference
/// with the output lifetime, this lint will not trigger. In practice, this
/// case is unlikely anyway.
/// mutable reference with the output lifetime, this lint will not trigger.
/// In practice, this case is unlikely anyway.
///
/// **Example:**
/// ```ignore

View file

@ -88,7 +88,7 @@ impl QuestionMark {
replacement_str,
applicability,
)
}
}
}
}
}

View file

@ -16,8 +16,13 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust,ignore
/// // Bad
/// let a = f(*&mut b);
/// let c = *&d;
///
/// // Good
/// let a = f(b);
/// let c = d;
/// ```
pub DEREF_ADDROF,
complexity,

View file

@ -86,11 +86,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Regex {
if let Some(span) = is_expn_of(expr.span, "regex");
then {
if !self.spans.contains(&span) {
span_lint(cx,
REGEX_MACRO,
span,
"`regex!(_)` found. \
Please use `Regex::new(_)`, which is faster for now.");
span_lint(
cx,
REGEX_MACRO,
span,
"`regex!(_)` found. \
Please use `Regex::new(_)`, which is faster for now."
);
self.spans.insert(span);
}
self.last = Some(block.hir_id);

View file

@ -25,7 +25,12 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// # let x = 1;
///
/// // Bad
/// let x = &x;
///
/// // Good
/// let y = &x; // use different variable name
/// ```
pub SHADOW_SAME,
restriction,
@ -77,7 +82,12 @@ declare_clippy_lint! {
/// # let y = 1;
/// # let z = 2;
/// let x = y;
///
/// // Bad
/// let x = z; // shadows the earlier binding
///
/// // Good
/// let w = z; // use different variable name
/// ```
pub SHADOW_UNRELATED,
pedantic,

View file

@ -16,7 +16,7 @@ declare_clippy_lint! {
///
/// **Example:**
///
/// ```rust, ignore
/// ```rust,ignore
/// use regex;
///
/// fn main() {
@ -24,7 +24,7 @@ declare_clippy_lint! {
/// }
/// ```
/// Better as
/// ```rust, ignore
/// ```rust,ignore
/// fn main() {
/// regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
/// }

View file

@ -22,11 +22,17 @@ declare_clippy_lint! {
/// ```rust
/// # use core::iter::repeat;
/// # let len = 4;
///
/// // Bad
/// let mut vec1 = Vec::with_capacity(len);
/// vec1.resize(len, 0);
///
/// let mut vec2 = Vec::with_capacity(len);
/// vec2.extend(repeat(0).take(len))
/// vec2.extend(repeat(0).take(len));
///
/// // Good
/// let mut vec1 = vec![0; len];
/// let mut vec2 = vec![0; len];
/// ```
pub SLOW_VECTOR_INITIALIZATION,
perf,

View file

@ -24,6 +24,10 @@ declare_clippy_lint! {
/// ```rust
/// let mut x = "Hello".to_owned();
/// x = x + ", World";
///
/// // More readable
/// x += ", World";
/// x.push_str(", World");
/// ```
pub STRING_ADD_ASSIGN,
pedantic,
@ -69,7 +73,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// // Bad
/// let bs = "a byte string".as_bytes();
///
/// // Good
/// let bs = b"a byte string";
/// ```
pub STRING_LIT_AS_BYTES,
style,

View file

@ -17,8 +17,14 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```rust,ignore
/// foo(&vec![1, 2])
/// ```rust
/// # fn foo(my_vec: &[u8]) {}
///
/// // Bad
/// foo(&vec![1, 2]);
///
/// // Good
/// foo(&[1, 2]);
/// ```
pub USELESS_VEC,
perf,

View file

@ -9,6 +9,7 @@ declare_clippy_lint! {
///
/// **Why is this bad?** `fs::{read, read_to_string}` provide the same functionality when `buf` is empty with fewer imports and no intermediate values.
/// See also: [fs::read docs](https://doc.rust-lang.org/std/fs/fn.read.html), [fs::read_to_string docs](https://doc.rust-lang.org/std/fs/fn.read_to_string.html)
///
/// **Known problems:** None.
///
/// **Example:**

View file

@ -19,8 +19,14 @@ declare_clippy_lint! {
/// still around.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// // Bad
/// use std::cmp::Ordering::*;
/// foo(Less);
///
/// // Good
/// use std::cmp::Ordering;
/// foo(Ordering::Less)
/// ```
pub ENUM_GLOB_USE,
pedantic,
@ -60,15 +66,15 @@ declare_clippy_lint! {
///
/// **Example:**
///
/// Bad:
/// ```rust,ignore
/// // Bad
/// use crate1::*;
///
/// foo();
/// ```
///
/// Good:
/// ```rust,ignore
/// // Good
/// use crate1::foo;
///
/// foo();

View file

@ -23,7 +23,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// // Bad
/// println!("");
///
/// // Good
/// println!();
/// ```
pub PRINTLN_EMPTY_STRING,
style,
@ -32,8 +36,7 @@ declare_clippy_lint! {
declare_clippy_lint! {
/// **What it does:** This lint warns when you use `print!()` with a format
/// string that
/// ends in a newline.
/// string that ends in a newline.
///
/// **Why is this bad?** You should use `println!()` instead, which appends the
/// newline.
@ -125,7 +128,12 @@ declare_clippy_lint! {
/// ```rust
/// # use std::fmt::Write;
/// # let mut buf = String::new();
///
/// // Bad
/// writeln!(buf, "");
///
/// // Good
/// writeln!(buf);
/// ```
pub WRITELN_EMPTY_STRING,
style,
@ -147,7 +155,12 @@ declare_clippy_lint! {
/// # use std::fmt::Write;
/// # let mut buf = String::new();
/// # let name = "World";
///
/// // Bad
/// write!(buf, "Hello {}!\n", name);
///
/// // Good
/// writeln!(buf, "Hello {}!", name);
/// ```
pub WRITE_WITH_NEWLINE,
style,
@ -168,7 +181,12 @@ declare_clippy_lint! {
/// ```rust
/// # use std::fmt::Write;
/// # let mut buf = String::new();
///
/// // Bad
/// writeln!(buf, "{}", "foo");
///
/// // Good
/// writeln!(buf, "foo");
/// ```
pub WRITE_LITERAL,
style,

View file

@ -14,7 +14,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// 0.0f32 / 0.0;
/// // Bad
/// let nan = 0.0f32 / 0.0;
///
/// // Good
/// let nan = f32::NAN;
/// ```
pub ZERO_DIVIDED_BY_ZERO,
complexity,

View file

@ -1742,7 +1742,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
Lint {
name: "pub_enum_variant_names",
group: "pedantic",
desc: "enums where all variants share a prefix/postfix",
desc: "public enums where all variants share a prefix/postfix",
deprecation: None,
module: "enum_variants",
},