]> git.lizzy.rs Git - rust.git/blobdiff - src/tools/clippy/clippy_lints/src/swap.rs
Rollup merge of #106410 - clubby789:borrow-mut-self-mut-self-diag, r=compiler-errors
[rust.git] / src / tools / clippy / clippy_lints / src / swap.rs
index 1885f3ca414dfe9dbef2a600fc961cf4f6b9ebb5..17e9cc5f6b7c7a7d72a5fd73fed5f043c366708d 100644 (file)
@@ -2,7 +2,7 @@
 use clippy_utils::source::snippet_with_applicability;
 use clippy_utils::sugg::Sugg;
 use clippy_utils::ty::is_type_diagnostic_item;
-use clippy_utils::{can_mut_borrow_both, eq_expr_value, std_or_core};
+use clippy_utils::{can_mut_borrow_both, eq_expr_value, in_constant, std_or_core};
 use if_chain::if_chain;
 use rustc_errors::Applicability;
 use rustc_hir::{BinOpKind, Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
@@ -16,6 +16,8 @@
     /// ### What it does
     /// Checks for manual swapping.
     ///
+    /// Note that the lint will not be emitted in const blocks, as the suggestion would not be applicable.
+    ///
     /// ### Why is this bad?
     /// The `std::mem::swap` function exposes the intent better
     /// without deinitializing or copying either variable.
@@ -96,7 +98,7 @@ fn generate_swap_warning(cx: &LateContext<'_>, e1: &Expr<'_>, e2: &Expr<'_>, spa
                             cx,
                             MANUAL_SWAP,
                             span,
-                            &format!("this looks like you are swapping elements of `{}` manually", slice),
+                            &format!("this looks like you are swapping elements of `{slice}` manually"),
                             "try",
                             format!(
                                 "{}.swap({}, {})",
@@ -121,16 +123,16 @@ fn generate_swap_warning(cx: &LateContext<'_>, e1: &Expr<'_>, e2: &Expr<'_>, spa
         cx,
         MANUAL_SWAP,
         span,
-        &format!("this looks like you are swapping `{}` and `{}` manually", first, second),
+        &format!("this looks like you are swapping `{first}` and `{second}` manually"),
         |diag| {
             diag.span_suggestion(
                 span,
                 "try",
-                format!("{}::mem::swap({}, {})", sugg, first.mut_addr(), second.mut_addr()),
+                format!("{sugg}::mem::swap({}, {})", first.mut_addr(), second.mut_addr()),
                 applicability,
             );
             if !is_xor_based {
-                diag.note(&format!("or maybe you should use `{}::mem::replace`?", sugg));
+                diag.note(format!("or maybe you should use `{sugg}::mem::replace`?"));
             }
         },
     );
@@ -138,6 +140,10 @@ fn generate_swap_warning(cx: &LateContext<'_>, e1: &Expr<'_>, e2: &Expr<'_>, spa
 
 /// Implementation of the `MANUAL_SWAP` lint.
 fn check_manual_swap(cx: &LateContext<'_>, block: &Block<'_>) {
+    if in_constant(cx, block.hir_id) {
+        return;
+    }
+
     for w in block.stmts.windows(3) {
         if_chain! {
             // let t = foo();
@@ -182,7 +188,7 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
                 let rhs0 = Sugg::hir_opt(cx, rhs0);
                 let (what, lhs, rhs) = if let (Some(first), Some(second)) = (lhs0, rhs0) {
                     (
-                        format!(" `{}` and `{}`", first, second),
+                        format!(" `{first}` and `{second}`"),
                         first.mut_addr().to_string(),
                         second.mut_addr().to_string(),
                     )
@@ -196,22 +202,19 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
                 span_lint_and_then(cx,
                     ALMOST_SWAPPED,
                     span,
-                    &format!("this looks like you are trying to swap{}", what),
+                    &format!("this looks like you are trying to swap{what}"),
                     |diag| {
                         if !what.is_empty() {
                             diag.span_suggestion(
                                 span,
                                 "try",
                                 format!(
-                                    "{}::mem::swap({}, {})",
-                                    sugg,
-                                    lhs,
-                                    rhs,
+                                    "{sugg}::mem::swap({lhs}, {rhs})",
                                 ),
                                 Applicability::MaybeIncorrect,
                             );
                             diag.note(
-                                &format!("or maybe you should use `{}::mem::replace`?", sugg)
+                                format!("or maybe you should use `{sugg}::mem::replace`?")
                             );
                         }
                     });