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, differing_macro_contexts, eq_expr_value};
+use clippy_utils::{can_mut_borrow_both, differing_macro_contexts, eq_expr_value, std_or_core};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
/// let mut b = 2;
/// std::mem::swap(&mut a, &mut b);
/// ```
+ #[clippy::version = "pre 1.29.0"]
pub MANUAL_SWAP,
complexity,
"manual swap of two variables"
/// # let mut b = 2;
/// std::mem::swap(&mut a, &mut b);
/// ```
+ #[clippy::version = "pre 1.29.0"]
pub ALMOST_SWAPPED,
correctness,
"`foo = bar; bar = foo` sequence"
if matches!(ty.kind(), ty::Slice(_))
|| matches!(ty.kind(), ty::Array(_, _))
- || is_type_diagnostic_item(cx, ty, sym::vec_type)
- || is_type_diagnostic_item(cx, ty, sym::vecdeque_type)
+ || is_type_diagnostic_item(cx, ty, sym::Vec)
+ || is_type_diagnostic_item(cx, ty, sym::VecDeque)
{
let slice = Sugg::hir_with_applicability(cx, lhs1, "<slice>", &mut applicability);
span_lint_and_sugg(
let first = Sugg::hir_with_applicability(cx, e1, "..", &mut applicability);
let second = Sugg::hir_with_applicability(cx, e2, "..", &mut applicability);
+ let Some(sugg) = std_or_core(cx) else { return };
+
span_lint_and_then(
cx,
MANUAL_SWAP,
diag.span_suggestion(
span,
"try",
- format!("std::mem::swap({}, {})", first.mut_addr(), second.mut_addr()),
+ format!("{}::mem::swap({}, {})", sugg, first.mut_addr(), second.mut_addr()),
applicability,
);
if !is_xor_based {
- diag.note("or maybe you should use `std::mem::replace`?");
+ diag.note(&format!("or maybe you should use `{}::mem::replace`?", sugg));
}
},
);
};
let span = first.span.to(second.span);
+ let Some(sugg) = std_or_core(cx) else { return };
span_lint_and_then(cx,
- ALMOST_SWAPPED,
- span,
- &format!("this looks like you are trying to swap{}", what),
- |diag| {
- if !what.is_empty() {
- diag.span_suggestion(
- span,
- "try",
- format!(
- "std::mem::swap({}, {})",
- lhs,
- rhs,
- ),
- Applicability::MaybeIncorrect,
- );
- diag.note("or maybe you should use `std::mem::replace`?");
- }
- });
+ ALMOST_SWAPPED,
+ span,
+ &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,
+ ),
+ Applicability::MaybeIncorrect,
+ );
+ diag.note(
+ &format!("or maybe you should use `{}::mem::replace`?", sugg)
+ );
+ }
+ });
}
}
}
}
}
-/// Returns the lhs and rhs of an xor assignment statement.
+/// Returns the lhs and rhs of an xor assignment statement.
fn extract_sides_of_xor_assign<'a, 'hir>(stmt: &'a Stmt<'hir>) -> Option<(&'a Expr<'hir>, &'a Expr<'hir>)> {
if let StmtKind::Semi(expr) = stmt.kind {
if let ExprKind::AssignOp(