Skip to content

Commit 179c037

Browse files
DevAccentorchansuke
authored andcommitted
improve almost swap to look for let statement
1 parent 5adeebf commit 179c037

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

clippy_lints/src/swap.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ fn check_manual_swap(cx: &LateContext<'_>, block: &Block<'_>) {
172172
}
173173
}
174174

175+
#[allow(clippy::too_many_lines)]
175176
/// Implementation of the `ALMOST_SWAPPED` lint.
176177
fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
177178
for w in block.stmts.windows(2) {
@@ -220,6 +221,79 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
220221
});
221222
}
222223
}
224+
225+
let lint_almost_swapped_note = |span, what: String, sugg, lhs, rhs| {
226+
span_lint_and_then(
227+
cx,
228+
ALMOST_SWAPPED,
229+
span,
230+
&format!("this looks like you are trying to swap{}", what),
231+
|diag| {
232+
if !what.is_empty() {
233+
diag.note(&format!(
234+
"maybe you could use `{sugg}::mem::swap({lhs}, {rhs})` or `{sugg}::mem::replace`?"
235+
));
236+
}
237+
},
238+
);
239+
};
240+
241+
if let StmtKind::Local(first) = w[0].kind
242+
&& let StmtKind::Local(second) = w[1].kind
243+
&& first.span.ctxt() == second.span.ctxt()
244+
&& let Some(rhs0) = first.init
245+
&& let Some(rhs1) = second.init
246+
&& let ExprKind::Path(QPath::Resolved(None, path_l)) = rhs0.kind
247+
&& let ExprKind::Path(QPath::Resolved(None, path_r)) = rhs1.kind
248+
&& let PatKind::Binding(_,_, ident_l,_) = first.pat.kind
249+
&& let PatKind::Binding(_,_, ident_r,_) = second.pat.kind
250+
&& ident_l.name.as_str() == path_r.segments.iter().map(|el| el.ident.to_string()).collect::<Vec<_>>().join("::")
251+
&& ident_r.name.as_str() == path_l.segments.iter().map(|el| el.ident.to_string()).collect::<Vec<_>>().join("::")
252+
{
253+
let rhs0 = Sugg::hir_opt(cx, rhs0);
254+
let (what, lhs, rhs) = if let Some(second) = rhs0 {
255+
(
256+
format!(" `{}` and `{}`", ident_l, second),
257+
format!("&mut {}", ident_l),
258+
second.mut_addr().to_string(),
259+
)
260+
} else {
261+
(String::new(), String::new(), String::new())
262+
};
263+
let span = first.span.to(second.span);
264+
let Some(sugg) = std_or_core(cx) else { return };
265+
266+
lint_almost_swapped_note(span, what, sugg, lhs, rhs);
267+
}
268+
269+
if let StmtKind::Local(first) = w[0].kind
270+
&& let StmtKind::Semi(second) = w[1].kind
271+
&& first.span.ctxt() == second.span.ctxt()
272+
&& let Some(rhs0) = first.init
273+
&& let ExprKind::Path(QPath::Resolved(None, path_l)) = rhs0.kind
274+
&& let PatKind::Binding(_,_, ident_l,_) = first.pat.kind
275+
&& let ExprKind::Assign(lhs1, rhs1, _) = second.kind
276+
&& let ExprKind::Path(QPath::Resolved(None, lhs1_path)) = lhs1.kind
277+
&& let ExprKind::Path(QPath::Resolved(None, rhs1_path)) = rhs1.kind
278+
&& ident_l.name.as_str() == rhs1_path.segments.iter().map(|el| el.ident.to_string()).collect::<Vec<_>>().join("::")
279+
&& path_l.segments.iter().map(|el| el.ident.to_string()).collect::<Vec<_>>().join("::") == lhs1_path.segments.iter().map(|el| el.ident.to_string()).collect::<Vec<_>>().join("::")
280+
{
281+
let lhs1 = Sugg::hir_opt(cx, lhs1);
282+
let rhs1 = Sugg::hir_opt(cx, rhs1);
283+
let (what, lhs, rhs) = if let (Some(first),Some(second)) = (lhs1,rhs1) {
284+
(
285+
format!(" `{}` and `{}`", first, second),
286+
first.mut_addr().to_string(),
287+
second.mut_addr().to_string(),
288+
)
289+
} else {
290+
(String::new(), String::new(), String::new())
291+
};
292+
let span = first.span.to(second.span);
293+
let Some(sugg) = std_or_core(cx) else { return };
294+
295+
lint_almost_swapped_note(span, what, sugg, lhs, rhs);
296+
}
223297
}
224298
}
225299

tests/ui/almost_swapped.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![allow(clippy::needless_late_init, clippy::manual_swap)]
2+
#![allow(unused_variables, unused_assignments)]
3+
#![warn(clippy::almost_swapped)]
4+
5+
fn main() {
6+
let b = 1;
7+
let a = b;
8+
let b = a;
9+
10+
let mut c = 1;
11+
let mut d = 2;
12+
d = c;
13+
c = d;
14+
15+
let mut b = 1;
16+
let a = b;
17+
b = a;
18+
19+
let b = 1;
20+
let a = 2;
21+
22+
let t = b;
23+
let b = a;
24+
let a = t;
25+
26+
let mut b = 1;
27+
let mut a = 2;
28+
29+
let t = b;
30+
b = a;
31+
a = t;
32+
}

tests/ui/almost_swapped.stderr

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error: this looks like you are trying to swap `a` and `b`
2+
--> $DIR/almost_swapped.rs:7:5
3+
|
4+
LL | / let a = b;
5+
LL | | let b = a;
6+
| |______________^
7+
|
8+
= note: `-D clippy::almost-swapped` implied by `-D warnings`
9+
= note: maybe you could use `std::mem::swap(&mut a, &mut b)` or `std::mem::replace`?
10+
11+
error: this looks like you are trying to swap `d` and `c`
12+
--> $DIR/almost_swapped.rs:12:5
13+
|
14+
LL | / d = c;
15+
LL | | c = d;
16+
| |_________^ help: try: `std::mem::swap(&mut d, &mut c)`
17+
|
18+
= note: or maybe you should use `std::mem::replace`?
19+
20+
error: this looks like you are trying to swap `b` and `a`
21+
--> $DIR/almost_swapped.rs:16:5
22+
|
23+
LL | / let a = b;
24+
LL | | b = a;
25+
| |_________^
26+
|
27+
= note: maybe you could use `std::mem::swap(&mut b, &mut a)` or `std::mem::replace`?
28+
29+
error: aborting due to 3 previous errors
30+

0 commit comments

Comments
 (0)