Skip to content

Commit ac60dca

Browse files
committed
Auto merge of #10177 - chansuke:almost_swapped, r=Alexendoo
Almost swapped Take over from #8945 Fix #8151 --- changelog: enhancement: [`almost_swapped`]: Now detects almost swaps using `let` statements [#10177](#10177) <!-- changelog_checked -->
2 parents 298f139 + ebca1b5 commit ac60dca

File tree

4 files changed

+166
-59
lines changed

4 files changed

+166
-59
lines changed

clippy_lints/src/swap.rs

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::ty;
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
1212
use rustc_span::source_map::Spanned;
13-
use rustc_span::{sym, Span};
13+
use rustc_span::{sym, symbol::Ident, Span};
1414

1515
declare_clippy_lint! {
1616
/// ### What it does
@@ -174,53 +174,74 @@ fn check_manual_swap(cx: &LateContext<'_>, block: &Block<'_>) {
174174

175175
/// Implementation of the `ALMOST_SWAPPED` lint.
176176
fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
177-
for w in block.stmts.windows(2) {
178-
if_chain! {
179-
if let StmtKind::Semi(first) = w[0].kind;
180-
if let StmtKind::Semi(second) = w[1].kind;
181-
if first.span.ctxt() == second.span.ctxt();
182-
if let ExprKind::Assign(lhs0, rhs0, _) = first.kind;
183-
if let ExprKind::Assign(lhs1, rhs1, _) = second.kind;
184-
if eq_expr_value(cx, lhs0, rhs1);
185-
if eq_expr_value(cx, lhs1, rhs0);
186-
then {
187-
let lhs0 = Sugg::hir_opt(cx, lhs0);
188-
let rhs0 = Sugg::hir_opt(cx, rhs0);
189-
let (what, lhs, rhs) = if let (Some(first), Some(second)) = (lhs0, rhs0) {
190-
(
191-
format!(" `{first}` and `{second}`"),
192-
first.mut_addr().to_string(),
193-
second.mut_addr().to_string(),
194-
)
195-
} else {
196-
(String::new(), String::new(), String::new())
197-
};
177+
for [first, second] in block.stmts.array_windows() {
178+
if let Some((lhs0, rhs0)) = parse(first)
179+
&& let Some((lhs1, rhs1)) = parse(second)
180+
&& first.span.eq_ctxt(second.span)
181+
&& is_same(cx, lhs0, rhs1)
182+
&& is_same(cx, lhs1, rhs0)
183+
&& let Some(lhs_sugg) = match &lhs0 {
184+
ExprOrIdent::Expr(expr) => Sugg::hir_opt(cx, expr),
185+
ExprOrIdent::Ident(ident) => Some(Sugg::NonParen(ident.as_str().into())),
186+
}
187+
&& let Some(rhs_sugg) = Sugg::hir_opt(cx, rhs0)
188+
{
189+
let span = first.span.to(rhs1.span);
190+
let Some(sugg) = std_or_core(cx) else { return };
191+
span_lint_and_then(
192+
cx,
193+
ALMOST_SWAPPED,
194+
span,
195+
&format!("this looks like you are trying to swap `{lhs_sugg}` and `{rhs_sugg}`"),
196+
|diag| {
197+
diag.span_suggestion(
198+
span,
199+
"try",
200+
format!("{sugg}::mem::swap({}, {})", lhs_sugg.mut_addr(), rhs_sugg.mut_addr()),
201+
Applicability::MaybeIncorrect,
202+
);
203+
diag.note(format!("or maybe you should use `{sugg}::mem::replace`?"));
204+
},
205+
);
206+
}
207+
}
208+
}
209+
210+
fn is_same(cx: &LateContext<'_>, lhs: ExprOrIdent<'_>, rhs: &Expr<'_>) -> bool {
211+
match lhs {
212+
ExprOrIdent::Expr(expr) => eq_expr_value(cx, expr, rhs),
213+
ExprOrIdent::Ident(ident) => {
214+
if let ExprKind::Path(QPath::Resolved(None, path)) = rhs.kind
215+
&& let [segment] = &path.segments
216+
&& segment.ident == ident
217+
{
218+
true
219+
} else {
220+
false
221+
}
222+
}
223+
}
224+
}
198225

199-
let span = first.span.to(second.span);
200-
let Some(sugg) = std_or_core(cx) else { return };
226+
#[derive(Debug, Clone, Copy)]
227+
enum ExprOrIdent<'a> {
228+
Expr(&'a Expr<'a>),
229+
Ident(Ident),
230+
}
201231

202-
span_lint_and_then(cx,
203-
ALMOST_SWAPPED,
204-
span,
205-
&format!("this looks like you are trying to swap{what}"),
206-
|diag| {
207-
if !what.is_empty() {
208-
diag.span_suggestion(
209-
span,
210-
"try",
211-
format!(
212-
"{sugg}::mem::swap({lhs}, {rhs})",
213-
),
214-
Applicability::MaybeIncorrect,
215-
);
216-
diag.note(
217-
format!("or maybe you should use `{sugg}::mem::replace`?")
218-
);
219-
}
220-
});
232+
fn parse<'a, 'hir>(stmt: &'a Stmt<'hir>) -> Option<(ExprOrIdent<'hir>, &'a Expr<'hir>)> {
233+
if let StmtKind::Semi(expr) = stmt.kind {
234+
if let ExprKind::Assign(lhs, rhs, _) = expr.kind {
235+
return Some((ExprOrIdent::Expr(lhs), rhs));
236+
}
237+
} else if let StmtKind::Local(expr) = stmt.kind {
238+
if let Some(rhs) = expr.init {
239+
if let PatKind::Binding(_, _, ident_l, _) = expr.pat.kind {
240+
return Some((ExprOrIdent::Ident(ident_l), rhs));
221241
}
222242
}
223243
}
244+
None
224245
}
225246

226247
/// Implementation of the xor case for `MANUAL_SWAP` lint.

tests/ui/swap.fixed

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
clippy::redundant_clone,
88
redundant_semicolons,
99
dead_code,
10-
unused_assignments
10+
unused_assignments,
11+
unused_variables
1112
)]
1213

1314
struct Foo(u32);
@@ -121,6 +122,27 @@ fn main() {
121122
std::mem::swap(&mut c.0, &mut a);
122123

123124
; std::mem::swap(&mut c.0, &mut a);
125+
126+
std::mem::swap(&mut a, &mut b);
127+
128+
let mut c = 1;
129+
let mut d = 2;
130+
std::mem::swap(&mut d, &mut c);
131+
132+
let mut b = 1;
133+
std::mem::swap(&mut a, &mut b);
134+
135+
let b = 1;
136+
let a = 2;
137+
138+
let t = b;
139+
let b = a;
140+
let a = t;
141+
142+
let mut b = 1;
143+
let mut a = 2;
144+
145+
std::mem::swap(&mut b, &mut a);
124146
}
125147

126148
fn issue_8154() {

tests/ui/swap.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
clippy::redundant_clone,
88
redundant_semicolons,
99
dead_code,
10-
unused_assignments
10+
unused_assignments,
11+
unused_variables
1112
)]
1213

