|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::sugg::Sugg; |
| 3 | +use rustc_ast::BorrowKind; |
| 4 | +use rustc_errors::{Applicability, Diag}; |
| 5 | +use rustc_hir::{Expr, ExprKind, Node, QPath}; |
| 6 | +use rustc_lint::LateContext; |
| 7 | +use rustc_span::sym; |
| 8 | + |
| 9 | +use super::SWAP_WITH_TEMPORARY; |
| 10 | + |
| 11 | +const MSG_TEMPORARY: &str = "this expression returns a temporary value"; |
| 12 | +const MSG_TEMPORARY_REFMUT: &str = "this is a mutable reference to a temporary value"; |
| 13 | + |
| 14 | +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, func: &Expr<'_>, args: &[Expr<'_>]) { |
| 15 | + if let ExprKind::Path(QPath::Resolved(_, func_path)) = func.kind |
| 16 | + && let Some(func_def_id) = func_path.res.opt_def_id() |
| 17 | + && cx.tcx.is_diagnostic_item(sym::mem_swap, func_def_id) |
| 18 | + { |
| 19 | + match (ArgKind::new(&args[0]), ArgKind::new(&args[1])) { |
| 20 | + (ArgKind::RefMutToTemp(left_temp), ArgKind::RefMutToTemp(right_temp)) => { |
| 21 | + emit_lint_useless(cx, expr, &args[0], &args[1], left_temp, right_temp); |
| 22 | + }, |
| 23 | + (ArgKind::RefMutToTemp(left_temp), right) => emit_lint_assign(cx, expr, &right, &args[0], left_temp), |
| 24 | + (left, ArgKind::RefMutToTemp(right_temp)) => emit_lint_assign(cx, expr, &left, &args[1], right_temp), |
| 25 | + _ => {}, |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +enum ArgKind<'tcx> { |
| 31 | + // Mutable reference to a place, coming from a macro |
| 32 | + RefMutToPlaceAsMacro(&'tcx Expr<'tcx>), |
| 33 | + // Place behind a mutable reference |
| 34 | + RefMutToPlace(&'tcx Expr<'tcx>), |
| 35 | + // Temporary value behind a mutable reference |
| 36 | + RefMutToTemp(&'tcx Expr<'tcx>), |
| 37 | + // Any other case |
| 38 | + Expr(&'tcx Expr<'tcx>), |
| 39 | +} |
| 40 | + |
| 41 | +impl<'tcx> ArgKind<'tcx> { |
| 42 | + fn new(arg: &'tcx Expr<'tcx>) -> Self { |
| 43 | + if let ExprKind::AddrOf(BorrowKind::Ref, _, target) = arg.kind { |
| 44 | + if target.is_syntactic_place_expr() { |
| 45 | + if arg.span.from_expansion() { |
| 46 | + ArgKind::RefMutToPlaceAsMacro(arg) |
| 47 | + } else { |
| 48 | + ArgKind::RefMutToPlace(target) |
| 49 | + } |
| 50 | + } else { |
| 51 | + ArgKind::RefMutToTemp(target) |
| 52 | + } |
| 53 | + } else { |
| 54 | + ArgKind::Expr(arg) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Emits a note either on the temporary expression if it can be found in the same context as the |
| 60 | +// base and returns `true`, or on the mutable reference to the temporary expression otherwise and |
| 61 | +// returns `false`. |
| 62 | +fn emit_note(diag: &mut Diag<'_, ()>, base: &Expr<'_>, expr: &Expr<'_>, expr_temp: &Expr<'_>) -> bool { |
| 63 | + if base.span.eq_ctxt(expr.span) { |
| 64 | + diag.span_note(expr_temp.span.source_callsite(), MSG_TEMPORARY); |
| 65 | + true |
| 66 | + } else { |
| 67 | + diag.span_note(expr.span.source_callsite(), MSG_TEMPORARY_REFMUT); |
| 68 | + false |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +fn emit_lint_useless( |
| 73 | + cx: &LateContext<'_>, |
| 74 | + expr: &Expr<'_>, |
| 75 | + left: &Expr<'_>, |
| 76 | + right: &Expr<'_>, |
| 77 | + left_temp: &Expr<'_>, |
| 78 | + right_temp: &Expr<'_>, |
| 79 | +) { |
| 80 | + span_lint_and_then( |
| 81 | + cx, |
| 82 | + SWAP_WITH_TEMPORARY, |
| 83 | + expr.span, |
| 84 | + "swapping temporary values has no effect", |
| 85 | + |diag| { |
| 86 | + emit_note(diag, expr, left, left_temp); |
| 87 | + emit_note(diag, expr, right, right_temp); |
| 88 | + }, |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +fn emit_lint_assign(cx: &LateContext<'_>, expr: &Expr<'_>, target: &ArgKind<'_>, reftemp: &Expr<'_>, temp: &Expr<'_>) { |
| 93 | + span_lint_and_then( |
| 94 | + cx, |
| 95 | + SWAP_WITH_TEMPORARY, |
| 96 | + expr.span, |
| 97 | + "swapping with a temporary value is inefficient", |
| 98 | + |diag| { |
| 99 | + if !emit_note(diag, expr, reftemp, temp) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + // Make the suggestion only when the original `swap()` call is a statement |
| 104 | + // or the last expression in a block. |
| 105 | + if matches!(cx.tcx.parent_hir_node(expr.hir_id), Node::Stmt(..) | Node::Block(..)) { |
| 106 | + let mut applicability = Applicability::MachineApplicable; |
| 107 | + let ctxt = expr.span.ctxt(); |
| 108 | + let assign_target = match target { |
| 109 | + ArgKind::Expr(target) | ArgKind::RefMutToPlaceAsMacro(target) => { |
| 110 | + Sugg::hir_with_context(cx, target, ctxt, "_", &mut applicability).deref() |
| 111 | + }, |
| 112 | + ArgKind::RefMutToPlace(target) => Sugg::hir_with_context(cx, target, ctxt, "_", &mut applicability), |
| 113 | + ArgKind::RefMutToTemp(_) => unreachable!(), |
| 114 | + }; |
| 115 | + let assign_source = Sugg::hir_with_context(cx, temp, ctxt, "_", &mut applicability); |
| 116 | + diag.span_suggestion( |
| 117 | + expr.span, |
| 118 | + "use assignment instead", |
| 119 | + format!("{assign_target} = {assign_source}"), |
| 120 | + applicability, |
| 121 | + ); |
| 122 | + } |
| 123 | + }, |
| 124 | + ); |
| 125 | +} |
0 commit comments