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};
/// ### 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.
applicability,
);
if !is_xor_based {
- diag.note(&format!("or maybe you should use `{sugg}::mem::replace`?"));
+ diag.note(format!("or maybe you should use `{sugg}::mem::replace`?"));
}
},
);
/// 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();
Applicability::MaybeIncorrect,
);
diag.note(
- &format!("or maybe you should use `{sugg}::mem::replace`?")
+ format!("or maybe you should use `{sugg}::mem::replace`?")
);
}
});