Refactor almost_swapped to lint with let statement correctly

This commit is contained in:
chansuke 2023-01-08 20:02:50 +09:00
parent 179c037643
commit ebca1b5d00
6 changed files with 162 additions and 191 deletions

View file

@ -1,32 +0,0 @@
#![allow(clippy::needless_late_init, clippy::manual_swap)]
#![allow(unused_variables, unused_assignments)]
#![warn(clippy::almost_swapped)]
fn main() {
let b = 1;
let a = b;
let b = a;
let mut c = 1;
let mut d = 2;
d = c;
c = d;
let mut b = 1;
let a = b;
b = a;
let b = 1;
let a = 2;
let t = b;
let b = a;
let a = t;
let mut b = 1;
let mut a = 2;
let t = b;
b = a;
a = t;
}

View file

@ -1,30 +0,0 @@
error: this looks like you are trying to swap `a` and `b`
--> $DIR/almost_swapped.rs:7:5
|
LL | / let a = b;
LL | | let b = a;
| |______________^
|
= note: `-D clippy::almost-swapped` implied by `-D warnings`
= note: maybe you could use `std::mem::swap(&mut a, &mut b)` or `std::mem::replace`?
error: this looks like you are trying to swap `d` and `c`
--> $DIR/almost_swapped.rs:12:5
|
LL | / d = c;
LL | | c = d;
| |_________^ help: try: `std::mem::swap(&mut d, &mut c)`
|
= note: or maybe you should use `std::mem::replace`?
error: this looks like you are trying to swap `b` and `a`
--> $DIR/almost_swapped.rs:16:5
|
LL | / let a = b;
LL | | b = a;
| |_________^
|
= note: maybe you could use `std::mem::swap(&mut b, &mut a)` or `std::mem::replace`?
error: aborting due to 3 previous errors

View file

