diff options
| author | bors <bors@rust-lang.org> | 2020-10-25 17:35:33 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-10-25 17:35:33 +0000 |
| commit | 83bb5ecec8dc893671cf1370f124ac3816750c7f (patch) | |
| tree | 13aaf4100e829e1503929f54d2a214f7740f7cc9 | |
| parent | 15f6fb99e4fb7b80639fa6a1217c61c6e95e8792 (diff) | |
| parent | 2f5d418011f80d99e0e4c8ddea3980b6cdc8332d (diff) | |
| download | rust-83bb5ecec8dc893671cf1370f124ac3816750c7f.tar.gz rust-83bb5ecec8dc893671cf1370f124ac3816750c7f.zip | |
Auto merge of #6202 - giraffate:fix_invalid_suggestion_in_needless_collect_test, r=flip1995
Fix an invalid suggestion in `needless_collect` test A test, https://github.com/rust-lang/rust-clippy/blob/master/tests/ui/needless_collect_indirect.rs#L11-L12, suggests following codes, but the suggested codes don't work. An example is here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6947d9f2806a83f41cc5eb8e39b09d0b. ``` error: avoid using `collect()` when not needed --> $DIR/needless_collect_indirect.rs:11:5 | LL | / let indirect_contains = sample.iter().collect::<VecDeque<_>>(); LL | | indirect_contains.contains(&&5); | |____^ | help: Check if the original Iterator contains an element instead of collecting then checking | LL | LL | sample.iter().any(|x| x == &&5); ``` changelog: none
| -rw-r--r-- | clippy_lints/src/loops.rs | 9 | ||||
| -rw-r--r-- | tests/ui/needless_collect_indirect.rs | 6 | ||||
| -rw-r--r-- | tests/ui/needless_collect_indirect.stderr | 17 |
3 files changed, 29 insertions, 3 deletions
diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 23ca35fffaa..c3f75f283f4 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -3001,7 +3001,14 @@ impl IterFunction { IterFunctionKind::IntoIter => String::new(), IterFunctionKind::Len => String::from(".count()"), IterFunctionKind::IsEmpty => String::from(".next().is_none()"), - IterFunctionKind::Contains(span) => format!(".any(|x| x == {})", snippet(cx, *span, "..")), + IterFunctionKind::Contains(span) => { + let s = snippet(cx, *span, ".."); + if let Some(stripped) = s.strip_prefix('&') { + format!(".any(|x| x == {})", stripped) + } else { + format!(".any(|x| x == *{})", s) + } + }, } } fn get_suggestion_text(&self) -> &'static str { diff --git a/tests/ui/needless_collect_indirect.rs b/tests/ui/needless_collect_indirect.rs index 4cf03e82035..4f6e5357727 100644 --- a/tests/ui/needless_collect_indirect.rs +++ b/tests/ui/needless_collect_indirect.rs @@ -16,4 +16,10 @@ fn main() { .into_iter() .map(|x| (*x, *x + 1)) .collect::<HashMap<_, _>>(); + + // #6202 + let a = "a".to_string(); + let sample = vec![a.clone(), "b".to_string(), "c".to_string()]; + let non_copy_contains = sample.into_iter().collect::<Vec<_>>(); + non_copy_contains.contains(&a); } diff --git a/tests/ui/needless_collect_indirect.stderr b/tests/ui/needless_collect_indirect.stderr index 0c1e61d7496..fb807da5f8a 100644 --- a/tests/ui/needless_collect_indirect.stderr +++ b/tests/ui/needless_collect_indirect.stderr @@ -48,8 +48,21 @@ LL | | indirect_contains.contains(&&5); help: Check if the original Iterator contains an element instead of collecting then checking | LL | -LL | sample.iter().any(|x| x == &&5); +LL | sample.iter().any(|x| x == &5); | -error: aborting due to 4 previous errors +error: avoid using `collect()` when not needed + --> $DIR/needless_collect_indirect.rs:23:5 + | +LL | / let non_copy_contains = sample.into_iter().collect::<Vec<_>>(); +LL | | non_copy_contains.contains(&a); + | |____^ + | +help: Check if the original Iterator contains an element instead of collecting then checking + | +LL | +LL | sample.into_iter().any(|x| x == a); + | + +error: aborting due to 5 previous errors |
