/// **Example:**
///
/// ```rust
+ /// // Bad
/// let x: u64 = 61864918973511;
+ ///
+ /// // Good
+ /// let x: u64 = 61_864_918_973_511;
/// ```
pub UNREADABLE_LITERAL,
pedantic,
/// **Example:**
///
/// ```rust
+ /// // Probably mistyped
/// 2_32;
+ ///
+ /// // Good
+ /// 2_i32;
/// ```
pub MISTYPED_LITERAL_SUFFIXES,
correctness,
/// **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,
/// ```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,
///
/// **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,
/// **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,
/// ```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,
/// ```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 => {},
///
/// **Example:**
/// ```rust
+ /// // Bad
/// match "foo" {
/// "a" => {},
/// "bar" | _ => {},
/// }
+ ///
+ /// // Good
+ /// match "foo" {
+ /// "a" => {},
+ /// _ => {},
+ /// }
/// ```
pub WILDCARD_IN_OR_PATTERNS,
complexity,
/// 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,
/// ```rust
/// # let x = 1.0;
///
+ /// // Bad
/// if x == f32::NAN { }
+ ///
+ /// // Good
+ /// if x.is_nan() { }
/// ```
pub CMP_NAN,
correctness,
/// ```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,
/// **Example:**
///
/// ```rust
+ /// // Bad
/// let a = 0 as *const u32;
+ ///
+ /// // Good
+ /// let a = std::ptr::null::<u32>();
/// ```
pub ZERO_PTR,
style,
/// ```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,
///
/// **Example:**
/// ```rust
+ /// // Bad
/// fn foo(a: i32, _a: i32) {}
+ ///
+ /// // Good
+ /// fn bar(a: i32, _b: i32) {}
/// ```
pub DUPLICATE_UNDERSCORE_ARGUMENT,
style,
///
/// **Example:**
/// ```rust,ignore
- /// (|| 42)()
+ /// // Bad
+ /// let a = (|| 42)()
+ ///
+ /// // Good
+ /// let a = 42
/// ```
pub REDUNDANT_CLOSURE_CALL,
complexity,
///
/// **Example:**
/// ```rust
+ /// // Bad
/// let y = 0x1a9BAcD;
+ ///
+ /// // Good
+ /// let y = 0x1A9BACD;
/// ```
pub MIXED_CASE_HEX_LITERALS,
style,
///
/// **Example:**
/// ```rust
+ /// // Bad
/// let y = 123832i32;
+ ///
+ /// // Good
+ /// let y = 123832_i32;
/// ```
pub UNSEPARATED_LITERAL_SUFFIX,
pedantic,
/// ```rust
/// # let v = Some("abc");
///
+ /// // Bad
+ /// match v {
+ /// Some(x) => (),
+ /// y @ _ => (),
+ /// }
+ ///
+ /// // Good
/// match v {
/// Some(x) => (),
- /// y @ _ => (), // easier written as `y`,
+ /// y => (),
/// }
/// ```
pub REDUNDANT_PATTERN,
/// # 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, ..) => (),
/// _ => (),
///
/// **Example:**
/// ```ignore
+ /// // Bad
/// my_vec.push(&mut value)
+ ///
+ /// // Good
+ /// my_vec.push(&value)
/// ```
pub UNNECESSARY_MUT_PASSED,
style,
///
/// **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,
/// ```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,
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.
///
/// **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"
/// assert_eq!(v.len(), 42);
/// }
/// ```
- ///
+ /// should be
/// ```rust
- /// // should be
/// fn foo(v: &[i32]) {
/// assert_eq!(v.len(), 42);
/// }
/// # 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,
///
/// **Example:**
/// ```ignore
+ /// // Bad
/// fn foo(&Vec<u32>) { .. }
+ ///
+ /// // Good
+ /// fn foo(&[u32]) { .. }
/// ```
pub PTR_ARG,
style,
///
/// **Example:**
/// ```ignore
+ /// // Bad
/// if x == ptr::null {
/// ..
/// }
+ ///
+ /// // Good
+ /// if x.is_null() {
+ /// ..
+ /// }
/// ```
pub CMP_NULL,
style,
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
replacement_str,
applicability,
)
- }
+ }
}
}
}
///
/// **Example:**
/// ```rust,ignore
+ /// // Bad
/// let a = f(*&mut b);
/// let c = *&d;
+ ///
+ /// // Good
+ /// let a = f(b);
+ /// let c = d;
/// ```
pub DEREF_ADDROF,
complexity,
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);
/// **Example:**
/// ```rust
/// # let x = 1;
+ ///
+ /// // Bad
/// let x = &x;
+ ///
+ /// // Good
+ /// let y = &x; // use different variable name
/// ```
pub SHADOW_SAME,
restriction,
/// # 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,
///
/// **Example:**
///
- /// ```rust, ignore
+ /// ```rust,ignore
/// use regex;
///
/// fn main() {
/// }
/// ```
/// Better as
- /// ```rust, ignore
+ /// ```rust,ignore
/// fn main() {
/// regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
/// }
/// ```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,
/// ```rust
/// let mut x = "Hello".to_owned();
/// x = x + ", World";
+ ///
+ /// // More readable
+ /// x += ", World";
+ /// x.push_str(", World");
/// ```
pub STRING_ADD_ASSIGN,
pedantic,
///
/// **Example:**
/// ```rust
+ /// // Bad
/// let bs = "a byte string".as_bytes();
+ ///
+ /// // Good
+ /// let bs = b"a byte string";
/// ```
pub STRING_LIT_AS_BYTES,
style,