-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New lint: swap_with_temporary
#14046
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::sugg::Sugg; | ||
use rustc_ast::BorrowKind; | ||
use rustc_errors::{Applicability, Diag}; | ||
use rustc_hir::{Expr, ExprKind, Node, QPath}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::SWAP_WITH_TEMPORARY; | ||
|
||
const MSG_TEMPORARY: &str = "this expression returns a temporary value"; | ||
const MSG_TEMPORARY_REFMUT: &str = "this is a mutable reference to a temporary value"; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, func: &Expr<'_>, args: &[Expr<'_>]) { | ||
if let ExprKind::Path(QPath::Resolved(_, func_path)) = func.kind | ||
&& let Some(func_def_id) = func_path.res.opt_def_id() | ||
&& cx.tcx.is_diagnostic_item(sym::mem_swap, func_def_id) | ||
{ | ||
match (ArgKind::new(&args[0]), ArgKind::new(&args[1])) { | ||
(ArgKind::RefMutToTemp(left_temp), ArgKind::RefMutToTemp(right_temp)) => { | ||
emit_lint_useless(cx, expr, &args[0], &args[1], left_temp, right_temp); | ||
}, | ||
(ArgKind::RefMutToTemp(left_temp), right) => emit_lint_assign(cx, expr, &right, &args[0], left_temp), | ||
(left, ArgKind::RefMutToTemp(right_temp)) => emit_lint_assign(cx, expr, &left, &args[1], right_temp), | ||
_ => {}, | ||
} | ||
} | ||
} | ||
|
||
enum ArgKind<'tcx> { | ||
// Mutable reference to a place, coming from a macro | ||
RefMutToPlaceAsMacro(&'tcx Expr<'tcx>), | ||
// Place behind a mutable reference | ||
RefMutToPlace(&'tcx Expr<'tcx>), | ||
// Temporary value behind a mutable reference | ||
RefMutToTemp(&'tcx Expr<'tcx>), | ||
// Any other case | ||
Expr(&'tcx Expr<'tcx>), | ||
} | ||
|
||
impl<'tcx> ArgKind<'tcx> { | ||
fn new(arg: &'tcx Expr<'tcx>) -> Self { | ||
if let ExprKind::AddrOf(BorrowKind::Ref, _, target) = arg.kind { | ||
if target.is_syntactic_place_expr() { | ||
if arg.span.from_expansion() { | ||
ArgKind::RefMutToPlaceAsMacro(arg) | ||
} else { | ||
ArgKind::RefMutToPlace(target) | ||
} | ||
} else { | ||
ArgKind::RefMutToTemp(target) | ||
} | ||
} else { | ||
ArgKind::Expr(arg) | ||
} | ||
} | ||
} | ||
|
||
// Emits a note either on the temporary expression if it can be found in the same context as the | ||
// base and returns `true`, or on the mutable reference to the temporary expression otherwise and | ||
// returns `false`. | ||
fn emit_note(diag: &mut Diag<'_, ()>, base: &Expr<'_>, expr: &Expr<'_>, expr_temp: &Expr<'_>) -> bool { | ||
if base.span.eq_ctxt(expr.span) { | ||
diag.span_note(expr_temp.span.source_callsite(), MSG_TEMPORARY); | ||
true | ||
} else { | ||
diag.span_note(expr.span.source_callsite(), MSG_TEMPORARY_REFMUT); | ||
false | ||
} | ||
} | ||
|
||
fn emit_lint_useless( | ||
cx: &LateContext<'_>, | ||
expr: &Expr<'_>, | ||
left: &Expr<'_>, | ||
right: &Expr<'_>, | ||
left_temp: &Expr<'_>, | ||
right_temp: &Expr<'_>, | ||
) { | ||
span_lint_and_then( | ||
cx, | ||
SWAP_WITH_TEMPORARY, | ||
expr.span, | ||
"swapping temporary values has no effect", | ||
|diag| { | ||
emit_note(diag, expr, left, left_temp); | ||
emit_note(diag, expr, right, right_temp); | ||
}, | ||
); | ||
} | ||
|
||
fn emit_lint_assign(cx: &LateContext<'_>, expr: &Expr<'_>, target: &ArgKind<'_>, reftemp: &Expr<'_>, temp: &Expr<'_>) { | ||
span_lint_and_then( | ||
cx, | ||
SWAP_WITH_TEMPORARY, | ||
expr.span, | ||
"swapping with a temporary value is inefficient", | ||
|diag| { | ||
if !emit_note(diag, expr, reftemp, temp) { | ||
return; | ||
} | ||
|
||
// Make the suggestion only when the original `swap()` call is a statement | ||
// or the last expression in a block. | ||
if matches!(cx.tcx.parent_hir_node(expr.hir_id), Node::Stmt(..) | Node::Block(..)) { | ||
let mut applicability = Applicability::MachineApplicable; | ||
let ctxt = expr.span.ctxt(); | ||
let assign_target = match target { | ||
ArgKind::Expr(target) | ArgKind::RefMutToPlaceAsMacro(target) => { | ||
Sugg::hir_with_context(cx, target, ctxt, "_", &mut applicability).deref() | ||
}, | ||
ArgKind::RefMutToPlace(target) => Sugg::hir_with_context(cx, target, ctxt, "_", &mut applicability), | ||
ArgKind::RefMutToTemp(_) => unreachable!(), | ||
}; | ||
let assign_source = Sugg::hir_with_context(cx, temp, ctxt, "_", &mut applicability); | ||
diag.span_suggestion( | ||
expr.span, | ||
"use assignment instead", | ||
format!("{assign_target} = {assign_source}"), | ||
applicability, | ||
); | ||
} | ||
}, | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#![warn(clippy::swap_with_temporary)] | ||
|
||
use std::mem::swap; | ||
|
||
fn func() -> String { | ||
String::from("func") | ||
} | ||
|
||
fn func_returning_refmut(s: &mut String) -> &mut String { | ||
s | ||
} | ||
|
||
fn main() { | ||
let mut x = String::from("x"); | ||
let mut y = String::from("y"); | ||
let mut zz = String::from("zz"); | ||
let z = &mut zz; | ||
|
||
// No lint | ||
swap(&mut x, &mut y); | ||
|
||
y = func(); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
|
||
x = func(); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
|
||
*z = func(); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
|
||
// No lint | ||
swap(z, func_returning_refmut(&mut x)); | ||
|
||
swap(&mut y, z); | ||
|
||
*z = func(); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
|
||
macro_rules! mac { | ||
(refmut $x:expr) => { | ||
&mut $x | ||
}; | ||
(funcall $f:ident) => { | ||
$f() | ||
}; | ||
(wholeexpr) => { | ||
swap(&mut 42, &mut 0) | ||
}; | ||
(ident $v:ident) => { | ||
$v | ||
}; | ||
} | ||
*z = mac!(funcall func); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
*mac!(ident z) = mac!(funcall func); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
*mac!(ident z) = mac!(funcall func); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
*mac!(refmut y) = func(); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
|
||
// No lint if it comes from a macro as it may depend on the arguments | ||
mac!(wholeexpr); | ||
} | ||
|
||
struct S { | ||
t: String, | ||
} | ||
|
||
fn dont_lint_those(s: &mut S, v: &mut [String], w: Option<&mut String>) { | ||
swap(&mut s.t, &mut v[0]); | ||
swap(&mut s.t, v.get_mut(0).unwrap()); | ||
swap(w.unwrap(), &mut s.t); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#![warn(clippy::swap_with_temporary)] | ||
|
||
use std::mem::swap; | ||
|
||
fn func() -> String { | ||
String::from("func") | ||
} | ||
|
||
fn func_returning_refmut(s: &mut String) -> &mut String { | ||
s | ||
} | ||
|
||
fn main() { | ||
let mut x = String::from("x"); | ||
let mut y = String::from("y"); | ||
let mut zz = String::from("zz"); | ||
let z = &mut zz; | ||
|
||
// No lint | ||
swap(&mut x, &mut y); | ||
|
||
swap(&mut func(), &mut y); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
|
||
swap(&mut x, &mut func()); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
|
||
swap(z, &mut func()); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
|
||
// No lint | ||
swap(z, func_returning_refmut(&mut x)); | ||
|
||
swap(&mut y, z); | ||
|
||
swap(&mut func(), z); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
|
||
macro_rules! mac { | ||
(refmut $x:expr) => { | ||
&mut $x | ||
}; | ||
(funcall $f:ident) => { | ||
$f() | ||
}; | ||
(wholeexpr) => { | ||
swap(&mut 42, &mut 0) | ||
}; | ||
(ident $v:ident) => { | ||
$v | ||
}; | ||
} | ||
swap(&mut mac!(funcall func), z); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
Comment on lines
+53
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add a test where both swap arguments are macro calls, i.e. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
swap(&mut mac!(funcall func), mac!(ident z)); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
swap(mac!(ident z), &mut mac!(funcall func)); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
swap(mac!(refmut y), &mut func()); | ||
//~^ ERROR: swapping with a temporary value is inefficient | ||
|
||
// No lint if it comes from a macro as it may depend on the arguments | ||
mac!(wholeexpr); | ||
} | ||
|
||
struct S { | ||
t: String, | ||
} | ||
|
||
fn dont_lint_those(s: &mut S, v: &mut [String], w: Option<&mut String>) { | ||
swap(&mut s.t, &mut v[0]); | ||
swap(&mut s.t, v.get_mut(0).unwrap()); | ||
swap(w.unwrap(), &mut s.t); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 shouldn't be an issue with the current implementation but I think it would be good to have some other projection test cases that create places to future-proof where it shouldn't lint, like indexing (
swap(&mut slice1[0], &mut slice2[0])
) and field access (swap(&mut s1.x, &mut s2.x)
).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.
Done.