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 4 times, most recently from befa2dc to b84eb55 Compare January 20, 2025 16:59
@samueltardieu
Copy link
Contributor Author

Rebased

@samueltardieu
Copy link
Contributor Author

Rebased.

@samueltardieu
Copy link
Contributor Author

Rebased

@samueltardieu
Copy link
Contributor Author

Rebased

Comment on lines 42 to 44
if target.span.from_expansion() {
ArgKind::FromExpansion
} else if target.is_syntactic_place_expr() {
Copy link
Member

Choose a reason for hiding this comment

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

Are there any cases where linting if the borrowed value is from an expansion would result in FPs? Not linting in this case prevents linting something like swap(&mut v, &mut vec![1]); which seems like it could be useful

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was worried that some macros may sometimes return a temporary value and some other times return a non-temporary value. But indeed, if I don't check this, many more cases get linted.

I'll do a test run where I remove the expansion check in a second commit just to show how it behaves, and to get lintcheck results.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, that went well, I've squashed the commits. This lint now supports expanded arguments as well, thanks for the idea.

/// ```
#[clippy::version = "1.86.0"]
pub SWAP_WITH_TEMPORARY,
perf,
Copy link
Member

Choose a reason for hiding this comment

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

This seems more like a complexity lint to me since it's just an unnecessarily complicated way to assign or drop. Does this actually make a difference in performance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably not, at least not a significant one. I'll switch it to complexity.

@samueltardieu samueltardieu force-pushed the push-nkqqkpnrplvv branch 5 times, most recently from 4e6fe44 to 3016c0b Compare February 21, 2025 22:32
@samueltardieu
Copy link
Contributor Author

Rebased

@samueltardieu
Copy link
Contributor Author

@y21 Anything else needs to be done?

@samueltardieu
Copy link
Contributor Author

Rebased

@samueltardieu
Copy link
Contributor Author

@y21 should I reassign?

@y21
Copy link
Member

y21 commented Mar 18, 2025

Sorry, I've been meaning to get to this. I'll try to look into this again in the next few days!

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