Skip to content

dangerous_implicit_autorefs suggestion is wrong #140721

Open
@wyfo

Description

@wyfo

The following code:

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

gives the two following warnings

warning: implicit autoref creates a reference to the dereference of a raw pointer
 --> src/main.rs:2:14
  |
2 |     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
  |
2 |     unsafe { (&(*slice)[..]).len() }
  |              ++            +

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

I the first warning is quite a duplicate of the second one, and its suggestion is also wrong, as (&(*slice)[..]).len() would still trigger the warning.

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.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions