about summary refs log tree commit diff
diff options
context:
space:
mode:
authorkoka <koka.code@gmail.com>2023-01-09 18:49:46 +0900
committerkoka <koka.code@gmail.com>2023-01-09 18:49:46 +0900
commit1a7ef02dcb65ee4aa9e7679ad0ce7e8a42bee6bf (patch)
treeb9cca57d336341b81288cb85780beb5bc24b7e97
parentd29c4c9f638f456a235a5bae632401b6d1bb3614 (diff)
downloadrust-1a7ef02dcb65ee4aa9e7679ad0ce7e8a42bee6bf.tar.gz
rust-1a7ef02dcb65ee4aa9e7679ad0ce7e8a42bee6bf.zip
Fix fp in unnecessary_safety_comment
-rw-r--r--clippy_lints/src/undocumented_unsafe_blocks.rs12
-rw-r--r--tests/ui/unnecessary_safety_comment.rs17
2 files changed, 29 insertions, 0 deletions
diff --git a/clippy_lints/src/undocumented_unsafe_blocks.rs b/clippy_lints/src/undocumented_unsafe_blocks.rs
index 2e1b6d8d4ea..2920684ade3 100644
--- a/clippy_lints/src/undocumented_unsafe_blocks.rs
+++ b/clippy_lints/src/undocumented_unsafe_blocks.rs
@@ -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(
diff --git a/tests/ui/unnecessary_safety_comment.rs b/tests/ui/unnecessary_safety_comment.rs
index 7fefea7051d..89fedb145f8 100644
--- a/tests/ui/unnecessary_safety_comment.rs
+++ b/tests/ui/unnecessary_safety_comment.rs
@@ -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() {}