forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
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 rust-lang#8992 - kyoto7250:fix_8753, r=flip1995
feat(fix): Do not lint if the target code is inside a loop close rust-lang#8753 we consider the following code. ```rust fn main() { let vec = vec![1]; let w: Vec<usize> = vec.iter().map(|i| i * i).collect(); // <- once. for i in 0..2 { let _ = w.contains(&i); } } ``` and the clippy will issue the following warning. ```rust warning: avoid using `collect()` when not needed --> src/main.rs:3:51 | 3 | let w: Vec<usize> = vec.iter().map(|i| i * i).collect(); | ^^^^^^^ ... 6 | let _ = w.contains(&i); | -------------- the iterator could be used here instead | = note: `#[warn(clippy::needless_collect)]` on by default = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_collect help: check if the original Iterator contains an element instead of collecting then checking | 3 ~ 4 | 5 | for i in 0..2 { 6 ~ let _ = vec.iter().map(|i| i * i).any(|x| x == i); ``` Rewrite the code as indicated. ```rust fn main() { let vec = vec![1]; for i in 0..2 { let _ = vec.iter().map(|i| i * i).any(|x| x == i); // <- execute `map` every loop. } } ``` this code is valid in the compiler, but, it is different from the code before the rewrite. So, we should not lint, If `collect` is outside of a loop. Thank you in advance. --- changelog: Do not lint if the target code is inside a loop in `needless_collect`
- Loading branch information
Showing
4 changed files
with
346 additions
and
6 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