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.
This commit is contained in:
commit
2cc04536b4
7 changed files with 98 additions and 1 deletions
|
@ -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(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());
|
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.
|
// Rewrap `db`, and pass control to the user.
|
||||||
decorate(db)
|
decorate(db)
|
||||||
|
|
|
@ -550,6 +550,12 @@ pub enum BuiltinLintDiagnostics {
|
||||||
/// The local binding that shadows the glob reexport.
|
/// The local binding that shadows the glob reexport.
|
||||||
private_item_span: Span,
|
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
|
/// Lints that are buffered up early on in the `Session` before the
|
||||||
|
|
|
@ -3922,11 +3922,15 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
|
||||||
};
|
};
|
||||||
if res == unqualified_result {
|
if res == unqualified_result {
|
||||||
let lint = lint::builtin::UNUSED_QUALIFICATIONS;
|
let lint = lint::builtin::UNUSED_QUALIFICATIONS;
|
||||||
self.r.lint_buffer.buffer_lint(
|
self.r.lint_buffer.buffer_lint_with_diagnostic(
|
||||||
lint,
|
lint,
|
||||||
finalize.node_id,
|
finalize.node_id,
|
||||||
finalize.path_span,
|
finalize.path_span,
|
||||||
"unnecessary qualification",
|
"unnecessary qualification",
|
||||||
|
lint::BuiltinLintDiagnostics::UnusedQualifications {
|
||||||
|
path_span: finalize.path_span,
|
||||||
|
unqualified_path: path.last().unwrap().ident
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,10 @@ note: the lint level is defined here
|
||||||
|
|
|
|
||||||
LL | #![deny(unused_qualifications)]
|
LL | #![deny(unused_qualifications)]
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
help: replace it with the unqualified path
|
||||||
|
|
|
||||||
|
LL | bar();
|
||||||
|
| ~~~
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
|
23
tests/ui/resolve/unused-qualifications-suggestion.fixed
Normal file
23
tests/ui/resolve/unused-qualifications-suggestion.fixed
Normal file
|
@ -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
|
||||||
|
}
|
23
tests/ui/resolve/unused-qualifications-suggestion.rs
Normal file
23
tests/ui/resolve/unused-qualifications-suggestion.rs
Normal file
|
@ -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
|
||||||
|
}
|
29
tests/ui/resolve/unused-qualifications-suggestion.stderr
Normal file
29
tests/ui/resolve/unused-qualifications-suggestion.stderr
Normal file
|
@ -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
|
||||||
|
|
Loading…
Add table
Reference in a new issue