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

unused import lint is misidentifying/blaming globs #8847

Closed
pnkfelix opened this issue Aug 29, 2013 · 1 comment
Closed

unused import lint is misidentifying/blaming globs #8847

pnkfelix opened this issue Aug 29, 2013 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@pnkfelix
Copy link
Member

Spawned off of "fun note" at end of #7663.

When a unused import (either specific or glob) is followed by a glob import that shadows it (and thus causes the first to go unused), the unused import lint is incorrectly blaming/identifying the latter glob as being unused, and failing to identify the former import as being unused.

Nearly exhaustive and certainly exhausting test case (multuse.rs):

mod A {
    pub fn p() { println("A::p"); }
}
mod B {
    pub fn p() { println("B::p"); }
}

mod C {
    pub fn q() { println("C::q"); }
}
mod D {
    pub fn q() { println("D::q"); }
}

mod E {
    pub fn r() { println("E::r"); }
}
mod F {
    pub fn r() { println("E::r"); }
}

mod G {
    pub fn s() { println("G::s"); }
    pub fn t() { println("G::t"); }
}
mod H {
    pub fn s() { println("H::s"); }
}

mod I {
    pub fn u() { println("I::u"); }
    pub fn v() { println("I::v"); }
}
mod J {
    pub fn u() { println("J::u"); }
    pub fn v() { println("J::v"); }
}

mod K {
    pub fn w() { println("K::w"); }
}
mod L {
    pub fn w() { println("L::w"); }
}

mod m {
   use A::p; // <-- this `p` is unused; correctly warns
   use B::p;
   use C::q; // <-- this `q` is unused, but NO WARNING
   use D::*; // <-- this `q` IS USED, yields false positive warning
   use E::*; // <-- this `r` is overridden and unused; correctly warns
   use F::r;
   use G::*; // <-- this `s` is overridden, no warning since t is used, as expected for glob.
   use H::*; // <-- this `s` is USED, yields false positive warning
   use I::*; // <-- this `v` is overridden, no warning since u is used, as expected for glob
   use J::v;
   use K::*; // <-- all imports here are overriden, none used, but NO WARNING
   use L::*; // <-- this `w` is USED, yields false positive warning

   #[main]
   fn my_main() {
       p();
       q();
       r();
       s();
       t();
       u();
       v();
       w();
   }
}

Result of the run (to confirm what the comments next to the uses say):

% rustc /tmp/multuse.rs && /tmp/multuse
/tmp/multuse.rs:47:7: 47:11 warning: unused import [-W unused-imports (default)]
/tmp/multuse.rs:47    use A::p; // <-- this `p` is unused; correctly warns
                          ^~~~
/tmp/multuse.rs:50:7: 50:12 warning: unused import [-W unused-imports (default)]
/tmp/multuse.rs:50    use D::*; // <-- this `q` IS USED, yields false positive warning
                          ^~~~~
/tmp/multuse.rs:51:7: 51:12 warning: unused import [-W unused-imports (default)]
/tmp/multuse.rs:51    use E::*; // <-- this `r` is overridden and unused; correctly warns
                          ^~~~~
/tmp/multuse.rs:54:7: 54:12 warning: unused import [-W unused-imports (default)]
/tmp/multuse.rs:54    use H::*; // <-- this `s` is USED, yields false positive warning
                          ^~~~~
/tmp/multuse.rs:58:7: 58:12 warning: unused import [-W unused-imports (default)]
/tmp/multuse.rs:58    use L::*; // <-- this `w` is USED, yields false positive warning
                          ^~~~~
warning: no debug symbols in executable (-arch x86_64)
B::p
D::q
E::r
H::s
G::t
I::u
J::v
L::w
@alexcrichton
Copy link
Member

Just want to say, this is an excellent test

flip1995 pushed a commit to flip1995/rust that referenced this issue Jun 4, 2022
add [`as_underscore`] lint

closes rust-lang#8847

detect usage of `as _` and enforce the usage of explicit type like
```rust
fn foo(n: usize) {}
let n: u16 = 256;
foo(n as _);
```
will suggest to change to
```rust
fn foo(n: usize) {}
let n: u16 = 256;
foo(n as usize);
```

changelog: add [`as_underscore`] lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants