Skip to content

Commit

Permalink
Auto merge of #10106 - koka831:fix/10084, r=Alexendoo
Browse files Browse the repository at this point in the history
Fix FP in `unnecessary_safety_comment`

Fix #10084

changelog: FP: [`unnecessary_safety_comment`]: No longer lints code inside macros
[#10106](#10106)
<!-- changelog_checked -->
  • Loading branch information
bors committed Jan 22, 2023
2 parents 9d1bb80 + 1a7ef02 commit a9c251f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions clippy_lints/src/undocumented_unsafe_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,18 @@ fn expr_has_unnecessary_safety_comment<'tcx>(
expr: &'tcx hir::Expr<'tcx>,
comment_pos: BytePos,
) -> Option<Span> {
if cx.tcx.hir().parent_iter(expr.hir_id).any(|(_, ref node)| {
matches!(
node,
Node::Block(&Block {
rules: BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided),
..
}),
)
}) {
return None;
}

// this should roughly be the reverse of `block_parents_have_safety_comment`
if for_each_expr_with_closures(cx, expr, |expr| match expr.kind {
hir::ExprKind::Block(
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/unnecessary_safety_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,21 @@ fn unnecessary_on_stmt_and_expr() -> u32 {
24
}

mod issue_10084 {
unsafe fn bar() -> i32 {
42
}

macro_rules! foo {
() => {
// SAFETY: This is necessary
unsafe { bar() }
};
}

fn main() {
foo!();
}
}

fn main() {}

0 comments on commit a9c251f

Please sign in to comment.