Skip to content

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

@p-avital

Description

@p-avital

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") }

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions