Skip to content

Commit 2cc0453

Browse files
authored
Rollup merge of rust-lang#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 rust-lang#92198.
2 parents efc3c71 + 0b5c683 commit 2cc0453

File tree

7 files changed

+98
-1
lines changed

7 files changed

+98
-1
lines changed

compiler/rustc_lint/src/context.rs

+8
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,14 @@ pub trait LintContext: Sized {
956956
db.span_note(glob_reexport_span, format!("the name `{}` in the {} namespace is supposed to be publicly re-exported here", name, namespace));
957957
db.span_note(private_item_span, "but the private item here shadows it".to_owned());
958958
}
959+
BuiltinLintDiagnostics::UnusedQualifications { path_span, unqualified_path } => {
960+
db.span_suggestion_verbose(
961+
path_span,
962+
"replace it with the unqualified path",
963+
unqualified_path,
964+
Applicability::MachineApplicable
965+
);
966+
}
959967
}
960968
// Rewrap `db`, and pass control to the user.
961969
decorate(db)

compiler/rustc_lint_defs/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,12 @@ pub enum BuiltinLintDiagnostics {
550550
/// The local binding that shadows the glob reexport.
551551
private_item_span: Span,
552552
},
553+
UnusedQualifications {
554+
/// The span of the unnecessarily-qualified path.
555+
path_span: Span,
556+
/// The replacement unqualified path.
557+
unqualified_path: Ident,
558+
},
553559
}
554560

555561
/// Lints that are buffered up early on in the `Session` before the

compiler/rustc_resolve/src/late.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -3922,11 +3922,15 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
39223922
};
39233923
if res == unqualified_result {
39243924
let lint = lint::builtin::UNUSED_QUALIFICATIONS;
3925-
self.r.lint_buffer.buffer_lint(
3925+
self.r.lint_buffer.buffer_lint_with_diagnostic(
39263926
lint,
39273927
finalize.node_id,
39283928
finalize.path_span,
39293929
"unnecessary qualification",
3930+
lint::BuiltinLintDiagnostics::UnusedQualifications {
3931+
path_span: finalize.path_span,
3932+
unqualified_path: path.last().unwrap().ident
3933+
}
39303934
)
39313935
}
39323936
}

tests/ui/lint/lint-qualification.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ note: the lint level is defined here
99
|
1010
LL | #![deny(unused_qualifications)]
1111
| ^^^^^^^^^^^^^^^^^^^^^
12+
help: replace it with the unqualified path
13+
|
14+
LL | bar();
15+
| ~~~
1216

1317
error: aborting due to previous error
1418

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// run-rustfix
2+
3+
#![deny(unused_qualifications)]
4+
5+
mod foo {
6+
pub fn bar() {}
7+
}
8+
9+
mod baz {
10+
pub mod qux {
11+
pub fn quux() {}
12+
}
13+
}
14+
15+
fn main() {
16+
use foo::bar;
17+
bar();
18+
//~^ ERROR unnecessary qualification
19+
20+
use baz::qux::quux;
21+
quux();
22+
//~^ ERROR unnecessary qualification
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// run-rustfix
2+
3+
#![deny(unused_qualifications)]
4+
5+
mod foo {
6+
pub fn bar() {}
7+
}
8+
9+
mod baz {
10+
pub mod qux {
11+
pub fn quux() {}
12+
}
13+
}
14+
15+
fn main() {
16+
use foo::bar;
17+
foo::bar();
18+
//~^ ERROR unnecessary qualification
19+
20+
use baz::qux::quux;
21+
baz::qux::quux();
22+
//~^ ERROR unnecessary qualification
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: unnecessary qualification
2+
--> $DIR/unused-qualifications-suggestion.rs:17:5
3+
|
4+
LL | foo::bar();
5+
| ^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-qualifications-suggestion.rs:3:9
9+
|
10+
LL | #![deny(unused_qualifications)]
11+
| ^^^^^^^^^^^^^^^^^^^^^
12+
help: replace it with the unqualified path
13+
|
14+
LL | bar();
15+
| ~~~
16+
17+
error: unnecessary qualification
18+
--> $DIR/unused-qualifications-suggestion.rs:21:5
19+
|
20+
LL | baz::qux::quux();
21+
| ^^^^^^^^^^^^^^
22+
|
23+
help: replace it with the unqualified path
24+
|
25+
LL | quux();
26+
| ~~~~
27+
28+
error: aborting due to 2 previous errors
29+

0 commit comments

Comments
 (0)