-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #5825 - giraffate:same_item_push, r=Manishearth
Add the new lint `same_item_push` changelog: Add the new lint `same_item_push` Fixed #4078. As I said in #4078 (comment), I referrerd to #4647.
- Loading branch information
Showing
6 changed files
with
284 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#![warn(clippy::same_item_push)] | ||
|
||
fn mutate_increment(x: &mut u8) -> u8 { | ||
*x += 1; | ||
*x | ||
} | ||
|
||
fn increment(x: u8) -> u8 { | ||
x + 1 | ||
} | ||
|
||
fn main() { | ||
// Test for basic case | ||
let mut spaces = Vec::with_capacity(10); | ||
for _ in 0..10 { | ||
spaces.push(vec![b' ']); | ||
} | ||
|
||
let mut vec2: Vec<u8> = Vec::new(); | ||
let item = 2; | ||
for _ in 5..=20 { | ||
vec2.push(item); | ||
} | ||
|
||
let mut vec3: Vec<u8> = Vec::new(); | ||
for _ in 0..15 { | ||
let item = 2; | ||
vec3.push(item); | ||
} | ||
|
||
let mut vec4: Vec<u8> = Vec::new(); | ||
for _ in 0..15 { | ||
vec4.push(13); | ||
} | ||
|
||
// Suggestion should not be given as pushed variable can mutate | ||
let mut vec5: Vec<u8> = Vec::new(); | ||
let mut item: u8 = 2; | ||
for _ in 0..30 { | ||
vec5.push(mutate_increment(&mut item)); | ||
} | ||
|
||
let mut vec6: Vec<u8> = Vec::new(); | ||
let mut item: u8 = 2; | ||
let mut item2 = &mut mutate_increment(&mut item); | ||
for _ in 0..30 { | ||
vec6.push(mutate_increment(item2)); | ||
} | ||
|
||
let mut vec7: Vec<usize> = Vec::new(); | ||
for (a, b) in [0, 1, 4, 9, 16].iter().enumerate() { | ||
vec7.push(a); | ||
} | ||
|
||
let mut vec8: Vec<u8> = Vec::new(); | ||
for i in 0..30 { | ||
vec8.push(increment(i)); | ||
} | ||
|
||
let mut vec9: Vec<u8> = Vec::new(); | ||
for i in 0..30 { | ||
vec9.push(i + i * i); | ||
} | ||
|
||
// Suggestion should not be given as there are multiple pushes that are not the same | ||
let mut vec10: Vec<u8> = Vec::new(); | ||
let item: u8 = 2; | ||
for _ in 0..30 { | ||
vec10.push(item); | ||
vec10.push(item * 2); | ||
} | ||
|
||
// Suggestion should not be given as Vec is not involved | ||
for _ in 0..5 { | ||
println!("Same Item Push"); | ||
} | ||
|
||
struct A { | ||
kind: u32, | ||
} | ||
let mut vec_a: Vec<A> = Vec::new(); | ||
for i in 0..30 { | ||
vec_a.push(A { kind: i }); | ||
} | ||
let mut vec12: Vec<u8> = Vec::new(); | ||
for a in vec_a { | ||
vec12.push(2u8.pow(a.kind)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
error: it looks like the same item is being pushed into this Vec | ||
--> $DIR/same_item_push.rs:16:9 | ||
| | ||
LL | spaces.push(vec![b' ']); | ||
| ^^^^^^ | ||
| | ||
= note: `-D clippy::same-item-push` implied by `-D warnings` | ||
= help: try using vec![vec![b' '];SIZE] or spaces.resize(NEW_SIZE, vec![b' ']) | ||
|
||
error: it looks like the same item is being pushed into this Vec | ||
--> $DIR/same_item_push.rs:22:9 | ||
| | ||
LL | vec2.push(item); | ||
| ^^^^ | ||
| | ||
= help: try using vec![item;SIZE] or vec2.resize(NEW_SIZE, item) | ||
|
||
error: it looks like the same item is being pushed into this Vec | ||
--> $DIR/same_item_push.rs:28:9 | ||
| | ||
LL | vec3.push(item); | ||
| ^^^^ | ||
| | ||
= help: try using vec![item;SIZE] or vec3.resize(NEW_SIZE, item) | ||
|
||
error: it looks like the same item is being pushed into this Vec | ||
--> $DIR/same_item_push.rs:33:9 | ||
| | ||
LL | vec4.push(13); | ||
| ^^^^ | ||
| | ||
= help: try using vec![13;SIZE] or vec4.resize(NEW_SIZE, 13) | ||
|
||
error: aborting due to 4 previous errors | ||
|