needless_collect: look through assignments, not just declarations

This commit is contained in:
Michael Howell 2025-02-17 13:06:57 -07:00
parent c012693961
commit 6321ac79e1
3 changed files with 25 additions and 0 deletions

View file

@ -546,6 +546,12 @@ impl<'tcx> Visitor<'tcx> for IteratorMethodCheckVisitor<'_, 'tcx> {
&& !is_trait_method(self.cx, expr, sym::Iterator)
{
return ControlFlow::Break(());
} else if let ExprKind::Assign(place, value, _span) = &expr.kind
&& value.hir_id == self.hir_id_of_expr
&& let Some(id) = path_to_local(place)
{
// our iterator was directly assigned to a variable
self.hir_id_of_let_binding = Some(id);
}
walk_expr(self, expr)
}
@ -561,6 +567,7 @@ impl<'tcx> Visitor<'tcx> for IteratorMethodCheckVisitor<'_, 'tcx> {
}) = &stmt.kind
&& expr.hir_id == self.hir_id_of_expr
{
// our iterator was directly assigned to a variable
self.hir_id_of_let_binding = Some(*id);
}
walk_stmt(self, stmt)

View file

@ -87,6 +87,15 @@ fn main() {
let my_collection: Vec<()> = vec![()].into_iter().map(|()| {}).collect();
let my_iter = my_collection.into_iter();
let _sliced = my_iter.as_slice();
// Assignment outside of main scope
{
let x;
{
let xxx: Vec<()> = vec![()].into_iter().map(|()| {}).collect();
x = xxx.into_iter();
for i in x.as_slice() {}
}
}
}
fn foo(_: impl IntoIterator<Item = usize>) {}

View file

@ -87,6 +87,15 @@ fn main() {
let my_collection: Vec<()> = vec![()].into_iter().map(|()| {}).collect();
let my_iter = my_collection.into_iter();
let _sliced = my_iter.as_slice();
// Assignment outside of main scope
{
let x;
{
let xxx: Vec<()> = vec![()].into_iter().map(|()| {}).collect();
x = xxx.into_iter();
for i in x.as_slice() {}
}
}
}
fn foo(_: impl IntoIterator<Item = usize>) {}