Skip to content

feat(lint): Add conditional_no_std_attribute lint#16971

Open
bushrat011899 wants to merge 1 commit intorust-lang:masterfrom
bushrat011899:lint_conditional_no_std_attribute
Open

feat(lint): Add conditional_no_std_attribute lint#16971
bushrat011899 wants to merge 1 commit intorust-lang:masterfrom
bushrat011899:lint_conditional_no_std_attribute

Conversation

@bushrat011899
Copy link
Copy Markdown

@bushrat011899 bushrat011899 commented May 7, 2026

Objective

Details

This adds a lint which discourages usage of cfg_attr with no_std. This is usually bad because it changes the implicit prelude across the crate, making it trivial to fall into a trap where either you are warned for redundant imports, or fail to compile because of missing ones. The lint will suggest replacing:

#![cfg_attr(some_arbitrary_condition, ..., no_std, ...)]

With:

#![cfg_attr(some_arbitrary_condition, ...)]
#![no_std]
#[cfg(not(some_arbitrary_condition))]
extern crate std;

Notes

  • No AI tooling of any kind was used during the creation of this PR.
  • This suggestion can fail if the inner attribute is not the last one (since it would insert the extern crate statement between inner attributes). I have marked it as Applicability::MaybeIncorrect to reflect this.
  • The cfg_attr statement will be removed entirely if the only attribute within it is no_std.
  • The lint checks for MSRV 1.6.0 (the version #![no_std] was stabilised in), as crates supporting older versions of Rust may use cfg_attr to add no_std support without raising MSRV.

changelog: [conditional_no_std_attribute]: add new lint for conditional inclusion of #![no_std] attribute

@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 7, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 7, 2026

r? @dswij

rustbot has assigned @dswij.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

@bushrat011899 bushrat011899 force-pushed the lint_conditional_no_std_attribute branch from 6a5603d to 9e7df92 Compare May 7, 2026 03:18
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 7, 2026

Lintcheck changes for cc4638b

Lint Added Removed Changed
clippy::conditional_no_std_attribute 47 0 0

This comment will be updated if you push new changes

Comment thread clippy_lints/src/attrs/mod.rs Outdated
Comment thread clippy_lints/src/attrs/mod.rs Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants