Skip to content

mem::replace -> mem::take lint is too aggressive  #5586

Closed

Description

@benesch

Clippy will suggest replacing

let x = mem::replace(&mut thing, Thing::default());

with:

let x = mem::take(&mut thing);

This is great! But it will also suggest replacing

mem::replace(&mut thing, Thing::default());

with:

mem::take(&mut thing);

This is not great. The construction using mem::replace in the second case is much clearer IMO as it indicates that the function is being executed for its side effects. The construction using mem::take looks like a mistake.

Would you folks be open to making the lint not apply in the second case?

Activity

  1. flip1995 commented on May 11, 2020

    @flip1995
    Member

    So basically only suggest using mem::take when the replaced thing is not assigned to anything?

  2. added
    S-needs-discussionStatus: Needs further discussion before merging or work can be started
    on May 11, 2020
  3. benesch commented on May 11, 2020

    @benesch
    Author

    Exactly. Arguably clippy should suggest *thing = Thing::default() in the latter case.

  4. Luro02 commented on May 12, 2020

    @Luro02

    This is great! But it will also suggest replacing

    mem::replace(&mut thing, Thing::default());

    with:

    mem::take(&mut thing);

    It might be useful to have a clippy lint that suggests replacing the latter with the former, because like you said the latter looks like a mistake (it is harder to read, because there is an implicit replacement of the field with default that is not obvious by the method name).

  5. taiki-e commented on May 12, 2020

    @taiki-e
    Member
    mem::replace(&mut thing, Thing::default());

    Given that mem::replace is #[must_use] (rust-lang/rust#71256), the usage that ignores mem::replace's return value is probably not the recommended way.

  6. benesch commented on May 12, 2020

    @benesch
    Author

    Yeah, looks like the upstream recommended way is just to assign:

    *thing = Thing::default()
    
  7. camsteffen commented on May 28, 2020

    @camsteffen
    Contributor

    Since mem::replace was given #[must_use], perhaps the mem_replace_with_default lint should simply ignore cases where the result isn't used. The #[must_use] lint will provide the correct suggestion.

  8. added
    good first issueThese issues are a good way to get started with Clippy
    C-bugCategory: Clippy is not doing the correct thing
    I-false-positiveIssue: The lint was triggered on code it shouldn't have
    on Jan 26, 2021
  9. GabrielBFern commented on Feb 11, 2024

    @GabrielBFern
    Contributor

    @rustbot claim

  10. added a commit that references this issue on Feb 13, 2024
    3e3a09e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveS-needs-discussionStatus: Needs further discussion before merging or work can be startedgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions