Rust-clippy: Suggest `resize` rather when pushing same item to vec

Created on 10 May 2019  路  3Comments  路  Source: rust-lang/rust-clippy

Example in https://github.com/Gymmasssorla/finshir/blob/eae5d0c6761f5558c8ed33ef098d8bd13a07e64f/src/testing/helpers.rs#L55-L61

When pushing the same item to vec.

fn gen_portions() -> Vec<Vec<u8>> {
    let mut spaces = Vec::with_capacity(EMPTY_SPACES_COUNT);
    for _ in 0..EMPTY_SPACES_COUNT {
        spaces.push(vec![b' ']);
    }
    spaces
}

Could be.

fn gen_portions() -> Vec<Vec<u8>> {
    let mut spaces = Vec::with_capacity(EMPTY_SPACES_COUNT);
    spaces.resize(EMPTY_SPACES_COUNT, vec![b' ']);
    spaces
}

Maybe we could even use vec![b''].repeat(EMPTY_SPACES_COUNT) after stabilization ofrepeat_generic_size? https://github.com/rust-lang/rust/issues/48784

A-complexity L-lint good-first-issue

All 3 comments

Was this attached PR https://github.com/rust-lang/rust-clippy/pull/4647 closed just because the conflict was not resolved? If so, and if no one is interested, can I take over?

@giraffate I believe no one is working on this, sure please go ahead. The PR also provide a good suggestion with vec![item; SIZE] which wasn't discussed here.

@giraffate All issued marked with S-inactive-closed are "free for all" and can be picked up and completed anytime.

Was this page helpful?
0 / 5 - 0 ratings