mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-30 16:39:26 +00:00
Give more corrected code examples in doc
This commit is contained in:
parent
262c9dc025
commit
19339334cb
18 changed files with 195 additions and 38 deletions
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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, ..) => (),
|
||||
/// _ => (),
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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.
|
||||
///
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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);
|
||||
/// }
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -88,7 +88,7 @@ impl QuestionMark {
|
|||
replacement_str,
|
||||
applicability,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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();
|
||||
/// }
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue