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

Lint suggestion: undeclared_feature #5660

Closed
pscott opened this issue May 28, 2020 · 5 comments
Closed

Lint suggestion: undeclared_feature #5660

pscott opened this issue May 28, 2020 · 5 comments

Comments

@pscott
Copy link

pscott commented May 28, 2020

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?

@pscott
Copy link
Author

pscott commented May 28, 2020

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
Copy link
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
Copy link
Member

Duplicate of #1614

@flip1995 flip1995 marked this as a duplicate of #1614 May 29, 2020
@flip1995
Copy link
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
Copy link
Author

pscott commented May 29, 2020

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants