diff options
Diffstat (limited to 'src/tools/clippy/src/docs/same_item_push.txt')
-rw-r--r-- | src/tools/clippy/src/docs/same_item_push.txt | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/tools/clippy/src/docs/same_item_push.txt b/src/tools/clippy/src/docs/same_item_push.txt new file mode 100644 index 000000000..7e724073f --- /dev/null +++ b/src/tools/clippy/src/docs/same_item_push.txt @@ -0,0 +1,29 @@ +### What it does +Checks whether a for loop is being used to push a constant +value into a Vec. + +### Why is this bad? +This kind of operation can be expressed more succinctly with +`vec![item; SIZE]` or `vec.resize(NEW_SIZE, item)` and using these alternatives may also +have better performance. + +### Example +``` +let item1 = 2; +let item2 = 3; +let mut vec: Vec<u8> = Vec::new(); +for _ in 0..20 { + vec.push(item1); +} +for _ in 0..30 { + vec.push(item2); +} +``` + +Use instead: +``` +let item1 = 2; +let item2 = 3; +let mut vec: Vec<u8> = vec![item1; 20]; +vec.resize(20 + 30, item2); +```
\ No newline at end of file |