diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-07-14 01:03:07 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-14 01:03:07 +0200 |
| commit | 2cc04536b4d0cec1c7831550106463d64b8fb739 (patch) | |
| tree | 2bd3b2273221daaf4df58696f3952f8dcad95ce9 | |
| parent | efc3c71c16d91d63e87c552088d2b6f27bff4177 (diff) | |
| parent | 0b5c683b06bba30cce4eb8528132a15bb1aa8edb (diff) | |
| download | rust-2cc04536b4d0cec1c7831550106463d64b8fb739.tar.gz rust-2cc04536b4d0cec1c7831550106463d64b8fb739.zip | |
Rollup merge of #112729 - jieyouxu:unused-qualifications-suggestion, r=b-naber
Add machine-applicable suggestion for `unused_qualifications` lint ``` error: unnecessary qualification --> $DIR/unused-qualifications-suggestion.rs:17:5 | LL | foo::bar(); | ^^^^^^^^ | note: the lint level is defined here --> $DIR/unused-qualifications-suggestion.rs:3:9 | LL | #![deny(unused_qualifications)] | ^^^^^^^^^^^^^^^^^^^^^ help: replace it with the unqualified path | LL | bar(); | ~~~ ``` Closes #92198.
| -rw-r--r-- | compiler/rustc_lint/src/context.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_lint_defs/src/lib.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/late.rs | 6 | ||||
| -rw-r--r-- | tests/ui/lint/lint-qualification.stderr | 4 | ||||
| -rw-r--r-- | tests/ui/resolve/unused-qualifications-suggestion.fixed | 23 | ||||
| -rw-r--r-- | tests/ui/resolve/unused-qualifications-suggestion.rs | 23 | ||||
| -rw-r--r-- | tests/ui/resolve/unused-qualifications-suggestion.stderr | 29 |
7 files changed, 98 insertions, 1 deletions
diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index 3761754f3ae..4770ed66393 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -956,6 +956,14 @@ pub trait LintContext: Sized { db.span_note(glob_reexport_span, format!("the name `{}` in the {} namespace is supposed to be publicly re-exported here", name, namespace)); db.span_note(private_item_span, "but the private item here shadows it".to_owned()); } + BuiltinLintDiagnostics::UnusedQualifications { path_span, unqualified_path } => { + db.span_suggestion_verbose( + path_span, + "replace it with the unqualified path", + unqualified_path, + Applicability::MachineApplicable + ); + } } // Rewrap `db`, and pass control to the user. decorate(db) diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 5a5031b7919..f6ffd46b1fe 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -550,6 +550,12 @@ pub enum BuiltinLintDiagnostics { /// The local binding that shadows the glob reexport. private_item_span: Span, }, + UnusedQualifications { + /// The span of the unnecessarily-qualified path. + path_span: Span, + /// The replacement unqualified path. + unqualified_path: Ident, + }, } /// Lints that are buffered up early on in the `Session` before the diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index a715a1453a3..f36b01c1a1f 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -3922,11 +3922,15 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { }; if res == unqualified_result { let lint = lint::builtin::UNUSED_QUALIFICATIONS; - self.r.lint_buffer.buffer_lint( + self.r.lint_buffer.buffer_lint_with_diagnostic( lint, finalize.node_id, finalize.path_span, "unnecessary qualification", + lint::BuiltinLintDiagnostics::UnusedQualifications { + path_span: finalize.path_span, + unqualified_path: path.last().unwrap().ident + } ) } } diff --git a/tests/ui/lint/lint-qualification.stderr b/tests/ui/lint/lint-qualification.stderr index 149a782d97c..d09cb78c4f0 100644 --- a/tests/ui/lint/lint-qualification.stderr +++ b/tests/ui/lint/lint-qualification.stderr @@ -9,6 +9,10 @@ note: the lint level is defined here | LL | #![deny(unused_qualifications)] | ^^^^^^^^^^^^^^^^^^^^^ +help: replace it with the unqualified path + | +LL | bar(); + | ~~~ error: aborting due to previous error diff --git a/tests/ui/resolve/unused-qualifications-suggestion.fixed b/tests/ui/resolve/unused-qualifications-suggestion.fixed new file mode 100644 index 00000000000..0d4b9007c7b --- /dev/null +++ b/tests/ui/resolve/unused-qualifications-suggestion.fixed @@ -0,0 +1,23 @@ +// run-rustfix + +#![deny(unused_qualifications)] + +mod foo { + pub fn bar() {} +} + +mod baz { + pub mod qux { + pub fn quux() {} + } +} + +fn main() { + use foo::bar; + bar(); + //~^ ERROR unnecessary qualification + + use baz::qux::quux; + quux(); + //~^ ERROR unnecessary qualification +} diff --git a/tests/ui/resolve/unused-qualifications-suggestion.rs b/tests/ui/resolve/unused-qualifications-suggestion.rs new file mode 100644 index 00000000000..f6722e96537 --- /dev/null +++ b/tests/ui/resolve/unused-qualifications-suggestion.rs @@ -0,0 +1,23 @@ +// run-rustfix + +#![deny(unused_qualifications)] + +mod foo { + pub fn bar() {} +} + +mod baz { + pub mod qux { + pub fn quux() {} + } +} + +fn main() { + use foo::bar; + foo::bar(); + //~^ ERROR unnecessary qualification + + use baz::qux::quux; + baz::qux::quux(); + //~^ ERROR unnecessary qualification +} diff --git a/tests/ui/resolve/unused-qualifications-suggestion.stderr b/tests/ui/resolve/unused-qualifications-suggestion.stderr new file mode 100644 index 00000000000..c8e91e07295 --- /dev/null +++ b/tests/ui/resolve/unused-qualifications-suggestion.stderr @@ -0,0 +1,29 @@ +error: unnecessary qualification + --> $DIR/unused-qualifications-suggestion.rs:17:5 + | +LL | foo::bar(); + | ^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/unused-qualifications-suggestion.rs:3:9 + | +LL | #![deny(unused_qualifications)] + | ^^^^^^^^^^^^^^^^^^^^^ +help: replace it with the unqualified path + | +LL | bar(); + | ~~~ + +error: unnecessary qualification + --> $DIR/unused-qualifications-suggestion.rs:21:5 + | +LL | baz::qux::quux(); + | ^^^^^^^^^^^^^^ + | +help: replace it with the unqualified path + | +LL | quux(); + | ~~~~ + +error: aborting due to 2 previous errors + |
