Skip to content

Commit 469ca7e

Browse files
authored
Rollup merge of rust-lang#98507 - xFrednet:rfc-2383-manual-expectation-magic, r=wesleywiser
Finishing touches for `#[expect]` (RFC 2383) This PR adds documentation and some functionality to rustc's lint passes, to manually fulfill expectations. This is needed for some lints in Clippy. Hopefully, it should be one of the last things before we can move forward with stabilizing this feature. As part of this PR, I've also updated `clippy::duplicate_mod` to showcase how this new functionality can be used and to ensure that it works correctly. --- changelog: [`duplicate_mod`]: Fixed lint attribute interaction r? ```@wesleywiser``` cc: rust-lang#97660, rust-lang#85549 And I guess that's it. Here have a magical unicorn 🦄
2 parents 19021fc + a2810cd commit 469ca7e

File tree

7 files changed

+92
-7
lines changed

7 files changed

+92
-7
lines changed

compiler/rustc_lint/src/context.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use rustc_middle::middle::stability;
3434
use rustc_middle::ty::layout::{LayoutError, LayoutOfHelpers, TyAndLayout};
3535
use rustc_middle::ty::print::with_no_trimmed_paths;
3636
use rustc_middle::ty::{self, print::Printer, subst::GenericArg, RegisteredTools, Ty, TyCtxt};
37-
use rustc_session::lint::BuiltinLintDiagnostics;
37+
use rustc_session::lint::{BuiltinLintDiagnostics, LintExpectationId};
3838
use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId};
3939
use rustc_session::Session;
4040
use rustc_span::lev_distance::find_best_match_for_name;
@@ -906,6 +906,29 @@ pub trait LintContext: Sized {
906906
) {
907907
self.lookup(lint, None as Option<Span>, decorate);
908908
}
909+
910+
/// This returns the lint level for the given lint at the current location.
911+
fn get_lint_level(&self, lint: &'static Lint) -> Level;
912+
913+
/// This function can be used to manually fulfill an expectation. This can
914+
/// be used for lints which contain several spans, and should be suppressed,
915+
/// if either location was marked with an expectation.
916+
///
917+
/// Note that this function should only be called for [`LintExpectationId`]s
918+
/// retrieved from the current lint pass. Buffered or manually created ids can
919+
/// cause ICEs.
920+
fn fulfill_expectation(&self, expectation: LintExpectationId) {
921+
// We need to make sure that submitted expectation ids are correctly fulfilled suppressed
922+
// and stored between compilation sessions. To not manually do these steps, we simply create
923+
// a dummy diagnostic and emit is as usual, which will be suppressed and stored like a normal
924+
// expected lint diagnostic.
925+
self.sess()
926+
.struct_expect(
927+
"this is a dummy diagnostic, to submit and store an expectation",
928+
expectation,
929+
)
930+
.emit();
931+
}
909932
}
910933

911934
impl<'a> EarlyContext<'a> {
@@ -953,6 +976,10 @@ impl LintContext for LateContext<'_> {
953976
None => self.tcx.struct_lint_node(lint, hir_id, decorate),
954977
}
955978
}
979+
980+
fn get_lint_level(&self, lint: &'static Lint) -> Level {
981+
self.tcx.lint_level_at_node(lint, self.last_node_with_lint_attrs).0
982+
}
956983
}
957984

958985
impl LintContext for EarlyContext<'_> {
@@ -975,6 +1002,10 @@ impl LintContext for EarlyContext<'_> {
9751002
) {
9761003
self.builder.struct_lint(lint, span.map(|s| s.into()), decorate)
9771004
}
1005+
1006+
fn get_lint_level(&self, lint: &'static Lint) -> Level {
1007+
self.builder.lint_level(lint).0
1008+
}
9781009
}
9791010

9801011
impl<'tcx> LateContext<'tcx> {

compiler/rustc_lint_defs/src/builtin.rs

+5
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,11 @@ declare_lint! {
520520
/// The `expect` attribute can be removed if this is intended behavior otherwise
521521
/// it should be investigated why the expected lint is no longer issued.
522522
///
523+
/// In rare cases, the expectation might be emitted at a different location than
524+
/// shown in the shown code snippet. In most cases, the `#[expect]` attribute
525+
/// works when added to the outer scope. A few lints can only be expected
526+
/// on a crate level.
527+
///
523528
/// Part of RFC 2383. The progress is being tracked in [#54503]
524529
///
525530
/// [#54503]: https://github.com/rust-lang/rust/issues/54503

compiler/rustc_lint_defs/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ impl Level {
232232
Level::Deny | Level::Forbid => true,
233233
}
234234
}
235+
236+
pub fn get_expectation_id(&self) -> Option<LintExpectationId> {
237+
match self {
238+
Level::Expect(id) | Level::ForceWarn(Some(id)) => Some(*id),
239+
_ => None,
240+
}
241+
}
235242
}
236243

