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

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

Closed
Kinrany opened this issue Apr 22, 2020 · 5 comments · Fixed by #121961
Closed

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

Kinrany opened this issue Apr 22, 2020 · 5 comments · Fixed by #121961
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Kinrany
Copy link
Contributor

Kinrany commented Apr 22, 2020

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).

@Kinrany Kinrany added the C-bug Category: This is a bug. label Apr 22, 2020
@jonas-schievink jonas-schievink added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 22, 2020
@dillona
Copy link
Contributor

dillona commented Apr 27, 2020

It seems this regressed somewhere between 1.34.0 and 1.35.0

@dillona
Copy link
Contributor

dillona commented Apr 28, 2020

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

@Kinrany
Copy link
Contributor Author

Kinrany commented Apr 28, 2020

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
Copy link

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
Copy link
Contributor

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

@petrochenkov petrochenkov added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Mar 2, 2024
surechen added a commit to surechen/rust that referenced this issue Mar 4, 2024
surechen added a commit to surechen/rust that referenced this issue Mar 5, 2024
surechen added a commit to surechen/rust that referenced this issue Mar 5, 2024
surechen added a commit to surechen/rust that referenced this issue Mar 5, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 5, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 5, 2024
…iaskrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#121280 (Implement MaybeUninit::fill{,_with,_from})
 - rust-lang#121438 (std support for wasm32 panic=unwind)
 - rust-lang#121658 (Hint user to update nightly on ICEs produced from outdated nightly)
 - rust-lang#121959 (Removing absolute path in proc-macro)
 - rust-lang#121961 (add test for rust-lang#78894 rust-lang#71450)
 - rust-lang#121975 (hir_analysis: enums return `None` in `find_field`)
 - rust-lang#121978 (Fix duplicated path in the "not found dylib" error)
 - rust-lang#121991 (Merge impl_trait_in_assoc_types_defined_by query back into `opaque_types_defined_by`)
 - rust-lang#122016 (will_wake tests fail on Miri and that is expected)
 - rust-lang#122018 (only set noalias on Box with the global allocator)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 5, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 5, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#121202 (Limit the number of names and values in check-cfg diagnostics)
 - rust-lang#121301 (errors: share `SilentEmitter` between rustc and rustfmt)
 - rust-lang#121658 (Hint user to update nightly on ICEs produced from outdated nightly)
 - rust-lang#121846 (only compare ambiguity item that have hard error)
 - rust-lang#121961 (add test for rust-lang#78894 rust-lang#71450)
 - rust-lang#121975 (hir_analysis: enums return `None` in `find_field`)
 - rust-lang#121978 (Fix duplicated path in the "not found dylib" error)
 - rust-lang#121991 (Merge impl_trait_in_assoc_types_defined_by query back into `opaque_types_defined_by`)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 6, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#121202 (Limit the number of names and values in check-cfg diagnostics)
 - rust-lang#121301 (errors: share `SilentEmitter` between rustc and rustfmt)
 - rust-lang#121658 (Hint user to update nightly on ICEs produced from outdated nightly)
 - rust-lang#121846 (only compare ambiguity item that have hard error)
 - rust-lang#121961 (add test for rust-lang#78894 rust-lang#71450)
 - rust-lang#121975 (hir_analysis: enums return `None` in `find_field`)
 - rust-lang#121978 (Fix duplicated path in the "not found dylib" error)
 - rust-lang#121991 (Merge impl_trait_in_assoc_types_defined_by query back into `opaque_types_defined_by`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in 5cdf870 Mar 6, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 6, 2024
Rollup merge of rust-lang#121961 - surechen:add_test_20240304, r=petrochenkov

add test for rust-lang#78894 rust-lang#71450

fixes