Closed
Description
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 commentedon 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 theCargo.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 commentedon May 29, 2020
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 commentedon May 29, 2020
Duplicate of #1614
flip1995 commentedon 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.pscott commentedon May 29, 2020
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!