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

C-FEATURE: improve the example for conditional no_std #278

Merged
merged 1 commit into from
Mar 10, 2024
Merged

C-FEATURE: improve the example for conditional no_std #278

merged 1 commit into from
Mar 10, 2024

Conversation

kadiwa4
Copy link
Contributor

@kadiwa4 kadiwa4 commented Mar 3, 2024

Nightly rustc has recently expanded the unused_imports lint to warn in scenarios like this (rust-lang/rust#121708):

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

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "alloc")]
pub mod alloc_only {
    use alloc::boxed::Box;

    pub fn foo(_bar: Box<u8>) {}
}

(only gives a warning if feature = "std" is enabled)

warning
warning: the item `Box` is imported redundantly
   --> src/lib.rs:8:9
    |
8   |     use alloc::boxed::Box;
    |         ^^^^^^^^^^^^^^^^^
    |
   ::: /playground/.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

The underlying issue is that #![cfg_attr(not(feature = "std"), no_std)] will sometimes enable the std prelude. We should nudge people to not use the std prelude in no_std crates anymore by doing this:

#![no_std]

#[cfg(feature = "std")]
extern crate std;

The lint extension might be reverted again but I still think this should be the canonical way of including std.

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@dtolnay dtolnay merged commit 385ba00 into rust-lang:master Mar 10, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants