Skip to content

Lint against empty cfg(any()/all())#158136

Open
clubby789 wants to merge 1 commit into
rust-lang:mainfrom
clubby789:lint-empty-cfg
Open

Lint against empty cfg(any()/all())#158136
clubby789 wants to merge 1 commit into
rust-lang:mainfrom
clubby789:lint-empty-cfg

Conversation

@clubby789

@clubby789 clubby789 commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

View all comments

Detect usages of cfg(any()) and cfg(all()) and suggest the literal boolean equivalents. When --lint-rust-version is available, gate the diagnostic appropriately.
Will need to be updated for #158134

Implements the lint suggested in rust-lang/rfcs#3695 (Tracking: #131204)
Tracking issue: #157574

@rustbot

rustbot commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 19, 2026
@rustbot

rustbot commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

r? @Kivooeo

rustbot has assigned @Kivooeo.
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: compiler
  • compiler expanded to 73 candidates
  • Random selection from 19 candidates

@clubby789 clubby789 removed O-windows Operating system: Windows T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 19, 2026
@rust-log-analyzer

This comment has been minimized.

@rustbot

rustbot commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

miri is developed in its own repository. If possible, consider making this change to rust-lang/miri instead.

cc @rust-lang/miri

@rustbot rustbot added the O-windows Operating system: Windows label Jun 19, 2026
@mejrs

mejrs commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Does this lint fire if the cfg predicate is produced in a macro expansion? I can see some valid use cases for producing all() etc in a macro.

Comment thread library/std/src/os/windows/raw.rs
@clubby789

Copy link
Copy Markdown
Contributor Author

Does this lint fire if the cfg predicate is produced in a macro expansion? I can see some valid use cases for producing all() etc in a macro.

No;

