Skip to content

Bad suggestion with borrowed_box lint #6723

Description

@ThibsG

I tried this code:

pub trait Inner{}
pub struct Wrapper(Box<dyn Inner>);

pub unsafe fn get_inner(wrapper: *mut Wrapper) -> &'static Box<dyn Inner> {
    &(*wrapper).0
}

I expected to see this happen: nothing happens or a valid suggestion is given

Instead, this happened: fires borrowed-box lint and suggests removing Box in return type which results in compilation error

warning: you seem to be trying to use `&Box<T>`. Consider using just `&T`
  --> src/lib.rs:10:51
   |
10 | pub unsafe fn get_inner(wrapper: *mut Wrapper) -> &'static Box<dyn Inner> {
   |                                                   ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&'static dyn Inner`
   |
   = note: `#[warn(clippy::borrowed_box)]` on by default

but using suggestion causes compile time error:

error[E0277]: the trait bound `std::boxed::Box<(dyn Inner + 'static)>: Inner` is not satisfied
  --> src/lib.rs:11:5
   |
11 |     &(*wrapper).0
   |     ^^^^^^^^^^^^^ the trait `Inner` is not implemented for `std::boxed::Box<(dyn Inner + 'static)>`
   |
   = note: required for the cast to the object type `dyn Inner`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.

Meta

Rust 1.49.0

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f6b0796cfa3ede3c6a695aba8e497f83

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 applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions