Skip to content

Commit 1933933

Browse files
committed
Give more corrected code examples in doc
1 parent 262c9dc commit 1933933

18 files changed

+195
-38
lines changed

clippy_lints/src/literal_representation.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ declare_clippy_lint! {
2424
/// **Example:**
2525
///
2626
/// ```rust
27+
/// // Bad
2728
/// let x: u64 = 61864918973511;
29+
///
30+
/// // Good
31+
/// let x: u64 = 61_864_918_973_511;
2832
/// ```
2933
pub UNREADABLE_LITERAL,
3034
pedantic,
@@ -44,7 +48,11 @@ declare_clippy_lint! {
4448
/// **Example:**
4549
///
4650
/// ```rust
51+
/// // Probably mistyped
4752
/// 2_32;
53+
///
54+
/// // Good
55+
/// 2_i32;
4856
/// ```
4957
pub MISTYPED_LITERAL_SUFFIXES,
5058
correctness,
@@ -63,7 +71,11 @@ declare_clippy_lint! {
6371
/// **Example:**
6472
///
6573
/// ```rust
74+
/// // Bad
6675
/// let x: u64 = 618_64_9189_73_511;
76+
///
77+
/// // Good
78+
/// let x: u64 = 61_864_918_973_511;
6779
/// ```
6880
pub INCONSISTENT_DIGIT_GROUPING,
6981
style,

clippy_lints/src/matches.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,17 @@ declare_clippy_lint! {
3636
/// ```rust
3737
/// # fn bar(stool: &str) {}
3838
/// # let x = Some("abc");
39+
///
40+
/// // Bad
3941
/// match x {
4042
/// Some(ref foo) => bar(foo),
4143
/// _ => (),
4244
/// }
45+
///
46+
/// // Good
47+
/// if let Some(ref foo) = x {
48+
/// bar(foo);
49+
/// }
4350
/// ```
4451
pub SINGLE_MATCH,
4552
style,
@@ -97,11 +104,19 @@ declare_clippy_lint! {
97104
///
98105
/// **Example:**
99106
/// ```rust,ignore
107+
/// // Bad
100108
/// match x {
101109
/// &A(ref y) => foo(y),
102110
/// &B => bar(),
103111
/// _ => frob(&x),
104112
/// }
113+
///
114+
/// // Good
115+
/// match *x {
116+
/// A(ref y) => foo(y),
117+
/// B => bar(),
118+
/// _ => frob(x),
119+
/// }
105120
/// ```
106121
pub MATCH_REF_PATS,
107122
style,
@@ -197,10 +212,15 @@ declare_clippy_lint! {
197212
/// **Example:**
198213
/// ```rust
199214
/// let x: Option<()> = None;
215+
///
216+
/// // Bad
200217
/// let r: Option<&()> = match x {
201218
/// None => None,
202219
/// Some(ref v) => Some(v),
203220
/// };
221+
///
222+
/// // Good
223+
/// let r: Option<&()> = x.as_ref();
204224
/// ```
205225
pub MATCH_AS_REF,
206226
complexity,
@@ -219,10 +239,18 @@ declare_clippy_lint! {
219239
/// ```rust
220240
/// # enum Foo { A(usize), B(usize) }
221241
/// # let x = Foo::B(1);
242+
///
243+
/// // Bad
222244
/// match x {
223245
/// Foo::A(_) => {},
224246
/// _ => {},
225247
/// }
248+
///
249+
/// // Good
250+
/// match x {
251+
/// Foo::A(_) => {},
252+
/// Foo::B(_) => {},
253+
/// }
226254
/// ```
227255
pub WILDCARD_ENUM_MATCH_ARM,
228256
restriction,
@@ -242,16 +270,15 @@ declare_clippy_lint! {
242270
/// ```rust
243271
/// # enum Foo { A, B, C }
244272
/// # let x = Foo::B;
273+
///
274+
/// // Bad
245275
/// match x {
246276
/// Foo::A => {},
247277
/// Foo::B => {},
248278
/// _ => {},
249279
/// }
250-
/// ```
251-
/// Use instead:
252-
/// ```rust
253-
/// # enum Foo { A, B, C }
254-
/// # let x = Foo::B;
280+
///
281+
/// // Good
255282
/// match x {
256283
/// Foo::A => {},
257284
/// Foo::B => {},
@@ -273,10 +300,17 @@ declare_clippy_lint! {
273300
///
274301
/// **Example:**
275302
/// ```rust
303+
/// // Bad
276304
/// match "foo" {
277305
/// "a" => {},
278306
/// "bar" | _ => {},
279307
/// }
308+
///
309+
/// // Good
310+
/// match "foo" {
311+
/// "a" => {},
312+
/// _ => {},
313+
/// }
280314
/// ```
281315
pub WILDCARD_IN_OR_PATTERNS,
282316
complexity,

clippy_lints/src/misc.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@ declare_clippy_lint! {
3838
/// dereferences, e.g., changing `*x` to `x` within the function.
3939
///
4040
/// **Example:**
41-
/// ```rust
41+
/// ```rust,ignore
42+
/// // Bad
4243
/// fn foo(ref x: u8) -> bool {
4344
/// true
4445
/// }
46+
///
47+
/// // Good
48+
/// fn foo(x: &u8) -> bool {
49+
/// true
50+
/// }
4551
/// ```
4652
pub TOPLEVEL_REF_ARG,
4753
style,
@@ -60,7 +66,11 @@ declare_clippy_lint! {
6066
/// ```rust
6167
/// # let x = 1.0;
6268
///
69+
/// // Bad
6370
/// if x == f32::NAN { }
71+
///
72+
/// // Good
73+
/// if x.is_nan() { }
6474
/// ```
6575
pub CMP_NAN,
6676
correctness,
@@ -83,8 +93,15 @@ declare_clippy_lint! {
8393
/// ```rust
8494
/// let x = 1.2331f64;
8595
/// let y = 1.2332f64;
96+
///
97+
/// // Bad
8698
/// if y == 1.23f64 { }
8799
/// if y != x {} // where both are floats
100+
///
101+
/// // Good
102+
/// let error = 0.01f64; // Use an epsilon for comparison
103+
/// if (y - 1.23f64).abs() < error { }
104+
/// if (y - x).abs() > error { }
88105
/// ```
89106
pub FLOAT_CMP,
90107
correctness,
@@ -191,7 +208,11 @@ declare_clippy_lint! {
191208
/// **Example:**
192209
///
193210
/// ```rust
211+
/// // Bad
194212
/// let a = 0 as *const u32;
213+
///
214+
/// // Good
215+
/// let a = std::ptr::null::<u32>();
195216
/// ```
196217
pub ZERO_PTR,
197218
style,
@@ -214,7 +235,13 @@ declare_clippy_lint! {
214235
/// ```rust
215236
/// let x: f64 = 1.0;
216237
/// const ONE: f64 = 1.00;
217-
/// x == ONE; // where both are floats
238+
///
239+
/// // Bad
240+
/// if x == ONE { } // where both are floats
241+
///
242+
/// // Good
243+
/// let error = 0.1f64; // Use an epsilon for comparison
244+
/// if (x - ONE).abs() < error { }
218245
/// ```
219246
pub FLOAT_CMP_CONST,
220247
restriction,

clippy_lints/src/misc_early.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ declare_clippy_lint! {
5959
///
6060
/// **Example:**
6161
/// ```rust
62+
/// // Bad
6263
/// fn foo(a: i32, _a: i32) {}
64+
///
65+
/// // Good
66+
/// fn bar(a: i32, _b: i32) {}
6367
/// ```
6468
pub DUPLICATE_UNDERSCORE_ARGUMENT,
6569
style,
@@ -77,7 +81,11 @@ declare_clippy_lint! {
7781
///
7882
/// **Example:**
7983
/// ```rust,ignore
80-
/// (|| 42)()
84+
/// // Bad
85+
/// let a = (|| 42)()
86+
///
87+
/// // Good
88+
/// let a = 42
8189
/// ```
8290
pub REDUNDANT_CLOSURE_CALL,
8391
complexity,
@@ -112,7 +120,11 @@ declare_clippy_lint! {
112120
///
113121
/// **Example:**
114122
/// ```rust
123+
/// // Bad
115124
/// let y = 0x1a9BAcD;
125+
///
126+
/// // Good
127+
/// let y = 0x1A9BACD;
116128
/// ```
117129
pub MIXED_CASE_HEX_LITERALS,
118130
style,
@@ -129,7 +141,11 @@ declare_clippy_lint! {
129141
///
130142
/// **Example:**
131143
/// ```rust
144+
/// // Bad
132145
/// let y = 123832i32;
146+
///
147+
/// // Good
148+
/// let y = 123832_i32;
133149
/// ```
134150
pub UNSEPARATED_LITERAL_SUFFIX,
135151
pedantic,
@@ -207,9 +223,16 @@ declare_clippy_lint! {
207223
/// ```rust
208224
/// # let v = Some("abc");
209225
///
226+
/// // Bad
227+
/// match v {
228+
/// Some(x) => (),
229+
/// y @ _ => (),
230+
/// }
231+
///
232+
/// // Good
210233
/// match v {
211234
/// Some(x) => (),
212-
/// y @ _ => (), // easier written as `y`,
235+
/// y => (),
213236
/// }
214237
/// ```
215238
pub REDUNDANT_PATTERN,
@@ -235,16 +258,13 @@ declare_clippy_lint! {
235258
/// # struct TupleStruct(u32, u32, u32);
236259
/// # let t = TupleStruct(1, 2, 3);
237260
///
261+
/// // Bad
238262
/// match t {
239263
/// TupleStruct(0, .., _) => (),
240264
/// _ => (),
241265
/// }
242-
/// ```
243-
/// can be written as
244-
/// ```rust
245-
/// # struct TupleStruct(u32, u32, u32);
246-
/// # let t = TupleStruct(1, 2, 3);
247266
///
267+
/// // Good
248268
/// match t {
249269
/// TupleStruct(0, ..) => (),
250270
/// _ => (),

clippy_lints/src/mut_reference.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ declare_clippy_lint! {
1616
///
1717
/// **Example:**
1818
/// ```ignore
19+
/// // Bad
1920
/// my_vec.push(&mut value)
21+
///
22+
/// // Good
23+
/// my_vec.push(&value)
2024
/// ```
2125
pub UNNECESSARY_MUT_PASSED,
2226
style,

clippy_lints/src/mutex_atomic.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ declare_clippy_lint! {
2222
///
2323
/// **Example:**
2424
/// ```rust
25+
/// # let y = true;
26+
///
27+
/// // Bad
2528
/// # use std::sync::Mutex;
26-
/// # let y = 1;
2729
/// let x = Mutex::new(&y);
30+
///
31+
/// // Good
32+
/// # use std::sync::atomic::AtomicBool;
33+
/// let x = AtomicBool::new(y);
2834
/// ```
2935
pub MUTEX_ATOMIC,
3036
perf,
@@ -46,6 +52,10 @@ declare_clippy_lint! {
4652
/// ```rust
4753
/// # use std::sync::Mutex;
4854
/// let x = Mutex::new(0usize);
55+
///
56+
/// // Good
57+
/// # use std::sync::atomic::AtomicUsize;
58+
/// let x = AtomicUsize::new(0usize);
4959
/// ```
5060
pub MUTEX_INTEGER,
5161
nursery,

clippy_lints/src/needless_bool.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use rustc_span::Span;
1515

1616
declare_clippy_lint! {
1717
/// **What it does:** Checks for expressions of the form `if c { true } else {
18-
/// false }`
19-
/// (or vice versa) and suggest using the condition directly.
18+
/// false }` (or vice versa) and suggests using the condition directly.
2019
///
2120
/// **Why is this bad?** Redundant code.
2221
///

clippy_lints/src/needless_borrow.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ declare_clippy_lint! {
1818
/// **Why is this bad?** Suggests that the receiver of the expression borrows
1919
/// the expression.
2020
///
21+
/// **Known problems:** None.
22+
///
2123
/// **Example:**
2224
/// ```rust
25+
/// // Bad
2326
/// let x: &i32 = &&&&&&5;
24-
/// ```
2527
///
26-
/// **Known problems:** None.
28+
/// // Good
29+
/// let x: &i32 = &5;
30+
/// ```
2731
pub NEEDLESS_BORROW,
2832
nursery,
2933
"taking a reference that is going to be automatically dereferenced"

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ declare_clippy_lint! {
4040
/// assert_eq!(v.len(), 42);
4141
/// }
4242
/// ```
43-
///
43+
/// should be
4444
/// ```rust
45-
/// // should be
4645
/// fn foo(v: &[i32]) {
4746
/// assert_eq!(v.len(), 42);
4847
/// }

0 commit comments

Comments
 (0)