Skip to content

dangerous_implicit_autorefs lint suggestion still triggers clippy::needless_borrow #17414

Description

@viliml

Summary

Continuation of #14743

If the reference is not of the pointer directly but of a field of the pointed-to structure, the clippy::needless_borrow lint still triggers.

Lint Name

needless_borrow

Reproducer

If we modify the original reproducer like this:

unsafe fn repro<T>(slice: *const (i32, [T])) -> usize {
    unsafe { (*slice).1.len() }
}

that triggers roughly the same rustc lint as in the original reproducer:

error: implicit autoref creates a reference to the dereference of a raw pointer
   --> lib.rs:2:14
    |
  2 |     unsafe { (*slice).1.len() }
    |              ^^-----^^^^^^^^^
    |                |
    |                this raw pointer has type `*const (i32, [T])`       
    |
    = note: creating a reference requires the pointer target to be valid and imposes aliasing requirements
note: autoref is being applied to this expression, resulting in: `&[T]`  
   --> lib.rs:2:14
    |
  2 |     unsafe { (*slice).1.len() }
    |              ^^^^^^^^^^
    |
116 |     pub const fn len(&self) -> usize {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: `#[deny(dangerous_implicit_autorefs)]` on by default
help: try using a raw pointer method instead; or if this reference is intentional, make it explicit
    |
  2 |     unsafe { (&(*slice).1).len() }
    |              ++          +

If the suggestion is applied, then clippy complains:

warning: this expression borrows a value the compiler would automatically borrow
 --> lib.rs:2:14
  |
2 |     unsafe { (&(*slice).1).len() }
  |              ^^^^^^^^^^^^^ help: change this to: `(*slice).1`
  |
  = 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

Version

rustc 1.99.0-nightly (be8e82435 2026-07-11)
binary: rustc
commit-hash: be8e82435eb04fbe75ed5286b52735366e160bed
commit-date: 2026-07-11
host: x86_64-pc-windows-msvc
release: 1.99.0-nightly
LLVM version: 22.1.8

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions