Skip to content

Commit

Permalink
Don't trigger unused_qualifications on global paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jieyouxu authored and Mark-Simulacrum committed Apr 29, 2024
1 parent 21dcfbd commit 0d61eae
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
7 changes: 7 additions & 0 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4665,6 +4665,13 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
}

fn lint_unused_qualifications(&mut self, path: &[Segment], ns: Namespace, finalize: Finalize) {
// Don't lint on global paths because the user explicitly wrote out the full path.
if let Some(seg) = path.first()
&& seg.ident.name == kw::PathRoot
{
return;
}

if path.iter().any(|seg| seg.ident.span.from_expansion()) {
return;
}
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/lint/lint-qualification.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ fn main() {
let _ = || -> Result<(), ()> { try!(Ok(())); Ok(()) }; // issue #37345

let _ = String::new(); //~ ERROR: unnecessary qualification
let _ = std::env::current_dir(); //~ ERROR: unnecessary qualification

let _: Vec<String> = Vec::<String>::new();
//~^ ERROR: unnecessary qualification
Expand All @@ -27,7 +26,7 @@ fn main() {
let _: std::fmt::Result = Ok(());
// don't report unnecessary qualification because fix(#122373) for issue #121331

let _ = <bool as Default>::default(); // issue #121999
let _ = <bool as Default>::default(); // issue #121999 (modified)
//~^ ERROR: unnecessary qualification

macro_rules! m { ($a:ident, $b:ident) => {
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/lint/lint-qualification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ fn main() {
let _ = || -> Result<(), ()> { try!(Ok(())); Ok(()) }; // issue #37345

let _ = std::string::String::new(); //~ ERROR: unnecessary qualification
let _ = ::std::env::current_dir(); //~ ERROR: unnecessary qualification

let _: std::vec::Vec<String> = std::vec::Vec::<String>::new();
//~^ ERROR: unnecessary qualification
Expand All @@ -27,7 +26,7 @@ fn main() {
let _: std::fmt::Result = Ok(());
// don't report unnecessary qualification because fix(#122373) for issue #121331

let _ = <bool as ::std::default::Default>::default(); // issue #121999
let _ = <bool as std::default::Default>::default(); // issue #121999 (modified)
//~^ ERROR: unnecessary qualification

macro_rules! m { ($a:ident, $b:ident) => {
Expand Down
30 changes: 9 additions & 21 deletions tests/ui/lint/lint-qualification.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,7 @@ LL + let _ = String::new();
|

error: unnecessary qualification
--> $DIR/lint-qualification.rs:19:13
|
LL | let _ = ::std::env::current_dir();
| ^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the unnecessary path segments
|
LL - let _ = ::std::env::current_dir();
LL + let _ = std::env::current_dir();
|

error: unnecessary qualification
--> $DIR/lint-qualification.rs:21:12
--> $DIR/lint-qualification.rs:20:12
|
LL | let _: std::vec::Vec<String> = std::vec::Vec::<String>::new();
| ^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -64,7 +52,7 @@ LL + let _: Vec<String> = std::vec::Vec::<String>::new();
|

error: unnecessary qualification
--> $DIR/lint-qualification.rs:21:36
--> $DIR/lint-qualification.rs:20:36
|
LL | let _: std::vec::Vec<String> = std::vec::Vec::<String>::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -76,7 +64,7 @@ LL + let _: std::vec::Vec<String> = Vec::<String>::new();
|

error: unused import: `std::fmt`
--> $DIR/lint-qualification.rs:25:9
--> $DIR/lint-qualification.rs:24:9
|
LL | use std::fmt;
| ^^^^^^^^
Expand All @@ -88,16 +76,16 @@ LL | #![deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: unnecessary qualification
--> $DIR/lint-qualification.rs:30:13
--> $DIR/lint-qualification.rs:29:13
|
LL | let _ = <bool as ::std::default::Default>::default(); // issue #121999
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | let _ = <bool as std::default::Default>::default(); // issue #121999 (modified)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the unnecessary path segments
|
LL - let _ = <bool as ::std::default::Default>::default(); // issue #121999
LL + let _ = <bool as Default>::default(); // issue #121999
LL - let _ = <bool as std::default::Default>::default(); // issue #121999 (modified)
LL + let _ = <bool as Default>::default(); // issue #121999 (modified)
|

error: aborting due to 8 previous errors
error: aborting due to 7 previous errors

12 changes: 12 additions & 0 deletions tests/ui/lint/unused-qualifications-global-paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Checks that `unused_qualifications` don't fire on explicit global paths.
// Issue: <https://github.com/rust-lang/rust/issues/122374>.

//@ check-pass

#![deny(unused_qualifications)]

pub fn bar() -> u64 {
::std::default::Default::default()
}

fn main() {}

0 comments on commit 0d61eae

Please sign in to comment.