Skip to content

Lint suggestion: undeclared_feature #5660

Closed
@pscott

Description

@pscott

Hi,

This issue suggests a new lint: undeclared_feature. I tried following the guidelines, but feel free to change the name obviously.

Take a fresh Cargo.toml file and add time as an optional dependency. Now write this code:

#[cfg(feature=time)]
fn main() {
  println!("foo");
}

#[cfg(not(feature=time))]
fn main() {
  println!("bar");
}

You can then run:

$ cargo run
bar

and

$ cargo run --features time
foo

I believe this is bad practice because this code effectively has a "hidden" feature, which should probably be documented and exposed via the Cargo.toml file. What do you think? Is this good / bad practice, and would a clippy lint be appropriate?

Activity

pscott

pscott commented on May 28, 2020

@pscott
Author

I would ideally like to submit a PR for this issue. I'm working on it right now, but it's taking some time and I'm not too sure how to go about it, since I need to capture the feature used inside the code, and check it against a feature that would be declared inside the Cargo.toml file.

If someone here has a good idea on how I should go about doing that, please let me know. I've read the CONTRIBUTING.md and adding_lints.md, but I still have A LOT to learn ^^'

flip1995

flip1995 commented on May 29, 2020

@flip1995
Member

You can look at this PR, which implements pretty much, what you're asking for: #5643

Sadly, it is not clear, how to proceed with pre-expansion lints (which means, before code get's removed due to features).

flip1995

flip1995 commented on May 29, 2020

@flip1995
Member

Duplicate of #1614

flip1995

flip1995 commented on May 29, 2020

@flip1995
Member

Thanks for wanting to contribute though! I just don't want to let your work go to waste. If you want to get familiar with hacking on Clippy, you can look at the good first issues or if you want to add a new lint, you can look at the L-lint label.

pscott

pscott commented on May 29, 2020

@pscott
Author

Thanks for wanting to contribute though! I just don't want to let your work go to waste. If you want to get familiar with hacking on Clippy, you can look at the good first issues or if you want to add a new lint, you can look at the L-lint label.

Yup, all good! I had try to look up in the issues but didn't find what I was looking for. In the meantime, I'm going to write a small program that does just that :) Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @flip1995@pscott

        Issue actions

          Lint suggestion: undeclared_feature · Issue #5660 · rust-lang/rust-clippy