Skip to content

Commit b6f2ccb

Browse files
committed
needless_for_each: cover single expr without braces
1 parent 54548d2 commit b6f2ccb

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

clippy_lints/src/needless_for_each.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessForEach {
7474
&& let body = cx.tcx.hir_body(body)
7575
// Skip the lint if the body is not safe, so as not to suggest `for … in … unsafe {}`
7676
// and suggesting `for … in … { unsafe { } }` is a little ugly.
77-
&& let ExprKind::Block(Block { rules: BlockCheckMode::DefaultBlock, .. }, ..) = body.value.kind
77+
&& !matches!(body.value.kind, ExprKind::Block(Block { rules: BlockCheckMode::UnsafeBlock(_), .. }, ..))
7878
{
7979
let mut ret_collector = RetCollector::default();
8080
ret_collector.visit_expr(body.value);
@@ -107,12 +107,12 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessForEach {
107107
"for {} in {} {}",
108108
body_param_sugg,
109109
for_each_rev_sugg,
110-
if let ExprKind::Block(block, _) = body.value.kind
111-
&& is_let_desugar(block)
112-
{
113-
format!("{{ {body_value_sugg} }}")
114-
} else {
115-
body_value_sugg.to_string()
110+
match body.value.kind {
111+
ExprKind::Block(block, _) if is_let_desugar(block) => {
112+
format!("{{ {body_value_sugg} }}")
113+
},
114+
ExprKind::Block(_, _) => body_value_sugg.to_string(),
115+
_ => format!("{{ {body_value_sugg}; }}"),
116116
}
117117
);
118118

tests/ui/needless_for_each_fixable.fixed

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,17 @@ fn should_not_lint() {
129129

130130
fn main() {}
131131

132-
fn issue14734(rows: &[u8]) {
133-
let mut v = vec![];
134-
for x in rows.iter() { _ = v.push(x) }
135-
//~^ needless_for_each
132+
mod issue14734 {
133+
fn let_desugar(rows: &[u8]) {
134+
let mut v = vec![];
135+
for x in rows.iter() { _ = v.push(x) }
136+
//~^ needless_for_each
137+
}
138+
139+
fn do_something(_: &u8, _: u8) {}
140+
141+
fn single_expr(rows: &[u8]) {
142+
for x in rows.iter() { do_something(x, 1u8); }
143+
//~^ needless_for_each
144+
}
136145
}

tests/ui/needless_for_each_fixable.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,17 @@ fn should_not_lint() {
129129

130130
fn main() {}
131131

132-
fn issue14734(rows: &[u8]) {
133-
let mut v = vec![];
134-
rows.iter().for_each(|x| _ = v.push(x));
135-
//~^ needless_for_each
132+
mod issue14734 {
133+
fn let_desugar(rows: &[u8]) {
134+
let mut v = vec![];
135+
rows.iter().for_each(|x| _ = v.push(x));
136+
//~^ needless_for_each
137+
}
138+
139+
fn do_something(_: &u8, _: u8) {}
140+
141+
fn single_expr(rows: &[u8]) {
142+
rows.iter().for_each(|x| do_something(x, 1u8));
143+
//~^ needless_for_each
144+
}
136145
}

tests/ui/needless_for_each_fixable.stderr

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,16 @@ LL + }
137137
|
138138

139139
error: needless use of `for_each`
140-
--> tests/ui/needless_for_each_fixable.rs:134:5
140+
--> tests/ui/needless_for_each_fixable.rs:135:9
141141
|
142-
LL | rows.iter().for_each(|x| _ = v.push(x));
143-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in rows.iter() { _ = v.push(x) }`
142+
LL | rows.iter().for_each(|x| _ = v.push(x));
143+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in rows.iter() { _ = v.push(x) }`
144144

145-
error: aborting due to 9 previous errors
145+
error: needless use of `for_each`
146+
--> tests/ui/needless_for_each_fixable.rs:142:9
147+
|
148+
LL | rows.iter().for_each(|x| do_something(x, 1u8));
149+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in rows.iter() { do_something(x, 1u8); }`
150+
151+
error: aborting due to 10 previous errors
146152

0 commit comments

Comments
 (0)