Skip to content

borrowed_box incorrectly assumes mutable boxed slice allocation won't change #2907

Closed
@okready

Description

@okready

Defining a function that has a &mut Box<[T]> parameter triggers the borrowed_box lint, recommending it be replaced with a &mut [T]. It's possible to have a function that replaces the boxed slice with a different allocation (e.g. a slice with a different size), which cannot be done if the parameter is just a slice reference.

Example:

pub fn maybe_sets_box(boxed_slice: &mut Box<[i32]>) {
    if boxed_slice.is_empty() {
        let mut data = Vec::with_capacity(1);
        data.push(12);
        *boxed_slice = data.into_boxed_slice();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedgood 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