Closed
Description
Summary
.
Lint Name
manual_swap
Reproducer
I tried this code:
fn partition(input: &mut Vec<i32>, start: usize, len: usize) -> usize {
//use first element as pivot
let pivot= input[start];
//low index points to partitioned section
let mut index_l = start + 1;
for index in start + 1..len {
//if element is lower than pivot, swap
if input[index] < pivot {
let temp = input[index];
input[index] = input[index_l];
input[index_l] = temp;
index_l += 1;
}
}
//swap pivot to correct position
input[start] = input[index_l - 1];
input[index_l - 1] = pivot;
index_l - 1
}
pub fn quick_sort(input: &mut Vec<i32>, start: usize, len: usize) {
if start < len {
//get pivot position
let pivot_position = partition(input, start, len);
//sorting lower section
quick_sort(input, start, pivot_position);
//sorting higher section
quick_sort(input, pivot_position + 1, len);
}
}
pub fn main() {}
I saw this happen:
warning: this looks like you are swapping elements of `input` manually
--> src/main.rs:10:13
|
10 | / let temp = input[index];
11 | | input[index] = input[index_l];
12 | | input[index_l] = temp;
| |__________________________________^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_swap
= note: `#[warn(clippy::manual_swap)]` on by default
help: try
|
10 ~ let index = index;
11 + let index_l = index_l;
12 + input.swap(index, index_l);
|
The suggested code:
fn partition(input: &mut Vec<i32>, start: usize, len: usize) -> usize {
//use first element as pivot
let pivot= input[start];
//low index points to partitioned section
let mut index_l = start + 1;
for index in start + 1..len {
//if element is lower than pivot, swap
if input[index] < pivot {
let index = index;
let index_l = index_l;
input.swap(index, index_l);
index_l += 1;
}
}
//swap pivot to correct position
input[start] = input[index_l - 1];
input[index_l - 1] = pivot;
index_l - 1
}
pub fn quick_sort(input: &mut Vec<i32>, start: usize, len: usize) {
if start < len {
//get pivot position
let pivot_position = partition(input, start, len);
//sorting lower section
quick_sort(input, start, pivot_position);
//sorting higher section
quick_sort(input, pivot_position + 1, len);
}
}
pub fn main() {}
does not compile:
warning: value assigned to `index_l` is never read
--> src/main.rs:13:13
|
13 | index_l += 1;
| ^^^^^^^
|
= help: maybe it is overwritten before being read?
= note: `#[warn(unused_assignments)]` on by default
warning: variable does not need to be mutable
--> src/main.rs:6:9
|
6 | let mut index_l = start + 1;
| ----^^^^^^^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` on by default
error[E0384]: cannot assign twice to immutable variable `index_l`
--> src/main.rs:13:13
|
11 | let index_l = index_l;
| ------- first assignment to `index_l`
12 | input.swap(index, index_l);
13 | index_l += 1;
| ^^^^^^^^^^^^ cannot assign twice to immutable variable
|
help: consider making this binding mutable
|
11 | let mut index_l = index_l;
| +++
Version
Additional Labels
No response