Skip to content

False positive unused_import warning on std::string::String #71450

Closed
@Kinrany

Description

This (very contrived) example emits a warning:

mod foo {
    pub struct String;
    
    #[derive(Debug)]
    pub struct Number;
}

fn main() {
    use std::string::String;
    use foo::*;
    
    let n = Number;
    let s = String::new();

    println!("{:?} {}", n, s);
}

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4333aa0dec7b94c3d2942e7380894003

warning: the item `String` is imported redundantly
  --> src/main.rs:9:9
   |
9  |     use std::string::String;
   |         ^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

This is a false positive because removing use std::string::String results in a compilation error:

error[E0599]: no function or associated item named `new` found for struct `foo::String` in the current scope
  --> src/main.rs:13:21
   |
2  |     pub struct String;
   |     ------------------ function or associated item `new` not found for this
...
13 |     let s = String::new();
   |                     ^^^ function or associated item not found in `foo::String`

Meta

$ rustc --version --verbose
rustc 1.42.0 (b8cedc004 2020-03-09)
binary: rustc
commit-hash: b8cedc00407a4c56a3bda1ed605c6fc166655447
commit-date: 2020-03-09
host: x86_64-pc-windows-msvc
release: 1.42.0
LLVM version: 9.0

Same for 1.43.0-beta.6 and 1.44.0-nightly (2020-04-21 45d050cde277b22a7558).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions