Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix removal span calculation of unused_qualifications suggestion #113811

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions compiler/rustc_lint/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,11 +956,11 @@ 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 } => {
BuiltinLintDiagnostics::UnusedQualifications { removal_span } => {
db.span_suggestion_verbose(
path_span,
"replace it with the unqualified path",
unqualified_path,
removal_span,
"remove the unnecessary path segments",
"",
Applicability::MachineApplicable
);
}
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,8 @@ pub enum BuiltinLintDiagnostics {
private_item_span: Span,
},
UnusedQualifications {
/// The span of the unnecessarily-qualified path.
path_span: Span,
/// The replacement unqualified path.
unqualified_path: Ident,
/// The span of the unnecessarily-qualified path to remove.
removal_span: Span,
},
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3911,8 +3911,9 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
&& path[0].ident.name != kw::PathRoot
&& path[0].ident.name != kw::DollarCrate
{
let last_segment = *path.last().unwrap();
let unqualified_result = {
match self.resolve_path(&[*path.last().unwrap()], Some(ns), None) {
match self.resolve_path(&[last_segment], Some(ns), None) {
PathResult::NonModule(path_res) => path_res.expect_full_res(),
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
module.res().unwrap()
Expand All @@ -3928,8 +3929,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
finalize.path_span,
"unnecessary qualification",
lint::BuiltinLintDiagnostics::UnusedQualifications {
path_span: finalize.path_span,
unqualified_path: path.last().unwrap().ident
removal_span: finalize.path_span.until(last_segment.ident.span),
}
)
}
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/lint/lint-qualification.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// run-rustfix
#![deny(unused_qualifications)]
#![allow(deprecated)]

mod foo {
pub fn bar() {}
}

fn main() {
use foo::bar;
bar(); //~ ERROR: unnecessary qualification
bar();

let _ = || -> Result<(), ()> { try!(Ok(())); Ok(()) }; // issue #37345

macro_rules! m { () => {
$crate::foo::bar(); // issue #37357
::foo::bar(); // issue #38682
} }
m!();
}
1 change: 1 addition & 0 deletions tests/ui/lint/lint-qualification.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// run-rustfix
#![deny(unused_qualifications)]
#![allow(deprecated)]

Expand Down
11 changes: 6 additions & 5 deletions tests/ui/lint/lint-qualification.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
error: unnecessary qualification
--> $DIR/lint-qualification.rs:10:5
--> $DIR/lint-qualification.rs:11:5
|
LL | foo::bar();
| ^^^^^^^^
|
note: the lint level is defined here
--> $DIR/lint-qualification.rs:1:9
--> $DIR/lint-qualification.rs:2:9
|
LL | #![deny(unused_qualifications)]
| ^^^^^^^^^^^^^^^^^^^^^
help: replace it with the unqualified path
help: remove the unnecessary path segments
|
LL - foo::bar();
LL + bar();
|
LL | bar();
| ~~~

error: aborting due to previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// run-rustfix

#![deny(unused_qualifications)]
#![feature(unsized_fn_params)]

#[allow(unused_imports)]
use std::ops;
use std::ops::Index;

pub struct A;

impl Index<str> for A {
//~^ ERROR unnecessary qualification
type Output = ();
fn index(&self, _: str) -> &Self::Output {
&()
}
}

mod inner {
pub trait Trait<T> {}
}

// the import needs to be here for the lint to show up
#[allow(unused_imports)]
use inner::Trait;

impl Trait<u8> for () {}
//~^ ERROR unnecessary qualification

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// run-rustfix

#![deny(unused_qualifications)]
#![feature(unsized_fn_params)]

#[allow(unused_imports)]
use std::ops;
use std::ops::Index;

pub struct A;

impl ops::Index<str> for A {
//~^ ERROR unnecessary qualification
type Output = ();
fn index(&self, _: str) -> &Self::Output {
&()
}
}

mod inner {
pub trait Trait<T> {}
}

// the import needs to be here for the lint to show up
#[allow(unused_imports)]
use inner::Trait;

impl inner::Trait<u8> for () {}
//~^ ERROR unnecessary qualification

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
error: unnecessary qualification
--> $DIR/issue-113808-invalid-unused-qualifications-suggestion.rs:12:6
|
LL | impl ops::Index<str> for A {
| ^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/issue-113808-invalid-unused-qualifications-suggestion.rs:3:9
|
LL | #![deny(unused_qualifications)]
| ^^^^^^^^^^^^^^^^^^^^^
help: remove the unnecessary path segments
|
LL - impl ops::Index<str> for A {
LL + impl Index<str> for A {
|

error: unnecessary qualification
--> $DIR/issue-113808-invalid-unused-qualifications-suggestion.rs:28:6
|
LL | impl inner::Trait<u8> for () {}
| ^^^^^^^^^^^^^^^^
|
help: remove the unnecessary path segments
|
LL - impl inner::Trait<u8> for () {}
LL + impl Trait<u8> for () {}
|

error: aborting due to 2 previous errors

14 changes: 8 additions & 6 deletions tests/ui/resolve/unused-qualifications-suggestion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ note: the lint level is defined here
|
LL | #![deny(unused_qualifications)]
| ^^^^^^^^^^^^^^^^^^^^^
help: replace it with the unqualified path
help: remove the unnecessary path segments
|
LL - foo::bar();
LL + bar();
|
LL | bar();
| ~~~

error: unnecessary qualification
--> $DIR/unused-qualifications-suggestion.rs:21:5
|
LL | baz::qux::quux();
| ^^^^^^^^^^^^^^
|
help: replace it with the unqualified path
help: remove the unnecessary path segments
|
LL - baz::qux::quux();
LL + quux();
|
LL | quux();
| ~~~~

error: aborting due to 2 previous errors