Skip to content

dangerous_implicit_autorefs lint suggestion triggers clippy::needless_borrow #140720

@wyfo

Description

@wyfo

Let's take the following example:

unsafe fn slice_ptr_len_because_of_msrv<T>(slice: *[T]) -> usize {
    unsafe { (*slice).len() }
}

on current nightly, it triggers:

warning: implicit autoref creates a reference to the dereference of a raw pointer
 --> src/main.rs:2:14
  |
3 |     unsafe { (*slice).len() }
  |              ^^^^^^^^^^^^^^
  |
  = note: creating a reference requires the pointer target to be valid and imposes aliasing requirements
  = note: `#[warn(dangerous_implicit_autorefs)]` on by default
help: try using a raw pointer method instead; or if this reference is intentional, make it explicit
  |
3 |     unsafe { (&(*slice)).len() }
  |              ++        +

If the suggestion is applied, then clippy complains:

warning: this expression borrows a value the compiler would automatically borrow
 --> src/main.rs:2:14
  |
2 |     unsafe { (&(*slice)).len() }
  |              ^^^^^^^^^^^ help: change this to: `(*slice)`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
  = note: `#[warn(clippy::needless_borrow)]` on by default

Curiously enough, (*slice)[..].len() also triggers dangerous_implicit_autorefs, but the suggested (&(*slice))[..].len() doesn't trigger clippy::needless_borrow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.L-dangerous_implicit_autorefsLint: dangerous_implicit_autorefsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions