forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#12340 - not-elm:fix/issue-12334, r=llogiq
FIX(12334): manual_swap auto fix Fixed: rust-lang#12334 Initialization expressions are now generated as needed if the slice index is bound to a variable. ---- changelog: Fix [`manual_swap`]
- Loading branch information
Showing
4 changed files
with
381 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#![warn(clippy::manual_swap)] | ||
#![no_main] | ||
|
||
fn swap1() { | ||
let mut v = [3, 2, 1, 0]; | ||
let index = v[0]; | ||
v.swap(0, index); | ||
} | ||
|
||
fn swap2() { | ||
let mut v = [3, 2, 1, 0]; | ||
let tmp = v[0]; | ||
v.swap(0, 1); | ||
// check not found in this scope. | ||
let _ = tmp; | ||
} | ||
|
||
fn swap3() { | ||
let mut v = [3, 2]; | ||
let i1 = 0; | ||
let i2 = 1; | ||
v.swap(i1, i2); | ||
} | ||
|
||
fn swap4() { | ||
let mut v = [3, 2, 1]; | ||
let i1 = 0; | ||
let i2 = 1; | ||
v.swap(i1, i2 + 1); | ||
} | ||
|
||
fn swap5() { | ||
let mut v = [0, 1, 2, 3]; | ||
let i1 = 0; | ||
let i2 = 1; | ||
v.swap(i1, i2 + 1); | ||
} | ||
|
||
fn swap6() { | ||
let mut v = [0, 1, 2, 3]; | ||
let index = v[0]; | ||
v.swap(0, index + 1); | ||
} | ||
|
||
fn swap7() { | ||
let mut v = [0, 1, 2, 3]; | ||
let i1 = 0; | ||
let i2 = 6; | ||
v.swap(i1 * 3, i2 / 2); | ||
} | ||
|
||
fn swap8() { | ||
let mut v = [1, 2, 3, 4]; | ||
let i1 = 1; | ||
let i2 = 1; | ||
v.swap(i1 + i2, i2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#![warn(clippy::manual_swap)] | ||
#![no_main] | ||
|
||
fn swap1() { | ||
let mut v = [3, 2, 1, 0]; | ||
let index = v[0]; | ||
//~^ ERROR: this looks like you are swapping elements of `v` manually | ||
v[0] = v[index]; | ||
v[index] = index; | ||
} | ||
|
||
fn swap2() { | ||
let mut v = [3, 2, 1, 0]; | ||
let tmp = v[0]; | ||
v[0] = v[1]; | ||
v[1] = tmp; | ||
// check not found in this scope. | ||
let _ = tmp; | ||
} | ||
|
||
fn swap3() { | ||
let mut v = [3, 2]; | ||
let i1 = 0; | ||
let i2 = 1; | ||
let temp = v[i1]; | ||
v[i1] = v[i2]; | ||
v[i2] = temp; | ||
} | ||
|
||
fn swap4() { | ||
let mut v = [3, 2, 1]; | ||
let i1 = 0; | ||
let i2 = 1; | ||
let temp = v[i1]; | ||
v[i1] = v[i2 + 1]; | ||
v[i2 + 1] = temp; | ||
} | ||
|
||
fn swap5() { | ||
let mut v = [0, 1, 2, 3]; | ||
let i1 = 0; | ||
let i2 = 1; | ||
let temp = v[i1]; | ||
v[i1] = v[i2 + 1]; | ||
v[i2 + 1] = temp; | ||
} | ||
|
||
fn swap6() { | ||
let mut v = [0, 1, 2, 3]; | ||
let index = v[0]; | ||
//~^ ERROR: this looks like you are swapping elements of `v` manually | ||
v[0] = v[index + 1]; | ||
v[index + 1] = index; | ||
} | ||
|
||
fn swap7() { | ||
let mut v = [0, 1, 2, 3]; | ||
let i1 = 0; | ||
let i2 = 6; | ||
let tmp = v[i1 * 3]; | ||
v[i1 * 3] = v[i2 / 2]; | ||
v[i2 / 2] = tmp; | ||
} | ||
|
||
fn swap8() { | ||
let mut v = [1, 2, 3, 4]; | ||
let i1 = 1; | ||
let i2 = 1; | ||
let tmp = v[i1 + i2]; | ||
v[i1 + i2] = v[i2]; | ||
v[i2] = tmp; | ||
} |
Oops, something went wrong.