Skip to content

fix: incompatibility of non_canonical_clone_impl and implicit_return#16949

Merged
samueltardieu merged 1 commit intorust-lang:masterfrom
Kokoro2336:fix/non-canonical-clone-impl
May 7, 2026
Merged

fix: incompatibility of non_canonical_clone_impl and implicit_return#16949
samueltardieu merged 1 commit intorust-lang:masterfrom
Kokoro2336:fix/non-canonical-clone-impl

Conversation

@Kokoro2336
Copy link
Copy Markdown
Contributor

@Kokoro2336 Kokoro2336 commented May 1, 2026

View all comments

changelog: fix [non_canonical_clone_impl] incompatibility with [implicit_return]

Fixes #16945

Note

Concerns (0 active)

Managed by @rustbot—see help for details.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 1, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 1, 2026

r? @llogiq

rustbot has assigned @llogiq.
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

@Kokoro2336
Copy link
Copy Markdown
Contributor Author

@rustbot ready

Comment thread clippy_lints/src/non_canonical_impls.rs Outdated
@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 May 3, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 3, 2026

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

@samueltardieu
Copy link
Copy Markdown
Member

I don't think non_canonical_clone_impl should stop linting unless implicit_return is also active at the point of the check.

@Kokoro2336
Copy link
Copy Markdown
Contributor Author

I don't think non_canonical_clone_impl should stop linting unless implicit_return is also active at the point of the check.

@samueltardieu But, you know, checking whether there's an explicit return is not the job of non_canonical_clone_impl. The existence of return should not affect the detection of non_canonical_clone_impl. And adding a special check of whether implicit_return is enabled would be dirtly.

@samueltardieu
Copy link
Copy Markdown
Member

@samueltardieu But, you know, checking whether there's an explicit return is not the job of non_canonical_clone_impl. The existence of return should not affect the detection of non_canonical_clone_impl. And adding a special check of whether implicit_return is enabled would be dirtly.

Agreed. But adding a return makes, at least for me, the implementation not canonical, so the lint should trigger. I was proposing adding a mechanism to support your particular use case.

@rustbot concern Using return *self is not the canonical implementation of Clone

@rustbot rustbot added the S-waiting-on-concerns Status: This PR/issue has concerns that need to be addressed before moving forward with it label May 4, 2026
@Kokoro2336
Copy link
Copy Markdown
Contributor Author

Kokoro2336 commented May 5, 2026

@samueltardieu I think your idea is better. So I add a special check for implicit_return. By the way, we can't peel the blocks, or a body with multiple layers of blocks might be still considered canonicalized, so I revert the logic of this part.

@Kokoro2336
Copy link
Copy Markdown
Contributor Author

@rustbot ready

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

github-actions Bot commented May 5, 2026

Lintcheck changes for 0d1e095

Lint Added Removed Changed
clippy::non_canonical_clone_impl 0 0 2

This comment will be updated if you push new changes

@Kokoro2336
Copy link
Copy Markdown
Contributor Author

r? @samueltardieu

@rustbot rustbot assigned samueltardieu and unassigned llogiq May 6, 2026
Copy link
Copy Markdown
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

Can you please squash the commits?

@rustbot concern resolve Using return *self is not the canonical implementation of Clone

View changes since this review

Comment thread clippy_lints/src/non_canonical_impls.rs Outdated
@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-concerns Status: This PR/issue has concerns that need to be addressed before moving forward with it S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 7, 2026
@Kokoro2336
Copy link
Copy Markdown
Contributor Author

@samueltardieu Resolved

@Kokoro2336
Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 7, 2026
@Kokoro2336 Kokoro2336 force-pushed the fix/non-canonical-clone-impl branch from f431710 to ea42ff4 Compare May 7, 2026 08:46
@Kokoro2336
Copy link
Copy Markdown
Contributor Author

@samueltardieu Commits squashed

Comment thread clippy_lints/src/non_canonical_impls.rs Outdated
Comment on lines 228 to 249
let implicit_return_lint_enabled = !is_lint_allowed(cx, IMPLICIT_RETURN, body_expr.hir_id);

