Skip to content

fix suggestion causes error of needless_for_each #15262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions clippy_lints/src/needless_for_each.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_session::declare_lint_pass;
use rustc_span::Span;

use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
use clippy_utils::ty::has_iter_method;
use clippy_utils::{is_trait_method, sym};

Expand Down Expand Up @@ -101,18 +101,23 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessForEach {

let body_param_sugg = snippet_with_applicability(cx, body.params[0].pat.span, "..", &mut applicability);
let for_each_rev_sugg = snippet_with_applicability(cx, for_each_recv.span, "..", &mut applicability);
let body_value_sugg = snippet_with_applicability(cx, body.value.span, "..", &mut applicability);
let (body_value_sugg, is_macro_call) =
snippet_with_context(cx, body.value.span, for_each_recv.span.ctxt(), "..", &mut applicability);

let sugg = format!(
"for {} in {} {}",
body_param_sugg,
for_each_rev_sugg,
match body.value.kind {
ExprKind::Block(block, _) if is_let_desugar(block) => {
format!("{{ {body_value_sugg} }}")
},
ExprKind::Block(_, _) => body_value_sugg.to_string(),
_ => format!("{{ {body_value_sugg}; }}"),
if is_macro_call {
format!("{{ {body_value_sugg}; }}")
} else {
match body.value.kind {
ExprKind::Block(block, _) if is_let_desugar(block) => {
format!("{{ {body_value_sugg} }}")
},
ExprKind::Block(_, _) => body_value_sugg.to_string(),
_ => format!("{{ {body_value_sugg}; }}"),
}
}
);

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/needless_for_each_fixable.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,9 @@ mod issue14734 {
//~^ needless_for_each
}
}

fn issue15256() {
let vec: Vec<i32> = Vec::new();
for v in vec.iter() { println!("{v}"); }
//~^ needless_for_each
}
6 changes: 6 additions & 0 deletions tests/ui/needless_for_each_fixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,9 @@ mod issue14734 {
//~^ needless_for_each
}
}

fn issue15256() {
let vec: Vec<i32> = Vec::new();
vec.iter().for_each(|v| println!("{v}"));
//~^ needless_for_each
}
8 changes: 7 additions & 1 deletion tests/ui/needless_for_each_fixable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,11 @@ error: needless use of `for_each`
LL | rows.iter().for_each(|x| do_something(x, 1u8));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in rows.iter() { do_something(x, 1u8); }`

error: aborting due to 10 previous errors
error: needless use of `for_each`
--> tests/ui/needless_for_each_fixable.rs:149:5
|
LL | vec.iter().for_each(|v| println!("{v}"));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for v in vec.iter() { println!("{v}"); }`

error: aborting due to 11 previous errors