fix for x in y unsafe { }
This commit is contained in:
parent
89aba8d45d
commit
22f57ff584
@ -1,6 +1,6 @@
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::intravisit::{walk_expr, Visitor};
|
||||
use rustc_hir::{Closure, Expr, ExprKind, Stmt, StmtKind};
|
||||
use rustc_hir::{Block, BlockCheckMode, Closure, Expr, ExprKind, Stmt, StmtKind};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_session::declare_lint_pass;
|
||||
use rustc_span::{sym, Span, Symbol};
|
||||
@ -68,7 +68,8 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessForEach {
|
||||
// e.g. `v.iter().for_each(f)` is simpler and clearer than using `for` loop.
|
||||
&& let ExprKind::Closure(&Closure { body, .. }) = for_each_arg.kind
|
||||
&& let body = cx.tcx.hir().body(body)
|
||||
&& let ExprKind::Block(..) = body.value.kind
|
||||
// Skip the lint if the body is not safe.
|
||||
&& let ExprKind::Block(Block { rules: BlockCheckMode::DefaultBlock, .. }, ..) = body.value.kind
|
||||
{
|
||||
let mut ret_collector = RetCollector::default();
|
||||
ret_collector.visit_expr(body.value);
|
||||
|
@ -113,6 +113,10 @@ fn should_not_lint() {
|
||||
let _ = v.iter().for_each(|elem| {
|
||||
acc += elem;
|
||||
});
|
||||
// `for_each` has a closure with an unsafe block.
|
||||
v.iter().for_each(|elem| unsafe {
|
||||
acc += elem;
|
||||
});
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
@ -113,6 +113,10 @@ fn should_not_lint() {
|
||||
let _ = v.iter().for_each(|elem| {
|
||||
acc += elem;
|
||||
});
|
||||
// `for_each` has a closure with an unsafe block.
|
||||
v.iter().for_each(|elem| unsafe {
|
||||
acc += elem;
|
||||
});
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
Loading…
x
Reference in New Issue
Block a user