+use crate::utils::sugg::Sugg;
+use crate::utils::{
+ differing_macro_contexts, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty, SpanlessEq,
+};
+use if_chain::if_chain;
use matches::matches;
use rustc::hir::*;
-use rustc::lint::*;
-use rustc::{declare_lint, lint_array};
-use if_chain::if_chain;
+use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::ty;
-use crate::utils::{differing_macro_contexts, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty, SpanlessEq};
-use crate::utils::sugg::Sugg;
+use rustc::{declare_tool_lint, lint_array};
+use rustc_errors::Applicability;
/// **What it does:** Checks for manual swapping.
///
/// b = a;
/// a = t;
/// ```
+/// Use std::mem::swap():
+/// ```rust
+/// std::mem::swap(&mut a, &mut b);
+/// ```
declare_clippy_lint! {
pub MANUAL_SWAP,
complexity,
for w in block.stmts.windows(3) {
if_chain! {
// let t = foo();
- if let StmtKind::Decl(ref tmp, _) = w[0].node;
- if let DeclKind::Local(ref tmp) = tmp.node;
+ if let StmtKind::Local(ref tmp) = w[0].node;
if let Some(ref tmp_init) = tmp.init;
if let PatKind::Binding(_, _, ident, None) = tmp.pat.node;
// foo() = bar();
- if let StmtKind::Semi(ref first, _) = w[1].node;
+ if let StmtKind::Semi(ref first) = w[1].node;
if let ExprKind::Assign(ref lhs1, ref rhs1) = first.node;
// bar() = t;
- if let StmtKind::Semi(ref second, _) = w[2].node;
+ if let StmtKind::Semi(ref second) = w[2].node;
if let ExprKind::Assign(ref lhs2, ref rhs2) = second.node;
if let ExprKind::Path(QPath::Resolved(None, ref rhs2)) = rhs2.node;
if rhs2.segments.len() == 1;
if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs1, lhs2) {
let ty = walk_ptrs_ty(cx.tables.expr_ty(lhs1));
- if matches!(ty.sty, ty::TySlice(_)) ||
- matches!(ty.sty, ty::TyArray(_, _)) ||
+ if matches!(ty.sty, ty::Slice(_)) ||
+ matches!(ty.sty, ty::Array(_, _)) ||
match_type(cx, ty, &paths::VEC) ||
match_type(cx, ty, &paths::VEC_DEQUE) {
return Some((lhs1, idx1, idx2));
snippet(cx, idx1.span, ".."),
snippet(cx, idx2.span, "..")))
} else {
- (false, "".to_owned(), "".to_owned())
+ (false, String::new(), String::new())
}
} else if let (Some(first), Some(second)) = (Sugg::hir_opt(cx, lhs1), Sugg::hir_opt(cx, rhs1)) {
(true, format!(" `{}` and `{}`", first, second),
format!("std::mem::swap({}, {})", first.mut_addr(), second.mut_addr()))
} else {
- (true, "".to_owned(), "".to_owned())
+ (true, String::new(), String::new())
};
let span = w[0].span.to(second.span);
&format!("this looks like you are swapping{} manually", what),
|db| {
if !sugg.is_empty() {
- db.span_suggestion(span, "try", sugg);
+ db.span_suggestion_with_applicability(
+ span,
+ "try",
+ sugg,
+ Applicability::Unspecified,
+ );
if replace {
db.note("or maybe you should use `std::mem::replace`?");
fn check_suspicious_swap(cx: &LateContext<'_, '_>, block: &Block) {
for w in block.stmts.windows(2) {
if_chain! {
- if let StmtKind::Semi(ref first, _) = w[0].node;
- if let StmtKind::Semi(ref second, _) = w[1].node;
+ if let StmtKind::Semi(ref first) = w[0].node;
+ if let StmtKind::Semi(ref second) = w[1].node;
if !differing_macro_contexts(first.span, second.span);
if let ExprKind::Assign(ref lhs0, ref rhs0) = first.node;
if let ExprKind::Assign(ref lhs1, ref rhs1) = second.node;
second.mut_addr().to_string(),
)
} else {
- ("".to_owned(), "".to_owned(), "".to_owned())
+ (String::new(), String::new(), String::new())
};
let span = first.span.to(second.span);
&format!("this looks like you are trying to swap{}", what),
|db| {
if !what.is_empty() {
- db.span_suggestion(span, "try",
- format!("std::mem::swap({}, {})", lhs, rhs));
+ db.span_suggestion_with_applicability(
+ span,
+ "try",
+ format!(
+ "std::mem::swap({}, {})",
+ lhs,
+ rhs,
+ ),
+ Applicability::MaybeIncorrect,
+ );
db.note("or maybe you should use `std::mem::replace`?");
}
});