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

Fix false positive for multi dep single use statements #120

Merged

Conversation

Elrendio
Copy link
Contributor

@Elrendio Elrendio commented Apr 15, 2024

Resolves #91

This also resolves some false detections of not_my_dep::my_dep_name::stuff_in_my_dep 😊 I couldn't find a way to avoid detection of futures in with the single line regexp

pub use {
    async_trait::{mod1, dep2},
    not_futures::{
        futures::{futures_mod1, futures_mod2::{futures_mod21, futures_mod22}}
    },
    reqwest,
};

This update does come at a small performance downside (that might be resolved by not instantiating a regexp per dependency?). On our repo of ~500k lines of machete goes from ~45s user time to ~48s of user time.

Copy link
Owner

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Just want a bit more comments, please.

Comment on lines 80 to 81
// - `use (::)?(?i){name}(?-i)(::|;| as)`: matches `use foo;`, `use foo::bar`, `use foo as
// bar;`, with
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep the previous formatting here? It's weird to read on two lines.

Comment on lines 85 to 86
// - `extern crate (?i){name}(?-i)( |;)`: matches `extern crate foo`, or `extern crate foo as
// bar`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

src/search_unused.rs Show resolved Hide resolved
src/search_unused.rs Show resolved Hide resolved
// 2. Matches the crate's name with optional sub-modules
// 3. Matches modules after the usage of the crate's name
//
// In order to avoid false usage detection of `not_my_dep::my_dep` the regexp unsures that the
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// In order to avoid false usage detection of `not_my_dep::my_dep` the regexp unsures that the
// In order to avoid false usage detection of `not_my_dep::my_dep` the regexp ensures that the

// engine doesn't support recursion). Therefore, sub modules are authorized up to 4 levels
// deep.

let sub_modules_match = r#"(?:::\w+)*(?:::\*|\s+as\s+\w+|::\{(?:[^{}]*(?:\{(?:[^{}]*(?:\{(?:[^{}]*(?:\{[^{}]*\})?[^{}]*)*\})?[^{}]*)*\})?[^{}]*)*\})?"#;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already a nice comment, but can I ask for a little bit more please: could you take each part of this regular expression, and tie it to the comment? (Which part is 1. in your list, 2. in your list, etc. and feel free to add more comment sections if necessary)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've linked the different parts 😊

@bnjbvr bnjbvr mentioned this pull request Jun 1, 2024
@Elrendio
Copy link
Contributor Author

Elrendio commented Sep 3, 2024

Hi @bnjbvr ,

Sorry for the delay, I missed your comments 😞 I've addressed them and added a .rustfmt.toml that seems to mostly fit with your expectations in order for other contributors to follow your lint rules automatically. If you're not aligned I can remove it.

Have a nice day!

@bnjbvr bnjbvr self-requested a review September 25, 2024 06:25
Copy link
Owner

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'm not a huge fan of the custom .rustfmt.toml, so I'll get rid of it.

Thanks a lot for the comments, they make the regular expression slightly easier to understand (what a monster have we created here, now wondering if using a custom Rust syntax parser written with nom would be easier…).

I've tried it out on one of my favorite Rust projects wasmtime:

  • wall clock time took a 18% slowdown hit, from 720ms -> 856ms
  • user time took a 35% slowdown hit, from 2.7s -> 3.67s

While these are impressive in terms of relative numbers, since the tool is still running in less than a second on such a large code base, I consider it fine, especially since it's correcter (and what's the point of being fast if code isn't correct).

Merging, thanks a bunch 🙏

@bnjbvr bnjbvr merged commit a767940 into bnjbvr:main Sep 25, 2024
4 checks passed
@Elrendio Elrendio deleted the FIX-FalsePositiveForMultiDepSingleUseStatements branch November 4, 2024 10:16
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

Successfully merging this pull request may close these issues.

False positives for multi-dep single use statements
2 participants