Skip to content

Replace str path utils with new PathLookup type #14705

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

Merged
merged 2 commits into from
May 5, 2025

Conversation

Alexendoo
Copy link
Member

The &[&str] path based clippy_utils have been removed and replace with a new type PathLookup:

Internally PathLookup is a lazy call to lookup_path (the new name for def_path_res to distinguish it from path_res)

The invalid_paths internal lint is removed, it could be reimplemented but it feels redundant since every path should be covered by a test anyway

User facing changes

  • manual_saturating_arithmetic now checks for u32::MAX/MIN instead of only detecting the legacy numeric consts (std::u32::MAX/MIN), clippy::legacy_numeric_constants will redirect usages of the legacy versions to the new one

  • allow-invalid = true now suppresses all invalid path warnings, currently you can run into a warning that can't be ignored in some situations, e.g. with serde without the derive feature

    warning: expected a macro, found a trait
     --> /home/gh-Alexendoo/temp/clippy.toml:2:5
      |
    2 |     { path = "serde::Serialize", allow-invalid = true },
      |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
  • Re-exports of primitives types like std::primitive::* no longer work in disallowed-types, this seems acceptable since it would be unusual to deny a primitive this way rather than writing e.g. usize. Type aliases such as c_char are unaffected

  • A similar slight performance improvement to Replace interning of string literals with preinterned symbols #14650

    $ hyperfine -w 2 -p 'touch src/cargo/lib.rs' 'cargo +master clippy' 'cargo +lazy-paths clippy'
    Benchmark 1: cargo +master clippy
      Time (mean ± σ):      6.829 s ±  0.064 s    [User: 6.079 s, System: 0.673 s]
      Range (min … max):    6.705 s …  6.907 s    10 runs
     
    Benchmark 2: cargo +lazy-paths clippy
      Time (mean ± σ):      6.765 s ±  0.064 s    [User: 5.984 s, System: 0.698 s]
      Range (min … max):    6.636 s …  6.834 s    10 runs
     
    Summary
      cargo +lazy-paths clippy ran
        1.01 ± 0.01 times faster than cargo +master clippy
    

changelog: none

@Alexendoo Alexendoo added the performance-project For issues and PRs related to the Clippy Performance Project label Apr 28, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 28, 2025

r? @Jarcho

rustbot has assigned @Jarcho.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 28, 2025
@rustbot

This comment has been minimized.

Copy link
Contributor

@Jarcho Jarcho left a comment

Choose a reason for hiding this comment

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

This also breaks the author lint. It didn't really work properly before with match_qpath, but it's even more broken now.

Comment on lines 646 to 678
let base = find_crates(tcx, base)
.iter()
.chain(find_primitive_impls(tcx, base))
.copied()
.collect();

def_path_res_with_base(tcx, crates, path)
lookup_path_with_base(tcx, base, path)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're changing things can you make lookup_path_with_base take a vec to write into.

@Alexendoo
Copy link
Member Author

  • The paths now have to specify the namespace they want to resolve in, this also allowed item_child_by_name to no longer allocate
  • Replaced PathLookup::first with PathLookup::only
  • Added some suggestions for paths to external definitions for clippy::author

Copy link
Contributor

@Jarcho Jarcho left a comment

Choose a reason for hiding this comment

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

Definitely a lot nicer than what we had before. Just one question, but this looks good either way.

Comment on lines 667 to 678
pub fn lookup_path(tcx: TyCtxt<'_>, ns: PathNS, path: &[Symbol]) -> Vec<DefId> {
let (root, rest) = match *path {
[] | [_] => return Vec::new(),
[root, ref rest @ ..] => (root, rest),
};

let crates = find_primitive_impls(tcx, base)
.chain(local_crate)
.map(|id| Res::Def(tcx.def_kind(id), id))
.chain(find_crates(tcx, base_sym))
.collect();

def_path_res_with_base(tcx, crates, path)
let mut out = Vec::new();
for &base in find_crates(tcx, root).iter().chain(find_primitive_impls(tcx, root)) {
lookup_path_with_base(tcx, base, ns, rest, &mut out);
}
out
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to move this into the paths module? The crate root is kind of a mess right now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah good plan, moved them across

@Jarcho Jarcho added this pull request to the merge queue May 5, 2025
Merged via the queue into rust-lang:master with commit c1586e1 May 5, 2025
11 checks passed
@Alexendoo Alexendoo deleted the lazy-paths branch May 6, 2025 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance-project For issues and PRs related to the Clippy Performance Project S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants