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
add lint for recreation of an entire struct #12772
base: master
Are you sure you want to change the base?
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @y21 (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
Hi, is there a way to tell in a late pass whether one or more |
This might be better as an addition to the In particular, this lint also suffers from the same issues that that other lint does, which is that struct reinitialization only requires the fields to be |
Thanks for the pointer, I’ll look into folding it into
``unnecessary_struct_initialization``!
In particular, this lint also suffers from the same issues that
that other lint does, which is that struct reinitialization
only requires the fields to be `Copy`, whereas this lint's
suggestion forces a move of the whole struct, which doesn't
work if the struct is not `Copy`, and is also the reason why
that other lint is in `nursery` (#10547 is the issue).
Would it be sufficient to restrict the lint to ``Copy`` types?
|
I thought about this for a bit and yeah, I think restricting it to only Maybe what we could do to avoid false positives while still warning on some useful cases would be to not emit a warning if:
My reasoning is that if the struct is not However I think for the purposes of this PR it would be easier to just not worry about the possible false positives that had already existed and only extend the Fixing those false positives could be done separately and the lint is in the |
*However* I think for the purposes of this PR it would be
easier to just not worry about the possible false positives
that had already existed and only extend the
`unnecessary_struct_initialization` lint to also catch `Foo {
bar: foo.bar }` (in addition to what it already does).
I’ll take a stab at this.
Fixing those false positives could be done separately and the
lint is in the `nursery` category, so that's fine.
Understood, I’ll save that for another PR.
Thanks once more for the thorough response!
|
This lint makes Clippy warn about situations where an owned struct is essentially recreated by moving all its fields into a new instance of the struct. Clippy will suggest to simply use the original struct. The lint is not machine-applicable because the source struct may have been partially moved.
b8f8654
to
3119797
Compare
3119797
to
97c8880
Compare
This lint makes Clippy warn about situations where an owned struct is
essentially recreated by moving all its fields into a new instance of
the struct. The lint is not machine-applicable because the source
struct may have been partially moved.
This lint originated in something I spotted during peer review. While
working on their branch a colleague ended up with a commit where a
function returned a struct that 1:1 replicated one of its owned inputs
from its members. Initially I suspected they hadn’t run their code
through Clippy but AFAICS there is no lint for this situation yet.
changelog: new lint: [
redundant_owned_struct_recreation
]New lint checklist
.stderr
file)cargo test
passes locallycargo dev update_lints
cargo dev fmt