-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
base: master
Are you sure you want to change the base?
Conversation
befa2dc
to
b84eb55
Compare
b84eb55
to
57f3cfb
Compare
Rebased |
57f3cfb
to
19ddfe2
Compare
Rebased. |
19ddfe2
to
67d0f0e
Compare
Rebased |
67d0f0e
to
b6a3a75
Compare
Rebased |
if target.span.from_expansion() { | ||
ArgKind::FromExpansion | ||
} else if target.is_syntactic_place_expr() { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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_lints/src/methods/mod.rs
Outdated
/// ``` | ||
#[clippy::version = "1.86.0"] | ||
pub SWAP_WITH_TEMPORARY, | ||
perf, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
4e6fe44
to
3016c0b
Compare
Rebased |
@y21 Anything else needs to be done? |
3016c0b
to
76e6421
Compare
Rebased |
@y21 should I reassign? |
Sorry, I've been meaning to get to this. I'll try to look into this again in the next few days! |
This lint detects inefficient or useless
{std,core}::mem::swap()
calls such as:It also takes care of using a form appropriate for a
()
context ifswap()
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 lintClose #1968