extend needless_collect (#14361)
changelog: [`needless_collect`]: extend needless_collect to lint more cases Fix #14350
This commit is contained in:
commit
e6298692a5
4 changed files with 52 additions and 29 deletions
|
|
@ -38,11 +38,14 @@ pub(super) fn check<'tcx>(
|
|||
Node::Expr(parent) => {
|
||||
check_collect_into_intoiterator(cx, parent, collect_expr, call_span, iter_expr);
|
||||
|
||||
let sugg: String;
|
||||
let mut app;
|
||||
|
||||
if let ExprKind::MethodCall(name, _, args @ ([] | [_]), _) = parent.kind {
|
||||
let mut app = Applicability::MachineApplicable;
|
||||
app = Applicability::MachineApplicable;
|
||||
let collect_ty = cx.typeck_results().expr_ty(collect_expr);
|
||||
|
||||
let sugg: String = match name.ident.name {
|
||||
sugg = match name.ident.name {
|
||||
sym::len => {
|
||||
if let Some(adt) = collect_ty.ty_adt_def()
|
||||
&& matches!(
|
||||
|
|
@ -78,17 +81,23 @@ pub(super) fn check<'tcx>(
|
|||
},
|
||||
_ => return,
|
||||
};
|
||||
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
NEEDLESS_COLLECT,
|
||||
call_span.with_hi(parent.span.hi()),
|
||||
NEEDLESS_COLLECT_MSG,
|
||||
"replace with",
|
||||
sugg,
|
||||
app,
|
||||
);
|
||||
} else if let ExprKind::Index(_, index, _) = parent.kind {
|
||||
app = Applicability::MaybeIncorrect;
|
||||
let snip = snippet_with_applicability(cx, index.span, "_", &mut app);
|
||||
sugg = format!("nth({snip}).unwrap()");
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
NEEDLESS_COLLECT,
|
||||
call_span.with_hi(parent.span.hi()),
|
||||
NEEDLESS_COLLECT_MSG,
|
||||
"replace with",
|
||||
sugg,
|
||||
app,
|
||||
);
|
||||
},
|
||||
Node::LetStmt(l) => {
|
||||
if let PatKind::Binding(BindingMode::NONE | BindingMode::MUT, id, _, None) = l.pat.kind
|
||||
|
|
|
|||
|
|
@ -20,6 +20,10 @@ fn main() {
|
|||
}
|
||||
sample.iter().cloned().any(|x| x == 1);
|
||||
//~^ needless_collect
|
||||
|
||||
let _ = sample.iter().cloned().nth(1).unwrap();
|
||||
//~^ needless_collect
|
||||
|
||||
// #7164 HashMap's and BTreeMap's `len` usage should not be linted
|
||||
sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
|
||||
sample.iter().map(|x| (x, x)).collect::<BTreeMap<_, _>>().len();
|
||||
|
|
|
|||
|
|
@ -20,6 +20,10 @@ fn main() {
|
|||
}
|
||||
sample.iter().cloned().collect::<Vec<_>>().contains(&1);
|
||||
//~^ needless_collect
|
||||
|
||||
let _ = sample.iter().cloned().collect::<Vec<_>>()[1];
|
||||
//~^ needless_collect
|
||||
|
||||
// #7164 HashMap's and BTreeMap's `len` usage should not be linted
|
||||
sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
|
||||
sample.iter().map(|x| (x, x)).collect::<BTreeMap<_, _>>().len();
|
||||
|
|
|
|||
|
|
@ -20,100 +20,106 @@ LL | sample.iter().cloned().collect::<Vec<_>>().contains(&1);
|
|||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == 1)`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:27:35
|
||||
--> tests/ui/needless_collect.rs:24:36
|
||||
|
|
||||
LL | let _ = sample.iter().cloned().collect::<Vec<_>>()[1];
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `nth(1).unwrap()`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:31:35
|
||||
|
|
||||
LL | sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().is_empty();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:29:35
|
||||
--> tests/ui/needless_collect.rs:33:35
|
||||
|
|
||||
LL | sample.iter().map(|x| (x, x)).collect::<BTreeMap<_, _>>().is_empty();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:37:19
|
||||
--> tests/ui/needless_collect.rs:41:19
|
||||
|
|
||||
LL | sample.iter().collect::<LinkedList<_>>().len();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `count()`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:39:19
|
||||
--> tests/ui/needless_collect.rs:43:19
|
||||
|
|
||||
LL | sample.iter().collect::<LinkedList<_>>().is_empty();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:41:28
|
||||
--> tests/ui/needless_collect.rs:45:28
|
||||
|
|
||||
LL | sample.iter().cloned().collect::<LinkedList<_>>().contains(&1);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == 1)`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:43:19
|
||||
--> tests/ui/needless_collect.rs:47:19
|
||||
|
|
||||
LL | sample.iter().collect::<LinkedList<_>>().contains(&&1);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == &1)`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:47:19
|
||||
--> tests/ui/needless_collect.rs:51:19
|
||||
|
|
||||
LL | sample.iter().collect::<BinaryHeap<_>>().len();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `count()`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:49:19
|
||||
--> tests/ui/needless_collect.rs:53:19
|
||||
|
|
||||
LL | sample.iter().collect::<BinaryHeap<_>>().is_empty();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:55:27
|
||||
--> tests/ui/needless_collect.rs:59:27
|
||||
|
|
||||
LL | let _ = sample.iter().collect::<HashSet<_>>().is_empty();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:57:27
|
||||
--> tests/ui/needless_collect.rs:61:27
|
||||
|
|
||||
LL | let _ = sample.iter().collect::<HashSet<_>>().contains(&&0);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == &0)`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:80:27
|
||||
--> tests/ui/needless_collect.rs:84:27
|
||||
|
|
||||
LL | let _ = sample.iter().collect::<VecWrapper<_>>().is_empty();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:82:27
|
||||
--> tests/ui/needless_collect.rs:86:27
|
||||
|
|
||||
LL | let _ = sample.iter().collect::<VecWrapper<_>>().contains(&&0);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `any(|x| x == &0)`
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:87:40
|
||||
--> tests/ui/needless_collect.rs:91:40
|
||||
|
|
||||
LL | Vec::<u8>::new().extend((0..10).collect::<Vec<_>>());
|
||||
| ^^^^^^^^^^^^^^^^^^^^ help: remove this call
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:89:20
|
||||
--> tests/ui/needless_collect.rs:93:20
|
||||
|
|
||||
LL | foo((0..10).collect::<Vec<_>>());
|
||||
| ^^^^^^^^^^^^^^^^^^^^ help: remove this call
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:91:49
|
||||
--> tests/ui/needless_collect.rs:95:49
|
||||
|
|
||||
LL | bar((0..10).collect::<Vec<_>>(), (0..10).collect::<Vec<_>>());
|
||||
| ^^^^^^^^^^^^^^^^^^^^ help: remove this call
|
||||
|
||||
error: avoid using `collect()` when not needed
|
||||
--> tests/ui/needless_collect.rs:93:37
|
||||
--> tests/ui/needless_collect.rs:97:37
|
||||
|
|
||||
LL | baz((0..10), (), ('a'..='z').collect::<Vec<_>>())
|
||||
| ^^^^^^^^^^^^^^^^^^^^ help: remove this call
|
||||
|
||||
error: aborting due to 19 previous errors
|
||||
error: aborting due to 20 previous errors
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue