Skip to content

Commit 9546517

Browse files
committed
Add external macro test + Now it works
1 parent e2ba75d commit 9546517

File tree

5 files changed

+35
-34
lines changed

5 files changed

+35
-34
lines changed

clippy_lints/src/swap.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ declare_lint_pass!(Swap => [MANUAL_SWAP, ALMOST_SWAPPED]);
7676
impl<'tcx> LateLintPass<'tcx> for Swap {
7777
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'_>) {
7878
check_manual_swap(cx, block);
79-
if !in_external_macro(cx.sess(), block.span) {
80-
check_suspicious_swap(cx, block);
81-
}
79+
check_suspicious_swap(cx, block);
8280
check_xor_swap(cx, block);
8381
}
8482
}
@@ -191,6 +189,7 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
191189
if let Some((lhs0, rhs0)) = parse(first)
192190
&& let Some((lhs1, rhs1)) = parse(second)
193191
&& first.span.eq_ctxt(second.span)
192+
&& !in_external_macro(&cx.sess(), first.span)
194193
&& is_same(cx, lhs0, rhs1)
195194
&& is_same(cx, lhs1, rhs0)
196195
&& !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: 3 additions & 7 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(
@@ -188,13 +189,8 @@ const fn issue_9864(mut u: u32) -> u32 {
188189
u + v
189190
}
190191

191-
macro_rules! issue_10421 {
192-
() => {
193-
let a = 1;
194-
let b = a;
195-
let b = b;
196-
};
197-
}
192+
#[macro_use]
193+
extern crate macro_rules;
198194

199195
const fn issue_10421(x: u32) -> u32 {
200196
issue_10421!();

tests/ui/swap.rs

Lines changed: 3 additions & 7 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(
@@ -217,13 +218,8 @@ const fn issue_9864(mut u: u32) -> u32 {
217218
u + v
218219
}
219220

220-
macro_rules! issue_10421 {
221-
() => {
222-
let a = 1;
223-
let b = a;
224-
let b = b;
225-
};
226-
}
221+
#[macro_use]
222+
extern crate macro_rules;
227223

228224
const fn issue_10421(x: u32) -> u32 {
229225
issue_10421!();

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:26: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:38: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:47: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:66: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:77: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:85: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:93: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:122: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:136: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:145: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:171: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:133: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:142: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:149: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:154: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:158: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:206: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)