match is_canonical_clone_body(body_expr) {
IsCanonical::WithoutReturn => return,
IsCanonical::WithReturn if implicit_return_lint_enabled => return,
IsCanonical::WithReturn | IsCanonical::No => {},
}

span_lint_and_sugg(
cx,
NON_CANONICAL_CLONE_IMPL,
block.span,
body_expr.span,
"non-canonical implementation of `clone` on a `Copy` type",
"change this to",
"{ *self }".to_owned(),
if implicit_return_lint_enabled {
"{ return *self; }"
} else {
"{ *self }"
}
.to_owned(),
Applicability::MaybeIncorrect,
);
Copy link
Copy Markdown
Member

@samueltardieu samueltardieu May 7, 2026

Choose a reason for hiding this comment

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

is_lint_allowed() does several lookups. I would prefer that it is invoked only when necessary, that is:

  • when a return *self; is found, to check if the return is legitimate there
  • when a non-canonical impl is found, to determine whether we want to suggest *self or return *self;.

This way, the lookup won't take place when a regular *self canonical implementation is found (fast path).

Suggested change
let implicit_return_lint_enabled = !is_lint_allowed(cx, IMPLICIT_RETURN, body_expr.hir_id);
match is_canonical_clone_body(body_expr) {
IsCanonical::WithoutReturn => return,
IsCanonical::WithReturn if implicit_return_lint_enabled => return,
IsCanonical::WithReturn | IsCanonical::No => {},
}
span_lint_and_sugg(
cx,
NON_CANONICAL_CLONE_IMPL,
block.span,
body_expr.span,
"non-canonical implementation of `clone` on a `Copy` type",
"change this to",
"{ *self }".to_owned(),
if implicit_return_lint_enabled {
"{ return *self; }"
} else {
"{ *self }"
}
.to_owned(),
Applicability::MaybeIncorrect,
);
let add_return = match is_canonical_clone_body(body_expr) {
IsCanonical::WithReturn if is_lint_allowed(cx, IMPLICIT_RETURN, body_expr.hir_id) => false,
IsCanonical::WithReturn | IsCanonical::WithoutReturn => return,
IsCanonical::No => !is_lint_allowed(cx, IMPLICIT_RETURN, body_expr.hir_id),
};
span_lint_and_sugg(
cx,
NON_CANONICAL_CLONE_IMPL,
body_expr.span,
"non-canonical implementation of `clone` on a `Copy` type",
"change this to",
if add_return { "{ return *self; }" } else { "{ *self }" }.to_owned(),
Applicability::MaybeIncorrect,
);

View changes since the review

@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 May 7, 2026
@Kokoro2336
Copy link
Copy Markdown
Contributor Author

@rustbot ready @samueltardieu Suggestion applied

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

Did you forget to push?

@Kokoro2336
Copy link
Copy Markdown
Contributor Author

@samueltardieu Sorry.

Copy link
Copy Markdown
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

Please squash the commits

View changes since this review

@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 May 7, 2026
fix: peeling of block.

test: supply corner case tests of implicit_return.

fix: pass body.value rather than block.expr.

fix: special checking for implicit_return.

fix: suggestion.

refactor: flatten the code structure.

refactor: remove lint submission in helper.

refactor: call is_lint_allowed when necessary.
@Kokoro2336 Kokoro2336 force-pushed the fix/non-canonical-clone-impl branch from d9cff52 to 0d1e095 Compare May 7, 2026 14:36
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 7, 2026

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.

@Kokoro2336
Copy link
Copy Markdown
Contributor Author

@rustbot ready @samueltardieu

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 7, 2026
@samueltardieu samueltardieu added this pull request to the merge queue May 7, 2026
@Kokoro2336
Copy link
Copy Markdown
Contributor Author

@samueltardieu thanks for your patience. this is my first pr merged into clippy's main repo :)

Merged via the queue into rust-lang:master with commit 840a6fa May 7, 2026
13 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 7, 2026
@Kokoro2336 Kokoro2336 deleted the fix/non-canonical-clone-impl branch May 7, 2026 16:00
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.

Denying non_canonical_clone_impl and implicit_return is incompatible

4 participants