Skip to content

Commit 4f9933a

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 5483a7d + 0d443d1 commit 4f9933a

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

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
}

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

Whitespace-only changes.

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() {}

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)