diff options
| author | Alejandra González <blyxyas@gmail.com> | 2025-04-01 18:05:23 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-01 18:05:23 +0000 |
| commit | e429bdebbdbfaad7b86caa0fa394e12e2dea22f5 (patch) | |
| tree | cfffaa6c6d4cb6ca0ff84e8a98980326d78ab07d | |
| parent | 46878e320a5c602c0600e0af25de5b32eb35517b (diff) | |
| parent | ffaecd008d8c2361d1043a20e06c7090f64193c3 (diff) | |
| download | rust-e429bdebbdbfaad7b86caa0fa394e12e2dea22f5.tar.gz rust-e429bdebbdbfaad7b86caa0fa394e12e2dea22f5.zip | |
Manually fulfill lint expectations for all unsafe blocks with metavars (#14501)
Fixes #14488 Context: the `macro_metavars_in_unsafe` lint looks for unsafe blocks with a macro span that then contain expressions with a root context span (which means that it is a macro with an unsafe block expanding a metavariable inside). In order to avoid emitting a warning for every single macro invocation, it will deduplicate the unsafe blocks by the span in the macro. This leads to the linked issue where because of the deduplicating and removing unsafe blocks that all belong to the same unsafe block in the macro, only one of the unsafe blocks will actually have its lint expectation fulfilled. This PR fixes that by manually fulfilling all of the unsafe blocks from all expansions before deduplicating them. changelog: [`macro_metavars_in_unsafe`]: fix unfulfilled `#[expect]` if macro is invoked multiple times
| -rw-r--r-- | clippy_lints/src/macro_metavars_in_unsafe.rs | 11 | ||||
| -rw-r--r-- | tests/ui-toml/macro_metavars_in_unsafe/default/test.rs | 16 |
2 files changed, 26 insertions, 1 deletions
diff --git a/clippy_lints/src/macro_metavars_in_unsafe.rs b/clippy_lints/src/macro_metavars_in_unsafe.rs index 006addb987f..0f9d373f0a3 100644 --- a/clippy_lints/src/macro_metavars_in_unsafe.rs +++ b/clippy_lints/src/macro_metavars_in_unsafe.rs @@ -5,7 +5,7 @@ use itertools::Itertools; use rustc_hir::def_id::LocalDefId; use rustc_hir::intravisit::{Visitor, walk_block, walk_expr, walk_stmt}; use rustc_hir::{BlockCheckMode, Expr, ExprKind, HirId, Stmt, UnsafeSource}; -use rustc_lint::{LateContext, LateLintPass}; +use rustc_lint::{LateContext, LateLintPass, Level, LintContext}; use rustc_session::impl_lint_pass; use rustc_span::{Span, SyntaxContext, sym}; use std::collections::BTreeMap; @@ -249,6 +249,15 @@ impl<'tcx> LateLintPass<'tcx> for ExprMetavarsInUnsafe { }) .flatten() .copied() + .inspect(|&unsafe_block| { + if let Level::Expect(id) = cx.tcx.lint_level_at_node(MACRO_METAVARS_IN_UNSAFE, unsafe_block).0 { + // Since we're going to deduplicate expanded unsafe blocks by its enclosing macro definition soon, + // which would lead to unfulfilled `#[expect()]`s in all other unsafe blocks that are filtered out + // except for the one we emit the warning at, we must manually fulfill the lint + // for all unsafe blocks here. + cx.fulfill_expectation(id); + } + }) .map(|id| { // Remove the syntax context to hide "in this macro invocation" in the diagnostic. // The invocation doesn't matter. Also we want to dedupe by the unsafe block and not by anything diff --git a/tests/ui-toml/macro_metavars_in_unsafe/default/test.rs b/tests/ui-toml/macro_metavars_in_unsafe/default/test.rs index 2465fe45645..d3d5b0c103e 100644 --- a/tests/ui-toml/macro_metavars_in_unsafe/default/test.rs +++ b/tests/ui-toml/macro_metavars_in_unsafe/default/test.rs @@ -251,6 +251,16 @@ pub mod issue13219 { } } +#[macro_export] +macro_rules! issue14488 { + ($e:expr) => { + #[expect(clippy::macro_metavars_in_unsafe)] + unsafe { + $e + } + }; +} + fn main() { allow_works!(1); simple!(1); @@ -271,4 +281,10 @@ fn main() { multiple_unsafe_blocks!(1, 1, 1); unsafe_from_root_ctxt!(unsafe { 1 }); nested_macros!(1, 1); + + // These two invocations lead to two expanded unsafe blocks, each with an `#[expect]` on it. + // Only of them gets a warning, which used to result in an unfulfilled expectation for the other + // expanded unsafe block. + issue14488!(1); + issue14488!(2); } |
