Skip to content

Conversation

ada4a
Copy link
Contributor

@ada4a ada4a commented Sep 23, 2025

Based on #15520 (comment), with the following changes/additions:

  • No longer use the Trait enum for quickly filtering out irrelevant impls -- instead, check the trait_of basically right away:
    1. pre-fetch DefIds of the relevant traits into the lint pass
    2. reuse cx.tcx.impl_trait_ref's output for the the DefId of the trait being implemented
    3. compare those DefIds, which should be very cheap
  • Next, check whether self_ty implements the other relevant trait.
  • Pre-filter impl items in the same (lazy) iterator, but delay the proc-macro check as much as possible

changelog: none

Not auto-assigning since @blyxyas and/or @Jarcho will be the ones reviewing it:
r? ghost

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 23, 2025
@ada4a
Copy link
Contributor Author

ada4a commented Sep 23, 2025

Depends on #15520, so @rustbot label S-blocked

@rustbot rustbot added the S-blocked Status: marked as blocked ❌ on something else such as an RFC or other implementation work label Sep 23, 2025
Copy link

github-actions bot commented Sep 23, 2025

No changes for e776e42

@ada4a ada4a force-pushed the non_canonical_impls-2 branch 3 times, most recently from 7b7a795 to 9718c47 Compare September 23, 2025 22:32
@ada4a
Copy link
Contributor Author

ada4a commented Sep 24, 2025

The PR was merged, so @rustbot label -S-blocked

@rustbot rustbot removed the S-blocked Status: marked as blocked ❌ on something else such as an RFC or other implementation work label Sep 24, 2025
@ada4a ada4a force-pushed the non_canonical_impls-2 branch from 9718c47 to 948e23d Compare September 24, 2025 14:23
@rustbot

This comment has been minimized.

@ada4a ada4a force-pushed the non_canonical_impls-2 branch from 948e23d to 2168603 Compare September 24, 2025 15:18
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.

Not a change you made, but can you move the trait impl type argument check for PartialOrd to be before walking the assoc items. If the left and right types don't match then there is no Ord impl to call.

View changes since this review

&& let Node::Item(item) = cx.tcx.parent_hir_node(impl_item.hir_id())
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
if let ItemKind::Impl(impl_) = item.kind
&& (1..=2).contains(&impl_.items.len())
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs at least since PartialOrd can have five function.

You'll also want to figure if the trait is one we need to check here before doing anything else. Get the DefId from the path to avoid doing a query.

.iter()
.map(|id| cx.tcx.hir_impl_item(*id))
.filter_map(|assoc| {
if let ImplItemKind::Fn(_, assoc_id) = assoc.kind
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: body_id not assoc_id.

for (assoc, _, block) in assoc_fns {
// NOTE: we don't bother doing the method name check before the proc-macro check, because we
// lint both methods of `Clone`, and therefore would need to repeat the proc-macro check anyway
if !is_from_proc_macro(cx, assoc) {
Copy link
Contributor

Choose a reason for hiding this comment

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

is_from_proc_macro should be moved right before emitting the lint. It's very unlikely to actually filter anything.

for (assoc, body, block) in assoc_fns {
// NOTE: `PartialOrd` has methods `lt/le/ge/gt` which we don't lint, so
// don't bother checking whether _they_ come from a proc-macro
if assoc.ident.name == sym::partial_cmp && !is_from_proc_macro(cx, assoc) {
Copy link
Contributor

Choose a reason for hiding this comment

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

is_from_proc_macro should be moved right before emitting the lint. It's very unlikely to actually filter anything.

@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 Oct 1, 2025
ada4a added 3 commits October 1, 2025 21:02
The missing external macro test for `non_canonical_clone_impl` was
caught thanks to lintcheck -- I went ahead and added all the other
combinations of the test while at it
@ada4a
Copy link
Contributor Author

ada4a commented Oct 1, 2025

Not a change you made, but can you move the trait impl type argument check for PartialOrd to be before walking the assoc items. If the left and right types don't match then there is no Ord impl to call.

Good idea. I'd like to keep that a separate commit though, if that's okay

@ada4a ada4a force-pushed the non_canonical_impls-2 branch from 2168603 to e776e42 Compare October 1, 2025 21:09
@rustbot
Copy link
Collaborator

rustbot commented Oct 1, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

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.

4 participants