Skip to content

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

Closed
@Kinrany

Description

@Kinrany
Contributor

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

added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Apr 22, 2020
dillona

dillona commented on Apr 27, 2020

@dillona
Contributor

It seems this regressed somewhere between 1.34.0 and 1.35.0

dillona

dillona commented on Apr 28, 2020

@dillona
Contributor

More accurately, it looks like the false positive has been present since the introduction of the warning in #58805

Kinrany

Kinrany commented on Apr 28, 2020

@Kinrany
ContributorAuthor

I was actually a little surprised that

use std::string::String;
use foo::*;

did not cause a compilation error due to two imports of the same name at the same level.

Kreijstal

Kreijstal commented on Mar 28, 2023

@Kreijstal

I got something similar while implementing ops::Div

warning: the item `panic` is imported redundantly
   --> src\main.rs:187:13
    |
47  | use core::*;
    |     ------- the item `panic` is already imported here
...
187 |         use core::panic;
    |             ^^^^^^^^^^^
    |
    = note: `#[warn(unused_imports)]` on by default

when removed, it errors

error[E0659]: `panic` is ambiguous
   --> src\main.rs:189:46
    |
189 |             (_, Number(a)) if a.is_zero() => panic!("no division by zero"),
    |                                              ^^^^^ ambiguous name
    |
    = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import 
or macro resolution
    = note: `panic` could refer to a macro from prelude
note: `panic` could also refer to the macro imported here
   --> src\main.rs:47:5
    |
47  | use core::*;
    |     ^^^^^^^
    = help: consider adding an explicit import of `panic` to disambiguate
    = help: or use `crate::panic` to refer to this macro unambiguously

error[E0659]: `panic` is ambiguous
   --> src\main.rs:191:17
    |
191 |                 panic!("Infinity/Infinity division")
    |                 ^^^^^ ambiguous name
    |
    = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import 
or macro resolution
    = note: `panic` could refer to a macro from prelude
note: `panic` could also refer to the macro imported here
   --> src\main.rs:47:5
    |
47  | use core::*;
    |     ^^^^^^^
    = help: consider adding an explicit import of `panic` to disambiguate
    = help: or use `crate::panic` to refer to this macro unambiguously
petrochenkov

petrochenkov commented on Mar 2, 2024

@petrochenkov
Contributor

This issue no longer reproduces and can be closed when a corresponding test is added.

added
E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
on Mar 2, 2024
added a commit that references this issue on Mar 4, 2024

11 remaining items

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

Metadata

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

      Participants

      @dillona@Kinrany@jonas-schievink@Kreijstal@petrochenkov

      Issue actions

        False positive unused_import warning on std::string::String · Issue #71450 · rust-lang/rust