about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-07-07 18:06:50 +0530
committerGitHub <noreply@github.com>2022-07-07 18:06:50 +0530
commit4f9933afbd2f238b16fdfe7a7a07cb52554edfea (patch)
tree7307b21327473811195f50b18be4f42f43a11643
parent5483a7dd44a91e190ba8eb1361879ac3936cc415 (diff)
parent0d443d17eb17450da66a76c015933f30734e99d8 (diff)
downloadrust-4f9933afbd2f238b16fdfe7a7a07cb52554edfea.tar.gz
rust-4f9933afbd2f238b16fdfe7a7a07cb52554edfea.zip
Rollup merge of #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: https://github.com/rust-lang/rust/issues/97660, https://github.com/rust-lang/rust/issues/85549

And I guess that's it. Here have a magical unicorn :unicorn:
-rw-r--r--clippy_lints/src/duplicate_mod.rs22
-rw-r--r--tests/ui-cargo/duplicate_mod/fail/src/d.rs0
-rw-r--r--tests/ui-cargo/duplicate_mod/fail/src/main.rs13
-rw-r--r--tests/ui-cargo/duplicate_mod/fail/src/main.stderr19
4 files changed, 48 insertions, 6 deletions
diff --git a/clippy_lints/src/duplicate_mod.rs b/clippy_lints/src/duplicate_mod.rs
index c6c7b959d4f..4f49bb879f5 100644
--- a/clippy_lints/src/duplicate_mod.rs
+++ b/clippy_lints/src/duplicate_mod.rs
@@ -1,7 +1,7 @@
 use clippy_utils::diagnostics::span_lint_and_help;
 use rustc_ast::ast::{Crate, Inline, Item, ItemKind, ModKind};
 use rustc_errors::MultiSpan;
-use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
+use rustc_lint::{EarlyContext, EarlyLintPass, LintContext, Level};
 use rustc_session::{declare_tool_lint, impl_lint_pass};
 use rustc_span::{FileName, Span};
 use std::collections::BTreeMap;
@@ -49,6 +49,7 @@ declare_clippy_lint! {
 struct Modules {
     local_path: PathBuf,
     spans: Vec<Span>,
+    lint_levels: Vec<Level>,
 }
 
 #[derive(Default)]
@@ -70,13 +71,30 @@ impl EarlyLintPass for DuplicateMod {
             let modules = self.modules.entry(absolute_path).or_insert(Modules {
                 local_path,
                 spans: Vec::new(),
+                lint_levels: Vec::new(),
             });
             modules.spans.push(item.span_with_attributes());
+            modules.lint_levels.push(cx.get_lint_level(DUPLICATE_MOD));
         }
     }
 
     fn check_crate_post(&mut self, cx: &EarlyContext<'_>, _: &Crate) {
-        for Modules { local_path, spans } in self.modules.values() {
+        for Modules { local_path, spans, lint_levels } in self.modules.values() {
+            if spans.len() < 2 {
+                continue;
+            }
+
+            // At this point the lint would be emitted
+            assert_eq!(spans.len(), lint_levels.len());
+            let spans: Vec<_> = spans.into_iter().zip(lint_levels).filter_map(|(span, lvl)|{
+                if let Some(id) = lvl.get_expectation_id() {
+                    cx.fulfill_expectation(id);
+                }
+
+                (!matches!(lvl, Level::Allow | Level::Expect(_))).then_some(*span)
+            })
+            .collect();
+
             if spans.len() < 2 {
                 continue;
             }
diff --git a/tests/ui-cargo/duplicate_mod/fail/src/d.rs b/tests/ui-cargo/duplicate_mod/fail/src/d.rs
new file mode 100644
index 00000000000..e69de29bb2d
--- /dev/null
+++ b/tests/ui-cargo/duplicate_mod/fail/src/d.rs
diff --git a/tests/ui-cargo/duplicate_mod/fail/src/main.rs b/tests/ui-cargo/duplicate_mod/fail/src/main.rs
index 79b343da247..99ca538b6e4 100644
--- a/tests/ui-cargo/duplicate_mod/fail/src/main.rs
+++ b/tests/ui-cargo/duplicate_mod/fail/src/main.rs
@@ -1,3 +1,5 @@
+#[feature(lint_reasons)]
+
 mod a;
 
 mod b;
@@ -13,4 +15,15 @@ mod c3;
 mod from_other_module;
 mod other_module;
 
+mod d;
+#[path = "d.rs"]
+mod d2;
+#[path = "d.rs"]
+#[expect(clippy::duplicate_mod)]
+mod d3;
+#[path = "d.rs"]
+#[allow(clippy::duplicate_mod)]
+mod d4;
+
+
 fn main() {}
diff --git a/tests/ui-cargo/duplicate_mod/fail/src/main.stderr b/tests/ui-cargo/duplicate_mod/fail/src/main.stderr
index 00d7739c8a2..61df1ad5d50 100644
--- a/tests/ui-cargo/duplicate_mod/fail/src/main.stderr
+++ b/tests/ui-cargo/duplicate_mod/fail/src/main.stderr
@@ -1,5 +1,5 @@
 error: file is loaded as a module multiple times: `$DIR/b.rs`
-  --> $DIR/main.rs:3:1
+  --> $DIR/main.rs:5:1
    |
 LL |   mod b;
    |   ^^^^^^ first loaded here
@@ -11,7 +11,7 @@ LL | | mod b2;
    = help: replace all but one `mod` item with `use` items
 
 error: file is loaded as a module multiple times: `$DIR/c.rs`
-  --> $DIR/main.rs:7:1
+  --> $DIR/main.rs:9:1
    |
 LL |   mod c;
    |   ^^^^^^ first loaded here
@@ -25,7 +25,7 @@ LL | | mod c3;
    = help: replace all but one `mod` item with `use` items
 
 error: file is loaded as a module multiple times: `$DIR/from_other_module.rs`
-  --> $DIR/main.rs:13:1
+  --> $DIR/main.rs:15:1
    |
 LL |   mod from_other_module;
    |   ^^^^^^^^^^^^^^^^^^^^^^ first loaded here
@@ -38,5 +38,16 @@ LL | | mod m;
    |
    = help: replace all but one `mod` item with `use` items
 
-error: aborting due to 3 previous errors
+error: file is loaded as a module multiple times: `$DIR/b.rs`
+  --> $DIR/main.rs:18:1
+   |
+LL |   mod d;
+   |   ^^^^^^ first loaded here
+LL | / #[path = "d.rs"]
+LL | | mod d2;
+   | |_______^ loaded again here
+   |
+   = help: replace all but one `mod` item with `use` items
+
+error: aborting due to 4 previous errors