forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a01033
commit 39269aa
Showing
3 changed files
with
96 additions
and
2 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,85 @@ | ||
#![allow(clippy::useless_vec)] | ||
use std::iter::repeat; | ||
fn main() { | ||
resize_vector(); | ||
extend_vector(); | ||
mixed_extend_resize_vector(); | ||
from_empty_vec(); | ||
} | ||
|
||
fn extend_vector() { | ||
// Extend with constant expression | ||
let len = 300; | ||
let mut vec1 = vec![0; len]; | ||
|
||
// Extend with len expression | ||
let mut vec2 = vec![0; len - 10]; | ||
|
||
// Extend with mismatching expression should not be warned | ||
let mut vec3 = Vec::with_capacity(24322); | ||
vec3.extend(repeat(0).take(2)); | ||
|
||
let mut vec4 = vec![0; len]; | ||
} | ||
|
||
fn mixed_extend_resize_vector() { | ||
// Mismatching len | ||
let mut mismatching_len = Vec::with_capacity(30); | ||
mismatching_len.extend(repeat(0).take(40)); | ||
|
||
// Slow initialization | ||
let mut resized_vec = vec![0; 30]; | ||
|
||
let mut extend_vec = vec![0; 30]; | ||
} | ||
|
||
fn resize_vector() { | ||
// Resize with constant expression | ||
let len = 300; | ||
let mut vec1 = vec![0; len]; | ||
|
||
// Resize mismatch len | ||
let mut vec2 = Vec::with_capacity(200); | ||
vec2.resize(10, 0); | ||
|
||
// Resize with len expression | ||
let mut vec3 = vec![0; len - 10]; | ||
|
||
let mut vec4 = vec![0; len]; | ||
|
||
// Reinitialization should be warned | ||
vec1 = vec![0; 10]; | ||
} | ||
|
||
fn from_empty_vec() { | ||
// Resize with constant expression | ||
let len = 300; | ||
let mut vec1 = vec![0; len]; | ||
|
||
// Resize with len expression | ||
let mut vec3 = vec![0; len - 10]; | ||
|
||
// Reinitialization should be warned | ||
vec1 = vec![0; 10]; | ||
|
||
vec1 = vec![0; 10]; | ||
|
||
macro_rules! x { | ||
() => { | ||
vec![] | ||
}; | ||
} | ||
|
||
// `vec![]` comes from another macro, don't warn | ||
vec1 = x!(); | ||
vec1.resize(10, 0); | ||
} | ||
|
||
fn do_stuff(vec: &mut [u8]) {} | ||
|
||
fn extend_vector_with_manipulations_between() { | ||
let len = 300; | ||
let mut vec1: Vec<u8> = Vec::with_capacity(len); | ||
do_stuff(&mut vec1); | ||
vec1.extend(repeat(0).take(len)); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
//@no-rustfix | ||
#![allow(clippy::useless_vec)] | ||
use std::iter::repeat; | ||
fn main() { | ||
resize_vector(); | ||
|