3 use syntax::codemap::mk_sp;
4 use utils::{differing_macro_contexts, snippet_opt, span_lint_and_then, SpanlessEq};
6 /// **What it does:** This lints manual swapping.
8 /// **Why is this bad?** The `std::mem::swap` function exposes the intent better without
9 /// deinitializing or copying either variable.
11 /// **Known problems:** None.
25 /// **What it does:** This lints `foo = bar; bar = foo` sequences.
27 /// **Why is this bad?** This looks like a failed attempt to swap.
29 /// **Known problems:** None.
39 "`foo = bar; bar = foo` sequence"
45 impl LintPass for Swap {
46 fn get_lints(&self) -> LintArray {
47 lint_array![MANUAL_SWAP, ALMOST_SWAPPED]
51 impl LateLintPass for Swap {
52 fn check_block(&mut self, cx: &LateContext, block: &Block) {
53 check_manual_swap(cx, block);
54 check_suspicious_swap(cx, block);
58 /// Implementation of the `MANUAL_SWAP` lint.
59 fn check_manual_swap(cx: &LateContext, block: &Block) {
60 for w in block.stmts.windows(3) {
63 let StmtDecl(ref tmp, _) = w[0].node,
64 let DeclLocal(ref tmp) = tmp.node,
65 let Some(ref tmp_init) = tmp.init,
66 let PatKind::Ident(_, ref tmp_name, None) = tmp.pat.node,
69 let StmtSemi(ref first, _) = w[1].node,
70 let ExprAssign(ref lhs1, ref rhs1) = first.node,
73 let StmtSemi(ref second, _) = w[2].node,
74 let ExprAssign(ref lhs2, ref rhs2) = second.node,
75 let ExprPath(None, ref rhs2) = rhs2.node,
76 rhs2.segments.len() == 1,
78 tmp_name.node.as_str() == rhs2.segments[0].name.as_str(),
79 SpanlessEq::new(cx).ignore_fn().eq_expr(tmp_init, lhs1),
80 SpanlessEq::new(cx).ignore_fn().eq_expr(rhs1, lhs2)
82 let (what, lhs, rhs) = if let (Some(first), Some(second)) = (snippet_opt(cx, lhs1.span), snippet_opt(cx, rhs1.span)) {
83 (format!(" `{}` and `{}`", first, second), first, second)
85 ("".to_owned(), "".to_owned(), "".to_owned())
88 let span = mk_sp(tmp.span.lo, second.span.hi);
90 span_lint_and_then(cx,
93 &format!("this looks like you are swapping{} manually", what),
96 db.span_suggestion(span, "try",
97 format!("std::mem::swap(&mut {}, &mut {})", lhs, rhs));
98 db.note("or maybe you should use `std::mem::replace`?");
105 /// Implementation of the `ALMOST_SWAPPED` lint.
106 fn check_suspicious_swap(cx: &LateContext, block: &Block) {
107 for w in block.stmts.windows(2) {
109 let StmtSemi(ref first, _) = w[0].node,
110 let StmtSemi(ref second, _) = w[1].node,
111 !differing_macro_contexts(first.span, second.span),
112 let ExprAssign(ref lhs0, ref rhs0) = first.node,
113 let ExprAssign(ref lhs1, ref rhs1) = second.node,
114 SpanlessEq::new(cx).ignore_fn().eq_expr(lhs0, rhs1),
115 SpanlessEq::new(cx).ignore_fn().eq_expr(lhs1, rhs0)
117 let (what, lhs, rhs) = if let (Some(first), Some(second)) = (snippet_opt(cx, lhs0.span), snippet_opt(cx, rhs0.span)) {
118 (format!(" `{}` and `{}`", first, second), first, second)
120 ("".to_owned(), "".to_owned(), "".to_owned())
123 let span = mk_sp(first.span.lo, second.span.hi);
125 span_lint_and_then(cx,
128 &format!("this looks like you are trying to swap{}", what),
130 if !what.is_empty() {
131 db.span_suggestion(span, "try",
132 format!("std::mem::swap(&mut {}, &mut {})", lhs, rhs));
133 db.note("or maybe you should use `std::mem::replace`?");