Skip to content

Commit ba66813

Browse files
committed
Auto merge of #4436 - BO41:written_as, r=phansch
Add some "could be written as" examples fixes #4405 changelog: none
2 parents b8e5e6f + 31a6ab4 commit ba66813

File tree

6 files changed

+57
-1
lines changed

6 files changed

+57
-1
lines changed

clippy_lints/src/loops.rs

Lines changed: 8 additions & 0 deletions
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

Lines changed: 20 additions & 1 deletion
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,
@@ -889,6 +904,10 @@ declare_clippy_lint! {
889904
/// ```rust
890905
/// let _ = [1, 2, 3].into_iter().map(|x| *x).collect::<Vec<u32>>();
891906
/// ```
907+
/// Could be written as:
908+
/// ```rust
909+
/// let _ = [1, 2, 3].iter().map(|x| *x).collect::<Vec<u32>>();
910+
/// ```
892911
pub INTO_ITER_ON_ARRAY,
893912
correctness,
894913
"using `.into_iter()` on an array"

clippy_lints/src/misc.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ declare_clippy_lint! {
105105
/// # let y = String::from("foo");
106106
/// if x.to_owned() == y {}
107107
/// ```
108+
/// Could be written as
109+
/// ```rust
110+
/// # let x = "foo";
111+
/// # let y = String::from("foo");
112+
/// if x == y {}
113+
/// ```
108114
pub CMP_OWNED,
109115
perf,
110116
"creating owned instances for comparing with others, e.g., `x == \"foo\".to_string()`"

clippy_lints/src/needless_bool.rs

Lines changed: 4 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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`"

clippy_lints/src/swap.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ declare_clippy_lint! {
5252
/// a = b;
5353
/// b = a;
5454
/// ```
55+
/// Could be written as:
56+
/// ```rust
57+
/// # let mut a = 1;
58+
/// # let mut b = 2;
59+
/// std::mem::swap(&mut a, &mut b);
60+
/// ```
5561
pub ALMOST_SWAPPED,
5662
correctness,
5763
"`foo = bar; bar = foo` sequence"

0 commit comments

Comments
 (0)