Skip to content

Commit

Permalink
Give more corrected code examples in doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibsG committed May 31, 2020
1 parent 6d2dcca commit 86814c5
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 38 deletions.
12 changes: 12 additions & 0 deletions clippy_lints/src/literal_representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -44,7 +48,11 @@ declare_clippy_lint! {
/// **Example:**
///
/// ```rust
/// // Probably mistyped
/// 2_32;
///
/// // Good
/// 2_i32;
/// ```
pub MISTYPED_LITERAL_SUFFIXES,
correctness,
Expand All @@ -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,
Expand Down
44 changes: 39 additions & 5 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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 => {},
Expand All @@ -273,10 +300,17 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// // Bad
/// match "foo" {
/// "a" => {},
/// "bar" | _ => {},
/// }
///
/// // Good
/// match "foo" {
/// "a" => {},
/// _ => {},
/// }
/// ```
pub WILDCARD_IN_OR_PATTERNS,
complexity,
Expand Down
31 changes: 29 additions & 2 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
34 changes: 27 additions & 7 deletions clippy_lints/src/misc_early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -77,7 +81,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust,ignore
/// (|| 42)()
/// // Bad
/// let a = (|| 42)()
///
/// // Good
/// let a = 42
/// ```
pub REDUNDANT_CLOSURE_CALL,
complexity,
Expand Down Expand Up @@ -112,7 +120,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// // Bad
/// let y = 0x1a9BAcD;
///
/// // Good
/// let y = 0x1A9BACD;
/// ```
pub MIXED_CASE_HEX_LITERALS,
style,
Expand All @@ -129,7 +141,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// // Bad
/// let y = 123832i32;
///
/// // Good
/// let y = 123832_i32;
/// ```
pub UNSEPARATED_LITERAL_SUFFIX,
pedantic,
Expand Down Expand Up @@ -207,9 +223,16 @@ declare_clippy_lint! {
/// ```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,
Expand All @@ -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, ..) => (),
/// _ => (),
Expand Down
4 changes: 4 additions & 0 deletions clippy_lints/src/mut_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion clippy_lints/src/mutex_atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/needless_bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
8 changes: 6 additions & 2 deletions clippy_lints/src/needless_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/needless_pass_by_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
/// }
Expand Down
Loading

0 comments on commit 86814c5

Please sign in to comment.