if list.is_empty() && !list.span.from_expansion() {

gates the emission.

@rust-log-analyzer

This comment has been minimized.

@rustbot

rustbot commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

clippy is developed in its own repository. If possible, consider making this change to rust-lang/rust-clippy instead.

cc @rust-lang/clippy

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Jun 19, 2026
@clubby789

Copy link
Copy Markdown
Contributor Author

This does overlap somewhat with clippy::non_minimal_cfg

Comment thread compiler/rustc_attr_parsing/src/attributes/cfg.rs
Comment thread tests/ui/lint/empty-cfg-predicate.rs Outdated
Comment thread compiler/rustc_lint_defs/src/builtin.rs
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 20, 2026
@rustbot

rustbot commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@clubby789 clubby789 added the I-lang-nominated Nominated for discussion during a lang team meeting. label Jun 21, 2026
@rustbot

rustbot commented Jun 21, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@JonathanBrouwer JonathanBrouwer added the S-waiting-on-t-lang Status: Awaiting decision from T-lang label Jun 21, 2026

@JonathanBrouwer JonathanBrouwer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

r=me after T-lang approves

View changes since this review

@clubby789 clubby789 removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 21, 2026
@traviscross traviscross added the P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang label Jun 24, 2026
/// `cfg(false)` and `cfg(true)` respectively may be used instead.
/// This used to be a common pattern before `cfg(true)` and `cfg(false)`
/// were added to the language in Rust 1.88
pub EMPTY_CFG_PREDICATE,

@traviscross traviscross Jun 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub EMPTY_CFG_PREDICATE,
pub EMPTY_CFG_PREDICATES,

Per our RFC 0344 guidance, this should be in the plural.

Also, the naming seems a bit loose. This is targeting two specific cfg predicates, not empty cfg predicates in general (and, of course, we might later add more cfg predicates that we might not want to lint when empty). Have any more targeted ideas for a name?

View changes since the review

@clubby789 clubby789 Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe EMPTY_CFG_COMBINATORS? Or EMPTY_CFG_LISTS (but that's a little less precise)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Or MANUAL_CFG_BOOL?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's probably my preferred suggestion so far (or MANUAL_CFG_BOOLS?), but open to others

@nikomatsakis

This comment was marked as outdated.

@nikomatsakis

Copy link
Copy Markdown
Contributor

My take is:

  • Personally, I think cargo clippy being a distinct tool instead of a lint-group etc is bad ux. I don't care to try and addrress this. I love the name clippy and it has an amazing brand. I have heard others feel differently than me. It's not really the high-order bit here.
  • We do need some sense of what kind of lints we want to have be warn/deny by default versus not and so forth. I think we should have clear rules and that warn-by-default should be used sparingly, it's better than people "opt in" to stylistic or pedantic lints.

I would like us to develop checklists and guidelines. We've tried a few times. We should just put something on the lang-team page and adjust it over time. I think there is generally settled precedent that...

Warn-by-default:

  • Likely to be a bug
  • Things that will change in the future (forward compat warnings) -- these additionally signal in dependencies if they will impact you even as a crate consumer
  • Key stylistic conventions (e.g., capitalization), particularly those that help us avoid bugs

Deny-by-default:

  • Overwhelmingly likely to be a bug

Allow-by-default:

  • Fits into the above categories but only for narrow sets of crates; obvious discoverability problem, for this, named groups are useful, this story is undeveloped.

None of these categories covers this lint. But I think there is another category of lint that may merit warn-by-default, which is to nudge people away from older style. This was compared in the meeting to an edition idiom lint, which makes sense to me, I think edition idiom lints probably could or should be warn-by-default in the new editions in the same way.

@nikomatsakis

nikomatsakis commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Personally I think putting our "thumb on the scale" to push people to adopt "new and better Rust" is a good idea, it benefits the whole ecosystem if there is a consistent idiomatic Rust and not a wide spread of usage. Many long-time Rust users will not learn about features any other way. I think this is why we warn for key stylistic conventions as well.

I do think clippy, in practice, largely serves this purpose though -- e.g., for consistency, we may want to flag on failure to use if let-conditions and so forth. Perhaps then clippy is the right answer, but this bring us back to discoverability. This seems a bit more extreme than that one, in that cfg(any()) is legitimately confusing.

@joshtriplett joshtriplett added the T-lang Relevant to the language team label Jun 24, 2026
@nikomatsakis

Copy link
Copy Markdown
Contributor

Try this again...

@rfcbot fcp merge lang

We discussed this in the lang-team meeting. Everyone agreed the newer style is better. There was some question about how far we want to go in stylistic lints and where the line between rustc and clippy falls.

I'm moving to merge to help move the discussion forward. @traviscross mentioned in the meeting that he would raise a concern about finalizing the naming, which makes sense. I think he or others may also raise a concern to talk about the bigger question (I'll give my thoughts on that separately).

@rust-rfcbot

rust-rfcbot commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

@nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jun 24, 2026
@traviscross

Copy link
Copy Markdown
Contributor

@rfcbot concern naming

There's an open discussion on the name for this in #158136 (comment). Since the name is part of what we accept in the FCP, I'd like to let this settle first.

@traviscross

Copy link
Copy Markdown
Contributor

@rfcbot concern consistency-and-plan

This lint goes beyond what we normally do with warn-by-default rustc lints. It's not linting against a likely bug or typographical error. It's not pushing a key stylistic convention, such as capitalization, that we lean on as a language matter. It's not something with unclear or shifting semantics that we're trying to drive out of the language. It's a minor stylistic point.

I'm not sure that we should light up CI for people on this.

That said, I agree that it can sometimes make sense to push our perspective on preferred usage with linting. If we want to absorb more of Clippy into rustc by creating appropriate lint groups and discussing whether lints in those groups should be allow-by-default or warn-by-default, I'm open to that. I'd want to finish that discussion first and agree on a plan (probably in the form of an RFC) before accepting lints that break from our established conventions and only make sense in that world.

@tmandry

tmandry commented Jun 24, 2026

Copy link
Copy Markdown
Member

In today's lang meeting I mentioned waiting for --lint-rust-version to become available, but I decided against filing a concern. cfg(false) and friends are not something I expect to see often in the published version of crates carrying an MSRV. We don't need that level of UX polish for a rare edge case.

@traviscross traviscross added I-lang-radar Items that are on lang's radar and will need eventual work or consideration. and removed I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang labels Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. O-windows Operating system: Windows proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-t-lang Status: Awaiting decision from T-lang T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.