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

Detect invalid cfg expressions, such as the cfg(feature_s_ = ...) typo #10224

Closed
p-avital opened this issue Jan 24, 2023 · 3 comments
Closed
Labels
A-lint Area: New lints

Comments

@p-avital
Copy link

What it does

It's generally easy to mistakenly use features = "x" instead of feature = "x" in cfgs.

To my knowledge, features = "x" doesn't currently have meaning, so it's likely to be the result of a mistake, and not intentional.

The main thing to look out for when looking for this pattern is to ensure that we're within a cfg context.

This can be generalized to ensuring that cfg conditions are always looking at "variables" that actually exist, such as target_family, target_os, etc...

Lint Name

invalid-cfg-condition

Category

correctness, suspicious

Advantage

  • Prevent accidental typos that fundamentally alter the behaviour

Drawbacks

  • If/when new "variables" are added to cfg conditions, the lint will need to be updated.

Example

#[cfg(features = "std")]
pub use std::error::Error;

const fn std_support_enabled() -> bool { cfg!(features = "std") }

Is highly likely to be an attempt at writing:

#[cfg(feature = "std")]
pub use std::error::Error;

const fn std_support_enabled() -> bool { cfg!(feature = "std") }
@p-avital p-avital added the A-lint Area: New lints label Jan 24, 2023
@taiki-e
Copy link
Member

taiki-e commented Jan 27, 2023

@epage
Copy link

epage commented Mar 4, 2024

FYI the cargo/rustc feature has been proposed for merge. Cargo has approved its side of it and its waiting on rustc.

See also #11649

@Alexendoo
Copy link
Member

Closing in favour of rust-lang/rust#82450

@Alexendoo Alexendoo closed this as not planned Won't fix, can't repro, duplicate, stale Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

4 participants