@ -7,7 +7,8 @@
clippy::redundant_clone,
redundant_semicolons,
dead_code,
unused_assignments
unused_assignments,
unused_variables
)]
struct Foo(u32);
@ -121,6 +122,27 @@ fn main() {
std::mem::swap(&mut c.0, &mut a);
; std::mem::swap(&mut c.0, &mut a);
std::mem::swap(&mut a, &mut b);
let mut c = 1;
let mut d = 2;
std::mem::swap(&mut d, &mut c);
let mut b = 1;
std::mem::swap(&mut a, &mut b);
let b = 1;
let a = 2;
let t = b;
let b = a;
let a = t;
let mut b = 1;
let mut a = 2;
std::mem::swap(&mut b, &mut a);
}
fn issue_8154() {

View file

@ -7,7 +7,8 @@
clippy::redundant_clone,
redundant_semicolons,
dead_code,
unused_assignments
unused_assignments,
unused_variables
)]
struct Foo(u32);
@ -143,6 +144,32 @@ fn main() {
; let t = c.0;
c.0 = a;
a = t;
let a = b;
let b = a;
let mut c = 1;
let mut d = 2;
d = c;
c = d;
let mut b = 1;
let a = b;
b = a;
let b = 1;
let a = 2;
let t = b;
let b = a;
let a = t;
let mut b = 1;
let mut a = 2;
let t = b;
b = a;
a = t;
}
fn issue_8154() {

View file

@ -1,5 +1,5 @@
error: this looks like you are swapping `bar.a` and `bar.b` manually
--> $DIR/swap.rs:24:5
--> $DIR/swap.rs:25:5
|
LL | / let temp = bar.a;
LL | | bar.a = bar.b;
@ -10,7 +10,7 @@ LL | | bar.b = temp;
= note: `-D clippy::manual-swap` implied by `-D warnings`
error: this looks like you are swapping elements of `foo` manually
--> $DIR/swap.rs:36:5
--> $DIR/swap.rs:37:5
|
LL | / let temp = foo[0];
LL | | foo[0] = foo[1];
@ -18,7 +18,7 @@ LL | | foo[1] = temp;
| |_________________^ help: try: `foo.swap(0, 1)`
error: this looks like you are swapping elements of `foo` manually
--> $DIR/swap.rs:45:5
--> $DIR/swap.rs:46:5
|
LL | / let temp = foo[0];
LL | | foo[0] = foo[1];
@ -26,7 +26,7 @@ LL | | foo[1] = temp;
| |_________________^ help: try: `foo.swap(0, 1)`
error: this looks like you are swapping elements of `foo` manually
--> $DIR/swap.rs:64:5
--> $DIR/swap.rs:65:5
|
LL | / let temp = foo[0];
LL | | foo[0] = foo[1];
@ -34,7 +34,7 @@ LL | | foo[1] = temp;
| |_________________^ help: try: `foo.swap(0, 1)`
error: this looks like you are swapping `a` and `b` manually
--> $DIR/swap.rs:75:5
--> $DIR/swap.rs:76:5
|
LL | / a ^= b;
LL | | b ^= a;
@ -42,7 +42,7 @@ LL | | a ^= b;
| |___________^ help: try: `std::mem::swap(&mut a, &mut b)`
error: this looks like you are swapping `bar.a` and `bar.b` manually
--> $DIR/swap.rs:83:5
--> $DIR/swap.rs:84:5
|
LL | / bar.a ^= bar.b;
LL | | bar.b ^= bar.a;
@ -50,7 +50,7 @@ LL | | bar.a ^= bar.b;
| |___________________^ help: try: `std::mem::swap(&mut bar.a, &mut bar.b)`
error: this looks like you are swapping elements of `foo` manually
--> $DIR/swap.rs:91:5
--> $DIR/swap.rs:92:5
|
LL | / foo[0] ^= foo[1];
LL | | foo[1] ^= foo[0];
@ -58,7 +58,7 @@ LL | | foo[0] ^= foo[1];
| |_____________________^ help: try: `foo.swap(0, 1)`
error: this looks like you are swapping `foo[0][1]` and `bar[1][0]` manually
--> $DIR/swap.rs:120:5
--> $DIR/swap.rs:121:5
|
LL | / let temp = foo[0][1];
LL | | foo[0][1] = bar[1][0];
@ -68,7 +68,7 @@ LL | | bar[1][0] = temp;
= note: or maybe you should use `std::mem::replace`?
error: this looks like you are swapping `a` and `b` manually
--> $DIR/swap.rs:134:7
--> $DIR/swap.rs:135:7
|
LL | ; let t = a;
| _______^
@ -79,7 +79,7 @@ LL | | b = t;
= note: or maybe you should use `std::mem::replace`?
error: this looks like you are swapping `c.0` and `a` manually
--> $DIR/swap.rs:143:7
--> $DIR/swap.rs:144:7
|
LL | ; let t = c.0;
| _______^
@ -89,8 +89,18 @@ LL | | a = t;
|
= note: or maybe you should use `std::mem::replace`?
error: this looks like you are swapping `b` and `a` manually
--> $DIR/swap.rs:170:5
|
LL | / let t = b;
LL | | b = a;
LL | | a = t;
| |_________^ help: try: `std::mem::swap(&mut b, &mut a)`
|
= note: or maybe you should use `std::mem::replace`?
error: this looks like you are trying to swap `a` and `b`
--> $DIR/swap.rs:131:5
--> $DIR/swap.rs:132:5
|
LL | / a = b;
LL | | b = a;
@ -100,7 +110,7 @@ LL | | b = a;
= note: `-D clippy::almost-swapped` implied by `-D warnings`
error: this looks like you are trying to swap `c.0` and `a`
--> $DIR/swap.rs:140:5
--> $DIR/swap.rs:141:5
|
LL | / c.0 = a;
LL | | a = c.0;
@ -108,8 +118,35 @@ LL | | a = c.0;
|
= note: or maybe you should use `std::mem::replace`?
error: this looks like you are trying to swap `a` and `b`
--> $DIR/swap.rs:148:5
|
LL | / let a = b;
LL | | let b = a;
| |_____________^ help: try: `std::mem::swap(&mut a, &mut b)`
|
= note: or maybe you should use `std::mem::replace`?
error: this looks like you are trying to swap `d` and `c`
--> $DIR/swap.rs:153:5
|
LL | / d = c;
LL | | c = d;
| |_________^ help: try: `std::mem::swap(&mut d, &mut c)`
|
= note: or maybe you should use `std::mem::replace`?
error: this looks like you are trying to swap `a` and `b`
--> $DIR/swap.rs:157:5
|
LL | / let a = b;
LL | | b = a;
| |_________^ help: try: `std::mem::swap(&mut a, &mut b)`
|
= note: or maybe you should use `std::mem::replace`?
error: this looks like you are swapping `s.0.x` and `s.0.y` manually
--> $DIR/swap.rs:178:5
--> $DIR/swap.rs:205:5
|
LL | / let t = s.0.x;
LL | | s.0.x = s.0.y;
@ -118,5 +155,5 @@ LL | | s.0.y = t;
|
= note: or maybe you should use `std::mem::replace`?
error: aborting due to 13 previous errors
error: aborting due to 17 previous errors