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

Redundant imports from std prelude in maybe-no_std crate #12335

Closed
mumbleskates opened this issue Feb 23, 2024 · 2 comments
Closed

Redundant imports from std prelude in maybe-no_std crate #12335

mumbleskates opened this issue Feb 23, 2024 · 2 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@mumbleskates
Copy link

Summary

This appears to be a regression; it does not happen in nightly-2024-02-18, but happens in nightly-2024-02-19 and later (the rustc -Vv version below)

Lint Name

unused_imports

Reproducer

I tried this code, which (in the real code) has std-dependent functionality gated behind a feature:

#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

use alloc::boxed::Box;

pub fn make() -> Box<usize> {
    Box::new(1)
}

As of the nightly-2024-04-19 toolchain, it now produces these errors when the std feature is enabled, complaining that the alloc imports are redundant:

$ cargo clippy
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
$ cargo clippy --all-features
warning: the item `Box` is imported redundantly
   --> src/lib.rs:5:5
    |
5   | use alloc::boxed::Box;
    |     ^^^^^^^^^^^^^^^^^
    |
   ::: /home/widders/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/prelude/mod.rs:125:13
    |
125 |     pub use super::v1::*;
    |             --------- the item `Box` is already defined here
    |
    = note: `#[warn(unused_imports)]` on by default

warning: `clippytest` (lib) generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.08s

I expected to see this happen:

No warning with the feature either enabled or disabled :)

The imports it is complaining about are redundant, in theory... we could put each and every import for something that is missing in the no_std prelude but present in the std one behind a #[cfg(feature = "std")]. However: this feels bad, this looks bad, and until less than a week ago it was not requested by clippy. Therefore I suspect this is a regression.

Version

rustc 1.78.0-nightly (2bf78d12d 2024-02-18)
binary: rustc
commit-hash: 2bf78d12d33ae02d10010309a0d85dd04e7cff72
commit-date: 2024-02-18
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

Additional Labels

No response

@mumbleskates mumbleskates added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Feb 23, 2024
@y21
Copy link
Member

y21 commented Feb 23, 2024

unused_imports is a rustc lint (implemented in the compiler, not in clippy), so there's not much we can do about it here. We just inherit rustc lints (including its bugs); you should also see these warnings with a regular cargo check.

That said, this looks like the same issue described in rust-lang/rust#121362 ?

@mumbleskates
Copy link
Author

Thank you! You're very right, I think I was about to create an issue there and ended up making it in the wrong repo.

And thank you for finding that issue, that's the one! I wasn't having luck digging it up, but many hands make light work 🤝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

2 participants