Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect swap suggestion #4478

Merged
merged 2 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clippy_lints/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ fn check_manual_swap(cx: &LateContext<'_, '_>, block: &Block) {
None
}

if let ExprKind::Field(ref lhs1, _) = lhs1.node {
if let ExprKind::Field(ref lhs2, _) = lhs2.node {
if lhs1.hir_id.owner_def_id() == lhs2.hir_id.owner_def_id() {
return;
}
}
}

let (replace, what, sugg) = if let Some((slice, idx1, idx2)) = check_for_slice(cx, lhs1, lhs2) {
if let Some(slice) = Sugg::hir_opt(cx, slice) {
(false,
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@

struct Foo(u32);

#[derive(Clone)]
struct Bar {
a: u32,
b: u32,
}

fn field() {
let mut bar = Bar { a: 1, b: 2 };

let temp = bar.a;
bar.a = bar.b;
bar.b = temp;

let mut baz = vec![bar.clone(), bar.clone()];
let temp = baz[0].a;
baz[0].a = baz[1].a;
baz[1].a = temp;
}

fn array() {
let mut foo = [1, 2];
let temp = foo[0];
Expand Down Expand Up @@ -32,6 +51,7 @@ fn vec() {

#[rustfmt::skip]
fn main() {
field();
array();
slice();
vec();
Expand Down
14 changes: 7 additions & 7 deletions tests/ui/swap.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this looks like you are swapping elements of `foo` manually
--> $DIR/swap.rs:8:5
--> $DIR/swap.rs:27:5
|
LL | / let temp = foo[0];
LL | | foo[0] = foo[1];
Expand All @@ -9,23 +9,23 @@ LL | | foo[1] = temp;
= note: `-D clippy::manual-swap` implied by `-D warnings`

error: this looks like you are swapping elements of `foo` manually
--> $DIR/swap.rs:17:5
--> $DIR/swap.rs:36:5
|
LL | / let temp = foo[0];
LL | | foo[0] = foo[1];
LL | | foo[1] = temp;
| |_________________^ help: try: `foo.swap(0, 1)`

error: this looks like you are swapping elements of `foo` manually
--> $DIR/swap.rs:26:5
--> $DIR/swap.rs:45:5
|
LL | / let temp = foo[0];
LL | | foo[0] = foo[1];
LL | | foo[1] = temp;
| |_________________^ help: try: `foo.swap(0, 1)`

error: this looks like you are swapping `a` and `b` manually
--> $DIR/swap.rs:45:7
--> $DIR/swap.rs:65:7
|
LL | ; let t = a;
| _______^
Expand All @@ -36,7 +36,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:54:7
--> $DIR/swap.rs:74:7
|
LL | ; let t = c.0;
| _______^
Expand All @@ -47,7 +47,7 @@ LL | | a = t;
= note: or maybe you should use `std::mem::replace`?

error: this looks like you are trying to swap `a` and `b`
--> $DIR/swap.rs:42:5
--> $DIR/swap.rs:62:5
|
LL | / a = b;
LL | | b = a;
Expand All @@ -57,7 +57,7 @@ LL | | b = a;
= note: or maybe you should use `std::mem::replace`?

error: this looks like you are trying to swap `c.0` and `a`
--> $DIR/swap.rs:51:5
--> $DIR/swap.rs:71:5
|
LL | / c.0 = a;
LL | | a = c.0;
Expand Down