237244
/// Specification of a single lint.

src/tools/clippy/clippy_lints/src/duplicate_mod.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use rustc_ast::ast::{Crate, Inline, Item, ItemKind, ModKind};
33
use rustc_errors::MultiSpan;
4-
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
4+
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext, Level};
55
use rustc_session::{declare_tool_lint, impl_lint_pass};
66
use rustc_span::{FileName, Span};
77
use std::collections::BTreeMap;
@@ -49,6 +49,7 @@ declare_clippy_lint! {
4949
struct Modules {
5050
local_path: PathBuf,
5151
spans: Vec<Span>,
52+
lint_levels: Vec<Level>,
5253
}
5354

5455
#[derive(Default)]
@@ -70,13 +71,30 @@ impl EarlyLintPass for DuplicateMod {
7071
let modules = self.modules.entry(absolute_path).or_insert(Modules {
7172
local_path,
7273
spans: Vec::new(),
74+
lint_levels: Vec::new(),
7375
});
7476
modules.spans.push(item.span_with_attributes());
77+
modules.lint_levels.push(cx.get_lint_level(DUPLICATE_MOD));
7578
}
7679
}
7780

7881
fn check_crate_post(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
79-
for Modules { local_path, spans } in self.modules.values() {
82+
for Modules { local_path, spans, lint_levels } in self.modules.values() {
83+
if spans.len() < 2 {
84+
continue;
85+
}
86+
87+
// At this point the lint would be emitted
88+
assert_eq!(spans.len(), lint_levels.len());
89+
let spans: Vec<_> = spans.into_iter().zip(lint_levels).filter_map(|(span, lvl)|{
90+
if let Some(id) = lvl.get_expectation_id() {
91+
cx.fulfill_expectation(id);
92+
}
93+
94+
(!matches!(lvl, Level::Allow | Level::Expect(_))).then_some(*span)
95+
})
96+
.collect();
97+
8098
if spans.len() < 2 {
8199
continue;
82100
}

src/tools/clippy/tests/ui-cargo/duplicate_mod/fail/src/d.rs

Whitespace-only changes.

src/tools/clippy/tests/ui-cargo/duplicate_mod/fail/src/main.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[feature(lint_reasons)]
2+
13
mod a;
24

35
mod b;
@@ -13,4 +15,15 @@ mod c3;
1315
mod from_other_module;
1416
mod other_module;
1517

18+
mod d;
19+
#[path = "d.rs"]
20+
mod d2;
21+
#[path = "d.rs"]
22+
#[expect(clippy::duplicate_mod)]
23+
mod d3;
24+
#[path = "d.rs"]
25+
#[allow(clippy::duplicate_mod)]
26+
mod d4;
27+
28+
1629
fn main() {}

src/tools/clippy/tests/ui-cargo/duplicate_mod/fail/src/main.stderr

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: file is loaded as a module multiple times: `$DIR/b.rs`
2-
--> $DIR/main.rs:3:1
2+
--> $DIR/main.rs:5:1
33
|
44
LL | mod b;
55
| ^^^^^^ first loaded here
@@ -11,7 +11,7 @@ LL | | mod b2;
1111
= help: replace all but one `mod` item with `use` items
1212

1313
error: file is loaded as a module multiple times: `$DIR/c.rs`
14-
--> $DIR/main.rs:7:1
14+
--> $DIR/main.rs:9:1
1515
|
1616
LL | mod c;
1717
| ^^^^^^ first loaded here
@@ -25,7 +25,7 @@ LL | | mod c3;
2525
= help: replace all but one `mod` item with `use` items
2626

2727
error: file is loaded as a module multiple times: `$DIR/from_other_module.rs`
28-
--> $DIR/main.rs:13:1
28+
--> $DIR/main.rs:15:1
2929
|
3030
LL | mod from_other_module;
3131
| ^^^^^^^^^^^^^^^^^^^^^^ first loaded here
@@ -38,5 +38,16 @@ LL | | mod m;
3838
|
3939
= help: replace all but one `mod` item with `use` items
4040

41-
error: aborting due to 3 previous errors
41+
error: file is loaded as a module multiple times: `$DIR/b.rs`
42+
--> $DIR/main.rs:18:1
43+
|
44+
LL | mod d;
45+
| ^^^^^^ first loaded here
46+
LL | / #[path = "d.rs"]
47+
LL | | mod d2;
48+
| |_______^ loaded again here
49+
|
50+
= help: replace all but one `mod` item with `use` items
51+
52+
error: aborting due to 4 previous errors
4253

0 commit comments

Comments
 (0)