TimonPost / cargo-unused-features

Find potential unused enabled feature flags and prune them.
MIT License
230 stars 10 forks source link

Running on binary project gives wrong results [fixed] #1

Closed TimonPost closed 1 year ago

TimonPost commented 2 years ago

From reddit.

Probably not a minimal example, but this fails for me:

cargo new bug
cd ./bug/
cargo add async-std
echo "fn main() { let _ = async_std::task::yield_now(); }" > ./src/main.rs
unused-features analyze
unused-features prune --input .\report.json
cargo check
TimonPost commented 2 years ago

I found the bug, it has to do with features depending on other features. async-std contains the 'std' feature, and the std feature depends on the 'alloc' feature. In my current implementation, I do a look-up into all dependencies of 'std' and add them to the list to remove one by one. Also, I add the std feature itself. But 'std' and 'alloc' are present in the list and will both activate the task namespace. I can remove 'alloc' and see if it compiles and it will since 'std' is still present. However, in fact, we can not remove it as alloc is a required dependency.

TimonPost commented 2 years ago

So somehow I will have to consider original features and their dependent features separately. One way to fix it is to not collect all sub-enabled features and just keep it at the root level. Then removing "std" will certainly break the compilation as it will disable all sub-features. But there might be sub-features of "std" that could be enabled individually rather than the whole set of sub-features. So we would lose some granularity/detail.

TimonPost commented 2 years ago

If you're running this library on a binary rather than a library, please make sure to supply '--bin' flag. This might actually solve the issue already. Default the project only compilers libraries.

TimonPost commented 2 years ago

Flag is made default enabled in https://crates.io/crates/cargo-unused-features/0.1.7