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 committed Mar 13, 2024
1 parent 9ce37dc commit fd7f617
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 @@ -4636,6 +4636,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 @@ -15,7 +15,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 @@ -24,7 +23,7 @@ fn main() {
use std::fmt;
let _: fmt::Result = Ok(()); //~ ERROR: unnecessary qualification

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 @@ -15,7 +15,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 @@ -24,7 +23,7 @@ fn main() {
use std::fmt;
let _: std::fmt::Result = Ok(()); //~ ERROR: unnecessary qualification

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:18: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:20:12
--> $DIR/lint-qualification.rs:19: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:20:36
--> $DIR/lint-qualification.rs:19: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: unnecessary qualification
--> $DIR/lint-qualification.rs:25:12
--> $DIR/lint-qualification.rs:24:12
|
LL | let _: std::fmt::Result = Ok(());
| ^^^^^^^^^^^^^^^^
Expand All @@ -88,16 +76,16 @@ LL + let _: fmt::Result = Ok(());
|

error: unnecessary qualification
--> $DIR/lint-qualification.rs:27:13
--> $DIR/lint-qualification.rs:26: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 fd7f617

Please sign in to comment.