Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 22f57ff

Browse files
committed
fix for x in y unsafe { }
1 parent 89aba8d commit 22f57ff

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

clippy_lints/src/needless_for_each.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_errors::Applicability;
22
use rustc_hir::intravisit::{walk_expr, Visitor};
3-
use rustc_hir::{Closure, Expr, ExprKind, Stmt, StmtKind};
3+
use rustc_hir::{Block, BlockCheckMode, Closure, Expr, ExprKind, Stmt, StmtKind};
44
use rustc_lint::{LateContext, LateLintPass};
55
use rustc_session::declare_lint_pass;
66
use rustc_span::{sym, Span, Symbol};
@@ -68,7 +68,8 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessForEach {
6868
// e.g. `v.iter().for_each(f)` is simpler and clearer than using `for` loop.
6969
&& let ExprKind::Closure(&Closure { body, .. }) = for_each_arg.kind
7070
&& let body = cx.tcx.hir().body(body)
71-
&& let ExprKind::Block(..) = body.value.kind
71+
// Skip the lint if the body is not safe.
72+
&& let ExprKind::Block(Block { rules: BlockCheckMode::DefaultBlock, .. }, ..) = body.value.kind
7273
{
7374
let mut ret_collector = RetCollector::default();
7475
ret_collector.visit_expr(body.value);

tests/ui/needless_for_each_fixable.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ fn should_not_lint() {
113113
let _ = v.iter().for_each(|elem| {
114114
acc += elem;
115115
});
116+
// `for_each` has a closure with an unsafe block.
117+
v.iter().for_each(|elem| unsafe {
118+
acc += elem;
119+
});
116120
}
117121

118122
fn main() {}

tests/ui/needless_for_each_fixable.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ fn should_not_lint() {
113113
let _ = v.iter().for_each(|elem| {
114114
acc += elem;
115115
});
116+
// `for_each` has a closure with an unsafe block.
117+
v.iter().for_each(|elem| unsafe {
118+
acc += elem;
119+
});
116120
}
117121

118122
fn main() {}

0 commit comments

Comments
 (0)