Skip to content

Allow [absolute_paths] to be configured based on occurrence#16679

Open
paulmialane wants to merge 1 commit intorust-lang:masterfrom
paulmialane:feat/#16574-add-absolute-paths-config
Open

Allow [absolute_paths] to be configured based on occurrence#16679
paulmialane wants to merge 1 commit intorust-lang:masterfrom
paulmialane:feat/#16574-add-absolute-paths-config

Conversation

@paulmialane
Copy link
Contributor

@paulmialane paulmialane commented Mar 7, 2026

fixes #16574

Add absolute-paths-max-occurrences config option to [absolute_paths]

Allow users to only trigger the lint when the same absolute path is used
more than N times in a single file, rather than on every occurrence. The
default value of 0 preserves the existing behavior of linting every
occurrence unconditionally.

Occurrences suppressed by #[allow] or #[expect] are excluded from
the count entirely, so they do not contribute toward the threshold.

I have two small problems :

  1. I have the following warning, I don't know how to proceed (allow it or better way to handle this ?)
warning: using `drain` can result in unstable query results
   --> clippy_lints/src/absolute_paths.rs:139:63
    |
139 |         for ((_def_id, _file), spans_vec) in self.occurrences.drain() {
    |                                                               ^^^^^
    |
    = note: if you believe this case to be fine, allow this lint and add a comment explaining your rationale
note: the lint level is defined here
   --> clippy_lints/src/lib.rs:26:5
    |
 26 |     rustc::internal
    |     ^^^^^^^^^^^^^^^
    = note: `#[warn(rustc::potential_query_instability)]` implied by `#[warn(rustc::internal)]`
  1. To test the behaviour "lints only if find N times in the file", I should probably add tests in tests/ui-cargo with a dedicated crate. My question is : should I move the other existing tests there ?

changelog: [absolute_paths]: Add absolute-paths-max-occurrences config option

Add `absolute-paths-max-occurrences` config option to `absolute_paths`

Allow users to only trigger the lint when the same absolute path is used
more than N times in a single file, rather than on every occurrence. The
default value of 0 preserves the existing behavior of linting every
occurrence unconditionally.

Occurrences suppressed by `#[allow]` or `#[expect]` are excluded from
the count entirely, so they do not contribute toward the threshold.

Related issue : rust-lang#16574
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 7, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 7, 2026

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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

@rustbot
Copy link
Collaborator

rustbot commented Mar 7, 2026

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

&& !self.absolute_paths_allowed_crates.contains(&crate_name)
&& !self.allowed_crates.contains(&crate_name)
&& !is_from_proc_macro(cx, path)
&& !fulfill_or_allowed(cx, ABSOLUTE_PATHS, [hir_id])
Copy link
Contributor

Choose a reason for hiding this comment

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

This should only run if max_occurrences > 0. This is slower to check than the allow and expect filtering that occurs in span_lint.

} else if let Res::Def(_, def_id) = path.res {
// Occurence based behaviour : accumulate spans and emit lint in check_crate_post
// if the occurence count is exceeded
let file = cx.tcx.sess.source_map().lookup_source_file(path.span.lo()).name.clone();
Copy link
Contributor

Choose a reason for hiding this comment

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

You should not be using the file, but the containing module. You can use hir_parent_owner_iter until you hit a module or the crate.

Comment on lines +139 to +149
for ((_def_id, _file), spans_vec) in self.occurrences.drain() {
if spans_vec.len() as u64 > self.max_occurrences {
for span in spans_vec {
span_lint(
cx,
ABSOLUTE_PATHS,
span,
"this absolute path is used too many times in this file, consider bringing it into scope with the `use` keyword",
);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't have a stable lint emission order. You need to collect and then sort the spans before you can emit them.

pub max_segments: u64,
pub allowed_crates: FxHashSet<Symbol>,
pub max_occurrences: u64,
occurrences: FxHashMap<(DefId, FileName), Vec<Span>>, // To track count of occurences
Copy link
Contributor

Choose a reason for hiding this comment

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

This would be better collected as Vec<(Key, Value)> . At the end you can the sort by key, split into chunks by key, and only emit based on each chunk's length.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Mar 9, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 9, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@Jarcho
Copy link
Contributor

Jarcho commented Mar 9, 2026

You'll need to remove the link to the issue from the commit message. They cause GitHub to spam that issue a fair bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow absolute_paths to be configured based on occurrence

3 participants