Skip to content

Commit 5c4040e

Browse files
committed
Auto merge of #10502 - blyxyas:fix-almost_swapped, r=giraffate
fix `almost_swapped`: Ignore external macros Fixes #10421 ; Related to #10499 (Fixing points *1* and *3* from #10421) changelog: [`almost_swapped`]: Add a check to ignore external macros
2 parents 5afa93b + 9546517 commit 5c4040e

File tree

5 files changed

+44
-22
lines changed

5 files changed

+44
-22
lines changed

clippy_lints/src/swap.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use clippy_utils::{can_mut_borrow_both, eq_expr_value, in_constant, std_or_core}
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
9-
use rustc_lint::{LateContext, LateLintPass};
9+
use rustc_lint::{LateContext, LateLintPass, LintContext};
10+
use rustc_middle::lint::in_external_macro;
1011
use rustc_middle::ty;
1112
use rustc_session::{declare_lint_pass, declare_tool_lint};
1213
use rustc_span::source_map::Spanned;
@@ -188,6 +189,7 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
188189
if let Some((lhs0, rhs0)) = parse(first)
189190
&& let Some((lhs1, rhs1)) = parse(second)
190191
&& first.span.eq_ctxt(second.span)
192+
&& !in_external_macro(&cx.sess(), first.span)
191193
&& is_same(cx, lhs0, rhs1)
192194
&& is_same(cx, lhs1, rhs0)
193195
&& !is_same(cx, lhs1, rhs1) // Ignore a = b; a = a (#10421)

tests/ui/auxiliary/macro_rules.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,13 @@ macro_rules! mut_mut {
2727
let mut_mut_ty: &mut &mut u32 = &mut &mut 1u32;
2828
};
2929
}
30+
31+
#[macro_export]
32+
macro_rules! issue_10421 {
33+
() => {
34+
let mut a = 1;
35+
let mut b = 2;
36+
a = b;
37+
b = a;
38+
};
39+
}

tests/ui/swap.fixed

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-rustfix
2+
// aux-build: macro_rules.rs
23

34
#![warn(clippy::all)]
45
#![allow(
@@ -8,7 +9,8 @@
89
redundant_semicolons,
910
dead_code,
1011
unused_assignments,
11-
unused_variables
12+
unused_variables,
13+
clippy::let_and_return
1214
)]
1315

1416
struct Foo(u32);
@@ -187,8 +189,11 @@ const fn issue_9864(mut u: u32) -> u32 {
187189
u + v
188190
}
189191

190-
#[allow(clippy::let_and_return)]
192+
#[macro_use]
193+
extern crate macro_rules;
194+
191195
const fn issue_10421(x: u32) -> u32 {
196+
issue_10421!();
192197
let a = x;
193198
let a = a;
194199
let a = a;

tests/ui/swap.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-rustfix
2+
// aux-build: macro_rules.rs
23

34
#![warn(clippy::all)]
45
#![allow(
@@ -8,7 +9,8 @@
89
redundant_semicolons,
910
dead_code,
1011
unused_assignments,
11-
unused_variables
12+
unused_variables,
13+
clippy::let_and_return
1214
)]
1315

1416
struct Foo(u32);
@@ -216,8 +218,11 @@ const fn issue_9864(mut u: u32) -> u32 {
216218
u + v
217219
}
218220

219-
#[allow(clippy::let_and_return)]
221+
#[macro_use]
222+
extern crate macro_rules;
223+
220224
const fn issue_10421(x: u32) -> u32 {
225+
issue_10421!();
221226
let a = x;
222227
let a = a;
223228
let a = a;

tests/ui/swap.stderr

Lines changed: 17 additions & 17 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:25:5
2+
--> $DIR/swap.rs:27: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:37:5
13+
--> $DIR/swap.rs:39: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:46:5
21+
--> $DIR/swap.rs:48: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:65:5
29+
--> $DIR/swap.rs:67: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:76:5
37+
--> $DIR/swap.rs:78: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:84:5
45+
--> $DIR/swap.rs:86: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:92:5
53+
--> $DIR/swap.rs:94: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:121:5
61+
--> $DIR/swap.rs:123: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:135:7
71+
--> $DIR/swap.rs:137: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:144:7
82+
--> $DIR/swap.rs:146:7
8383
|
8484
LL | ; let t = c.0;
8585
| _______^
@@ -90,7 +90,7 @@ LL | | a = t;
9090
= note: or maybe you should use `std::mem::replace`?
9191

9292
error: this looks like you are swapping `b` and `a` manually
93-
--> $DIR/swap.rs:170:5
93+
--> $DIR/swap.rs:172:5
9494
|
9595
LL | / let t = b;
9696
LL | | b = a;
@@ -100,7 +100,7 @@ LL | | a = t;
100100
= note: or maybe you should use `std::mem::replace`?
101101

102102
error: this looks like you are trying to swap `a` and `b`
103-
--> $DIR/swap.rs:132:5
103+
--> $DIR/swap.rs:134:5
104104
|
105105
LL | / a = b;
106106
LL | | b = a;
@@ -110,7 +110,7 @@ LL | | b = a;
110110
= note: `-D clippy::almost-swapped` implied by `-D warnings`
111111

112112
error: this looks like you are trying to swap `c.0` and `a`
113-
--> $DIR/swap.rs:141:5
113+
--> $DIR/swap.rs:143:5
114114
|
115115
LL | / c.0 = a;
116116
LL | | a = c.0;
@@ -119,7 +119,7 @@ LL | | a = c.0;
119119
= note: or maybe you should use `std::mem::replace`?
120120

121121
error: this looks like you are trying to swap `a` and `b`
122-
--> $DIR/swap.rs:148:5
122+
--> $DIR/swap.rs:150:5
123123
|
124124
LL | / let a = b;
125125
LL | | let b = a;
@@ -128,7 +128,7 @@ LL | | let b = a;
128128
= note: or maybe you should use `std::mem::replace`?
129129

130130
error: this looks like you are trying to swap `d` and `c`
131-
--> $DIR/swap.rs:153:5
131+
--> $DIR/swap.rs:155:5
132132
|
133133
LL | / d = c;
134134
LL | | c = d;
@@ -137,7 +137,7 @@ LL | | c = d;
137137
= note: or maybe you should use `std::mem::replace`?
138138

139139
error: this looks like you are trying to swap `a` and `b`
140-
--> $DIR/swap.rs:157:5
140+
--> $DIR/swap.rs:159:5
141141
|
142142
LL | / let a = b;
143143
LL | | b = a;
@@ -146,7 +146,7 @@ LL | | b = a;
146146
= note: or maybe you should use `std::mem::replace`?
147147

148148
error: this looks like you are swapping `s.0.x` and `s.0.y` manually
149-
--> $DIR/swap.rs:205:5
149+
--> $DIR/swap.rs:207:5
150150
|
151151
LL | / let t = s.0.x;
152152
LL | | s.0.x = s.0.y;

0 commit comments

Comments
 (0)