Skip to content

Commit b883645

Browse files
committed
manually fulfill lint expectations for all unsafe blocks with metavars
1 parent 4c610e3 commit b883645

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

clippy_lints/src/macro_metavars_in_unsafe.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use itertools::Itertools;
55
use rustc_hir::def_id::LocalDefId;
66
use rustc_hir::intravisit::{Visitor, walk_block, walk_expr, walk_stmt};
77
use rustc_hir::{BlockCheckMode, Expr, ExprKind, HirId, Stmt, UnsafeSource};
8-
use rustc_lint::{LateContext, LateLintPass};
8+
use rustc_lint::{LateContext, LateLintPass, Level, LintContext};
99
use rustc_session::impl_lint_pass;
1010
use rustc_span::{Span, SyntaxContext, sym};
1111
use std::collections::BTreeMap;
@@ -249,6 +249,14 @@ impl<'tcx> LateLintPass<'tcx> for ExprMetavarsInUnsafe {
249249
})
250250
.flatten()
251251
.copied()
252+
.inspect(|&unsafe_block| {
253+
if let Level::Expect(id) = cx.tcx.lint_level_at_node(MACRO_METAVARS_IN_UNSAFE, unsafe_block).0 {
254+
// Since we're going to deduplicate expanded unsafe blocks by its enclosing macro definition soon,
255+
// which would lead to unfulfilled `#[expect()]`s in all other unsafe blocks that are filtered out except for the one
256+
// we emit the warning at, we must manually fulfill the lint for all unsafe blocks here.
257+
cx.fulfill_expectation(id);
258+
}
259+
})
252260
.map(|id| {
253261
// Remove the syntax context to hide "in this macro invocation" in the diagnostic.
254262
// The invocation doesn't matter. Also we want to dedupe by the unsafe block and not by anything

tests/ui-toml/macro_metavars_in_unsafe/default/test.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ pub mod issue13219 {
251251
}
252252
}
253253

254+
#[macro_export]
255+
macro_rules! issue14488 {
256+
($e:expr) => {
257+
#[expect(clippy::macro_metavars_in_unsafe)]
258+
unsafe { $e }
259+
}
260+
}
261+
254262
fn main() {
255263
allow_works!(1);
256264
simple!(1);
@@ -271,4 +279,10 @@ fn main() {
271279
multiple_unsafe_blocks!(1, 1, 1);
272280
unsafe_from_root_ctxt!(unsafe { 1 });
273281
nested_macros!(1, 1);
282+
283+
// These two invocations lead to two expanded unsafe blocks, each with an `#[expect]` on it.
284+
// Only of them gets a warning, which used to result in an unfulfilled expectation for the other
285+
// expanded unsafe block.
286+
issue14488!(1);
287+
issue14488!(2);
274288
}

0 commit comments

Comments
 (0)