Skip to content

dangerous_implicit_autorefs lint suggestion triggers clippy::needless_borrow #14743

Open
@wyfo

Description

@wyfo

Description

(transfered from rust-lang/rust#140720)

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.

Version

rustc 1.88.0-nightly (2e6882ac5 2025-05-05)
binary: rustc
commit-hash: 2e6882ac5be27a73293d6f7ae56397fdf32848de
commit-date: 2025-05-05
host: aarch64-apple-darwin
release: 1.88.0-nightly
LLVM version: 20.1.4

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions