No unused import for std::f64 if only f64 types used

14cbcf6
Opened by johnthagen at 2023-04-05 17:33:38

I recently came across several instances of the following problem in a code base:

use std::f64; // No warning.

fn main() {
    let x: f64 = 0.0;
    println!("{}", x);
}

Why this didn't cause a warning baffled me for a while, until I realize that it was actually imported the type f64, even though I had been using it up until that point as a module like:

use std::f64;

fn main() {
    println!("{}", f64::consts::PI);
}

Then later, when I refactored the code (more complex than this toy example), something like this was left:

use std::f64; // No warning for "unused" import, but it is not necessary.
use std::f64::consts::PI;

fn main() {
    let x: f64 = 0.0;
    println!("{}", PI + x);
}

In this final case, it would be great if rustc warned about this since f64 is always implicitly in scope as a primitive type (I didn't think you could actually import it?).

Is this a case where the unused import lint got confused incorrectly?

  1. I had a look at this, but was unable to come up with a fix.

    In resolve_ident_in_module_unadjusted_ext there is a:

    self.record_use(ident, binding, restricted_shadowing);
    

    One idea would be to do:

    if PrimTy::from_name(ident.name).is_none() {
        self.record_use(ident, binding, restricted_shadowing);
    }
    

    But this doesn't work, because ident.name is simply f64 also for the usage that we want to record.

    Tor Hovland at 2021-11-11 16:11:06