1314
struct Foo(u32);
@@ -143,6 +144,32 @@ fn main() {
143144
; let t = c.0;
144145
c.0 = a;
145146
a = t;
147+
148+
let a = b;
149+
let b = a;
150+
151+
let mut c = 1;
152+
let mut d = 2;
153+
d = c;
154+
c = d;
155+
156+
let mut b = 1;
157+
let a = b;
158+
b = a;
159+
160+
let b = 1;
161+
let a = 2;
162+
163+
let t = b;
164+
let b = a;
165+
let a = t;
166+
167+
let mut b = 1;
168+
let mut a = 2;
169+
170+
let t = b;
171+
b = a;
172+
a = t;
146173
}
147174

148175
fn issue_8154() {

tests/ui/swap.stderr

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this looks like you are swapping `bar.a` and `bar.b` manually
2-
--> $DIR/swap.rs:24:5
2+
--> $DIR/swap.rs:25:5
33
|
44
LL | / let temp = bar.a;
55
LL | | bar.a = bar.b;
@@ -10,55 +10,55 @@ LL | | bar.b = temp;
1010
= note: `-D clippy::manual-swap` implied by `-D warnings`
1111

1212
error: this looks like you are swapping elements of `foo` manually
13-
--> $DIR/swap.rs:36:5
13+
--> $DIR/swap.rs:37:5
1414
|
1515
LL | / let temp = foo[0];
1616
LL | | foo[0] = foo[1];
1717
LL | | foo[1] = temp;
1818
| |_________________^ help: try: `foo.swap(0, 1)`
1919

2020
error: this looks like you are swapping elements of `foo` manually
21-
--> $DIR/swap.rs:45:5
21+
--> $DIR/swap.rs:46:5
2222
|
2323
LL | / let temp = foo[0];
2424
LL | | foo[0] = foo[1];
2525
LL | | foo[1] = temp;
2626
| |_________________^ help: try: `foo.swap(0, 1)`
2727

2828
error: this looks like you are swapping elements of `foo` manually
29-
--> $DIR/swap.rs:64:5
29+
--> $DIR/swap.rs:65:5
3030
|
3131
LL | / let temp = foo[0];
3232
LL | | foo[0] = foo[1];
3333
LL | | foo[1] = temp;
3434
| |_________________^ help: try: `foo.swap(0, 1)`
3535

3636
error: this looks like you are swapping `a` and `b` manually
37-
--> $DIR/swap.rs:75:5
37+
--> $DIR/swap.rs:76:5
3838
|
3939
LL | / a ^= b;
4040
LL | | b ^= a;
4141
LL | | a ^= b;
4242
| |___________^ help: try: `std::mem::swap(&mut a, &mut b)`
4343

4444
error: this looks like you are swapping `bar.a` and `bar.b` manually
45-
--> $DIR/swap.rs:83:5
45+
--> $DIR/swap.rs:84:5
4646
|
4747
LL | / bar.a ^= bar.b;
4848
LL | | bar.b ^= bar.a;
4949
LL | | bar.a ^= bar.b;
5050
| |___________________^ help: try: `std::mem::swap(&mut bar.a, &mut bar.b)`
5151

5252
error: this looks like you are swapping elements of `foo` manually
53-
--> $DIR/swap.rs:91:5
53+
--> $DIR/swap.rs:92:5
5454
|
5555
LL | / foo[0] ^= foo[1];
5656
LL | | foo[1] ^= foo[0];
5757
LL | | foo[0] ^= foo[1];
5858
| |_____________________^ help: try: `foo.swap(0, 1)`
5959

6060
error: this looks like you are swapping `foo[0][1]` and `bar[1][0]` manually
61-
--> $DIR/swap.rs:120:5
61+
--> $DIR/swap.rs:121:5
6262
|
6363
LL | / let temp = foo[0][1];
6464
LL | | foo[0][1] = bar[1][0];
@@ -68,7 +68,7 @@ LL | | bar[1][0] = temp;
6868
= note: or maybe you should use `std::mem::replace`?
6969

7070
error: this looks like you are swapping `a` and `b` manually
71-
--> $DIR/swap.rs:134:7
71+
--> $DIR/swap.rs:135:7
7272
|
7373
LL | ; let t = a;
7474
| _______^
@@ -79,7 +79,7 @@ LL | | b = t;
7979
= note: or maybe you should use `std::mem::replace`?
8080

8181
error: this looks like you are swapping `c.0` and `a` manually
82-
--> $DIR/swap.rs:143:7
82+
--> $DIR/swap.rs:144:7
8383
|
8484
LL | ; let t = c.0;
8585
| _______^
@@ -89,8 +89,18 @@ LL | | a = t;
8989
|
9090
= note: or maybe you should use `std::mem::replace`?
9191

92+
error: this looks like you are swapping `b` and `a` manually
93+
--> $DIR/swap.rs:170:5
94+
|
95+
LL | / let t = b;
96+
LL | | b = a;
97+
LL | | a = t;
98+
| |_________^ help: try: `std::mem::swap(&mut b, &mut a)`
99+
|
100+
= note: or maybe you should use `std::mem::replace`?
101+
92102
error: this looks like you are trying to swap `a` and `b`
93-
--> $DIR/swap.rs:131:5
103+
--> $DIR/swap.rs:132:5
94104
|
95105
LL | / a = b;
96106
LL | | b = a;
@@ -100,16 +110,43 @@ LL | | b = a;
100110
= note: `-D clippy::almost-swapped` implied by `-D warnings`
101111

102112
error: this looks like you are trying to swap `c.0` and `a`
103-
--> $DIR/swap.rs:140:5
113+
--> $DIR/swap.rs:141:5
104114
|
105115
LL | / c.0 = a;
106116
LL | | a = c.0;
107117
| |___________^ help: try: `std::mem::swap(&mut c.0, &mut a)`
108118
|
109119
= note: or maybe you should use `std::mem::replace`?
110120

121+
error: this looks like you are trying to swap `a` and `b`
122+
--> $DIR/swap.rs:148:5
123+
|
124+
LL | / let a = b;
125+
LL | | let b = a;
126+
| |_____________^ help: try: `std::mem::swap(&mut a, &mut b)`
127+
|
128+
= note: or maybe you should use `std::mem::replace`?
129+
130+
error: this looks like you are trying to swap `d` and `c`
131+
--> $DIR/swap.rs:153:5
132+
|
133+
LL | / d = c;
134+
LL | | c = d;
135+
| |_________^ help: try: `std::mem::swap(&mut d, &mut c)`
136+
|
137+
= note: or maybe you should use `std::mem::replace`?
138+
139+
error: this looks like you are trying to swap `a` and `b`
140+
--> $DIR/swap.rs:157:5
141+
|
142+
LL | / let a = b;
143+
LL | | b = a;
144+
| |_________^ help: try: `std::mem::swap(&mut a, &mut b)`
145+
|
146+
= note: or maybe you should use `std::mem::replace`?
147+
111148
error: this looks like you are swapping `s.0.x` and `s.0.y` manually
112-
--> $DIR/swap.rs:178:5
149+
--> $DIR/swap.rs:205:5
113150
|
114151
LL | / let t = s.0.x;
115152
LL | | s.0.x = s.0.y;
@@ -118,5 +155,5 @@ LL | | s.0.y = t;
118155
|
119156
= note: or maybe you should use `std::mem::replace`?
120157

121-
error: aborting due to 13 previous errors
158+
error: aborting due to 17 previous errors
122159

0 commit comments

Comments
 (0)