Skip to content

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

@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();
    }
}

Activity

EFanZh

EFanZh commented on Sep 14, 2019

@EFanZh

I think Clippy should allow the usage of &mut Box<T> but disallow the usage of &Box<T> since we might want to change the box pointer instead of the object it points to.

added
good first issueThese issues are a good way to get started with Clippy
C-bugCategory: Clippy is not doing the correct thing
I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
on Sep 17, 2019
jmaargh

jmaargh commented on Mar 20, 2020

@jmaargh

A (perhaps a little contrived) example: https://godbolt.org/z/LFRvd7

added 2 commits that reference this issue on Apr 19, 2020
ebroto

ebroto commented on May 15, 2020

@ebroto
Member

I think this issue can be closed given that #5491 was merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @okready@ebroto@EFanZh@flip1995@jmaargh

        Issue actions

          `borrowed_box` incorrectly assumes mutable boxed slice allocation won't change · Issue #2907 · rust-lang/rust-clippy