Skip to content

Commit eebb7cd

Browse files
author
BO41
committed
Add examples to some complexity lints
1 parent 3f56cb5 commit eebb7cd

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

clippy_lints/src/loops.rs

+8
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,14 @@ declare_clippy_lint! {
312312
/// for i in 0..v.len() { foo(v[i]); }
313313
/// for i in 0..v.len() { bar(i, v[i]); }
314314
/// ```
315+
/// Could be written as
316+
/// ```rust
317+
/// # let v = vec![1];
318+
/// # fn foo(bar: usize) {}
319+
/// # fn bar(bar: usize, baz: usize) {}
320+
/// for item in &v { foo(*item); }
321+
/// for (i, item) in v.iter().enumerate() { bar(i, *item); }
322+
/// ```
315323
pub EXPLICIT_COUNTER_LOOP,
316324
complexity,
317325
"for-looping with an explicit counter when `_.enumerate()` would do"

clippy_lints/src/methods/mod.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ declare_clippy_lint! {
302302
/// # let vec = vec![1];
303303
/// vec.iter().filter(|x| **x == 0).next();
304304
/// ```
305+
/// Could be written as
306+
/// ```rust
307+
/// # let vec = vec![1];
308+
/// vec.iter().find(|x| **x == 0);
309+
/// ```
305310
pub FILTER_NEXT,
306311
complexity,
307312
"using `filter(p).next()`, which is more succinctly expressed as `.find(p)`"
@@ -425,6 +430,11 @@ declare_clippy_lint! {
425430
/// # let vec = vec![1];
426431
/// vec.iter().find(|x| **x == 0).is_some();
427432
/// ```
433+
/// Could be written as
434+
/// ```rust
435+
/// # let vec = vec![1];
436+
/// vec.iter().any(|x| **x == 0);
437+
/// ```
428438
pub SEARCH_IS_SOME,
429439
complexity,
430440
"using an iterator search followed by `is_some()`, which is more succinctly expressed as a call to `any()`"
@@ -442,7 +452,12 @@ declare_clippy_lint! {
442452
/// **Example:**
443453
/// ```rust
444454
/// let name = "foo";
445-
/// name.chars().next() == Some('_');
455+
/// if name.chars().next() == Some('_') {};
456+
/// ```
457+
/// Could be written as
458+
/// ```rust
459+
/// let name = "foo";
460+
/// if name.starts_with('_') {};
446461
/// ```
447462
pub CHARS_NEXT_CMP,
448463
complexity,

clippy_lints/src/needless_bool.rs

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ declare_clippy_lint! {
3131
/// true
3232
/// }
3333
/// ```
34+
/// Could be written as
35+
/// ```rust,ignore
36+
/// !x
37+
/// ```
3438
pub NEEDLESS_BOOL,
3539
complexity,
3640
"if-statements with plain booleans in the then- and else-clause, e.g., `if p { true } else { false }`"

clippy_lints/src/ranges.rs

+13
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ declare_clippy_lint! {
4343
/// # let x = vec![1];
4444
/// x.iter().zip(0..x.len());
4545
/// ```
46+
/// Could be written as
47+
/// ```rust
48+
/// # let x = vec![1];
49+
/// x.iter().enumerate();
50+
/// ```
4651
pub RANGE_ZIP_WITH_LEN,
4752
complexity,
4853
"zipping iterator with a range when `enumerate()` would do"
@@ -64,6 +69,10 @@ declare_clippy_lint! {
6469
/// ```rust,ignore
6570
/// for x..(y+1) { .. }
6671
/// ```
72+
/// Could be written as
73+
/// ```rust,ignore
74+
/// for x..=y { .. }
75+
/// ```
6776
pub RANGE_PLUS_ONE,
6877
complexity,
6978
"`x..(y+1)` reads better as `x..=y`"
@@ -82,6 +91,10 @@ declare_clippy_lint! {
8291
/// ```rust,ignore
8392
/// for x..=(y-1) { .. }
8493
/// ```
94+
/// Could be written as
95+
/// ```rust,ignore
96+
/// for x..y { .. }
97+
/// ```
8598
pub RANGE_MINUS_ONE,
8699
complexity,
87100
"`x..=(y-1)` reads better as `x..y`"

0 commit comments

Comments
 (0)