fix: incompatibility of non_canonical_clone_impl and implicit_return#16949
Conversation
|
r? @llogiq rustbot has assigned @llogiq. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
@rustbot ready |
|
Reminder, once the PR becomes ready for a review, use |
|
I don't think |
@samueltardieu But, you know, checking whether there's an explicit return is not the job of |
Agreed. But adding a @rustbot concern Using |
|
@samueltardieu I think your idea is better. So I add a special check for |
|
@rustbot ready |
|
Lintcheck changes for 0d1e095
This comment will be updated if you push new changes |
There was a problem hiding this comment.
Can you please squash the commits?
@rustbot concern resolve Using return *self is not the canonical implementation of Clone
|
@samueltardieu Resolved |
|
@rustbot ready |
f431710 to
ea42ff4
Compare
|
@samueltardieu Commits squashed |
| 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, | ||
| ); |
There was a problem hiding this comment.
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 thereturnis legitimate there - when a non-canonical impl is found, to determine whether we want to suggest
*selforreturn *self;.
This way, the lookup won't take place when a regular *self canonical implementation is found (fast path).
| 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, | |
| ); |
|
@rustbot ready @samueltardieu Suggestion applied |
|
Did you forget to push? |
|
@samueltardieu Sorry. |
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.
d9cff52 to
0d1e095
Compare
|
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. |
|
@rustbot ready @samueltardieu |
|
@samueltardieu thanks for your patience. this is my first pr merged into clippy's main repo :) |
View all comments
changelog: fix [
non_canonical_clone_impl] incompatibility with [implicit_return]Fixes #16945
Note
Concerns (0 active)
Usingresolved in this commentreturn *selfis not the canonical implementation ofCloneManaged by
@rustbot—see help for details.