Skip to content

False positive in unsafe_vector_initialization: nonintegral vector element type #3477

@dtolnay

Description

@dtolnay
unsafe fn f<T: Copy, R: std::io::Read>(len: usize, io: R) -> Result<Vec<T>, ()> {
    let mut vec = Vec::<T>::with_capacity(len);
    vec.set_len(len);

    /* io.read_exact... */

    Ok(vec)
}
error: unsafe vector initialization
 --> src/main.rs:3:5
  |
2 |     let mut vec = Vec::<T>::with_capacity(len);
  |                   ---------------------------- help: consider replace allocation with: `vec![0; len]`
3 |     vec.set_len(len);
  |     ^^^^^^^^^^^^^^^^
  |

The suggested replacement of vec![0; len] is not a viable replacement because the vector element type is not something that can be "0".


clippy 0.0.212 (b2601be 2018-11-27)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveL-suggestionLint: Improving, adding or fixing lint suggestionsgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions