4 use utils::{differing_macro_contexts, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty, SpanlessEq};
6 use syntax_pos::{Span, NO_EXPANSION};
8 /// **What it does:** Checks for manual swapping.
10 /// **Why is this bad?** The `std::mem::swap` function exposes the intent better
11 /// without deinitializing or copying either variable.
13 /// **Known problems:** None.
24 "manual swap of two variables"
27 /// **What it does:** Checks for `foo = bar; bar = foo` sequences.
29 /// **Why is this bad?** This looks like a failed attempt to swap.
31 /// **Known problems:** None.
41 "`foo = bar; bar = foo` sequence"
47 impl LintPass for Swap {
48 fn get_lints(&self) -> LintArray {
49 lint_array![MANUAL_SWAP, ALMOST_SWAPPED]
53 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Swap {
54 fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx Block) {
55 check_manual_swap(cx, block);
56 check_suspicious_swap(cx, block);
60 /// Implementation of the `MANUAL_SWAP` lint.
61 fn check_manual_swap(cx: &LateContext, block: &Block) {
62 for w in block.stmts.windows(3) {
65 let StmtDecl(ref tmp, _) = w[0].node,
66 let DeclLocal(ref tmp) = tmp.node,
67 let Some(ref tmp_init) = tmp.init,
68 let PatKind::Binding(_, _, ref tmp_name, None) = tmp.pat.node,
71 let StmtSemi(ref first, _) = w[1].node,
72 let ExprAssign(ref lhs1, ref rhs1) = first.node,
75 let StmtSemi(ref second, _) = w[2].node,
76 let ExprAssign(ref lhs2, ref rhs2) = second.node,
77 let ExprPath(QPath::Resolved(None, ref rhs2)) = rhs2.node,
78 rhs2.segments.len() == 1,
80 tmp_name.node.as_str() == rhs2.segments[0].name.as_str(),
81 SpanlessEq::new(cx).ignore_fn().eq_expr(tmp_init, lhs1),
82 SpanlessEq::new(cx).ignore_fn().eq_expr(rhs1, lhs2)
84 fn check_for_slice<'a>(
88 ) -> Option<(&'a Expr, &'a Expr, &'a Expr)> {
89 if let ExprIndex(ref lhs1, ref idx1) = lhs1.node {
90 if let ExprIndex(ref lhs2, ref idx2) = lhs2.node {
91 if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs1, lhs2) {
92 let ty = walk_ptrs_ty(cx.tables.expr_ty(lhs1));
94 if matches!(ty.sty, ty::TySlice(_)) ||
95 matches!(ty.sty, ty::TyArray(_, _)) ||
96 match_type(cx, ty, &paths::VEC) ||
97 match_type(cx, ty, &paths::VEC_DEQUE) {
98 return Some((lhs1, idx1, idx2));
107 let (replace, what, sugg) = if let Some((slice, idx1, idx2)) = check_for_slice(cx, lhs1, lhs2) {
108 if let Some(slice) = Sugg::hir_opt(cx, slice) {
110 format!(" elements of `{}`", slice),
111 format!("{}.swap({}, {})",
113 snippet(cx, idx1.span, ".."),
114 snippet(cx, idx2.span, "..")))
116 (false, "".to_owned(), "".to_owned())
118 } else if let (Some(first), Some(second)) = (Sugg::hir_opt(cx, lhs1), Sugg::hir_opt(cx, rhs1)) {
119 (true, format!(" `{}` and `{}`", first, second),
120 format!("std::mem::swap({}, {})", first.mut_addr(), second.mut_addr()))
122 (true, "".to_owned(), "".to_owned())
125 let span = Span { lo: w[0].span.lo, hi: second.span.hi, ctxt: NO_EXPANSION};
127 span_lint_and_then(cx,
130 &format!("this looks like you are swapping{} manually", what),
132 if !sugg.is_empty() {
133 db.span_suggestion(span, "try", sugg);
136 db.note("or maybe you should use `std::mem::replace`?");
144 /// Implementation of the `ALMOST_SWAPPED` lint.
145 fn check_suspicious_swap(cx: &LateContext, block: &Block) {
146 for w in block.stmts.windows(2) {
148 let StmtSemi(ref first, _) = w[0].node,
149 let StmtSemi(ref second, _) = w[1].node,
150 !differing_macro_contexts(first.span, second.span),
151 let ExprAssign(ref lhs0, ref rhs0) = first.node,
152 let ExprAssign(ref lhs1, ref rhs1) = second.node,
153 SpanlessEq::new(cx).ignore_fn().eq_expr(lhs0, rhs1),
154 SpanlessEq::new(cx).ignore_fn().eq_expr(lhs1, rhs0)
156 let lhs0 = Sugg::hir_opt(cx, lhs0);
157 let rhs0 = Sugg::hir_opt(cx, rhs0);
158 let (what, lhs, rhs) = if let (Some(first), Some(second)) = (lhs0, rhs0) {
159 (format!(" `{}` and `{}`", first, second), first.mut_addr().to_string(), second.mut_addr().to_string())
161 ("".to_owned(), "".to_owned(), "".to_owned())
164 let span = Span{ lo: first.span.lo, hi: second.span.hi, ctxt: NO_EXPANSION};
166 span_lint_and_then(cx,
169 &format!("this looks like you are trying to swap{}", what),
171 if !what.is_empty() {
172 db.span_suggestion(span, "try",
173 format!("std::mem::swap({}, {})", lhs, rhs));
174 db.note("or maybe you should use `std::mem::replace`?");