Skip to content
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

New lint: swap_with_temporary #14046

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

samueltardieu
Copy link
Contributor

@samueltardieu samueltardieu commented Jan 20, 2025

This lint detects inefficient or useless {std,core}::mem::swap() calls such as:

    // Should be `a = temp();`
    swap(&mut a, &mut temp());
    // Should be `*b = temp();`
    swap(b, &mut temp());
    // Should be `temp1(); temp2();` if we want to keep the side effects
    swap(&mut temp1(), &mut temp2());

It also takes care of using a form appropriate for a () context if swap() is part of a larger expression (don't ask me why this wouldn't happen, I have no idea), by suggesting { x = y; } (statement in block) or {std,core}::mem::drop((temp1(), temp2()).

changelog: [swap_with_temporary]: new lint

Close #1968

@rustbot
Copy link
Collaborator

rustbot commented Jan 20, 2025

r? @y21

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

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 20, 2025
@samueltardieu samueltardieu force-pushed the push-nkqqkpnrplvv branch 3 times, most recently from c1d39af to befa2dc Compare January 20, 2025 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint calling mem::swap on a reference
3 participants