Skip to content

dangerous_implicit_autorefs suggestion is wrong #140721

Closed as not planned
Closed as not planned

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.

See also:

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on May 6, 2025
  2. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on May 6, 2025
  3. Urgau commented on May 7, 2025

    @Urgau
    Member

    The two warnings are not duplicated. They warn on two different part of the expression.

    Applying both suggestions make the warnings disappear.

    unsafe fn slice_ptr_len_because_of_msrv<T>(slice: *const [T]) -> usize {
        unsafe { (&(&(*slice))[..]).len() }
    }
  4. wyfo commented on May 7, 2025

    @wyfo
    ContributorAuthor

    I may be wrong, but the way I understand the first warning is that it indicates that the autoref happens between (*slice)[..] and .len() (hence the fix with (&(*slice)[..]).len()), which is wrong.
    There is only one place where the autoref happens in the expression, so I think there should be only one warning.
    By the way, you can as much additional warning that you want by adding [..].

  5. Jarcho commented on May 7, 2025

    @Jarcho
    Contributor

    The autoref actually happens on the slice operation (*slice)[..]. This is a call to <[T] as Index<RangeFull>>::index which takes &[T]. The compiler warning twice is definitely wrong since the call to len happens on the result of *Index::index(..) which is a deref of a reference.

  6. Urgau commented on May 7, 2025

    @Urgau
    Member

    For the compiler, *Index::index(..) does not exist, more precisely in the HIR the output type of (*slice)[..] is [T], there is no implicit or explicit deref.

    And because the len function is marked with #[rustc_no_implicit_refs] (and we peel the [..], rule 2. peel place expressions) we then see the deref of a raw pointer and lint on it.

    It's unclear to me if it's a bug.

    cc @traviscross

  7. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on May 14, 2025
  8. added a commit that references this issue on May 15, 2025
  9. added a commit that references this issue on May 15, 2025
  10. added a commit that references this issue on May 15, 2025
  11. added a commit that references this issue on May 16, 2025
  12. added
    T-langRelevant to the language team
    I-lang-radarItems that are on lang's radar and will need eventual work or consideration.
    I-lang-nominatedNominated for discussion during a lang team meeting.
    P-lang-drag-2Lang team prioritization drag level 2.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang.
    on Aug 20, 2025
  13. traviscross commented on Oct 15, 2025

    @traviscross
    Contributor

    We talked about this in a lang meeting. Interesting question. Similar to @Urgau, and for the same reasons, it wasn't clear to us that this is a bug.

    At the same time, we're always interested in making diagnostics better and more clear when we can do that without them being any less correct, so if someone has a particular proposal here, in the form of a PR, please nominate that for us along with a detailed analysis, in the context of the rules described in #103735 (comment), about why the change would be correct.

  14. removed
    I-lang-nominatedNominated for discussion during a lang team meeting.
    P-lang-drag-2Lang team prioritization drag level 2.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang.
    on Oct 15, 2025
  15. tmandry commented on Oct 15, 2025

    @tmandry
    Member

    For the compiler, *Index::index(..) does not exist, more precisely in the HIR the output type of (*slice)[..] is [T], there is no implicit or explicit deref.

    Is it correct that the compiler still warns on (*slice)[..] then? (I think the answer is yes.)

    But it's reasonable to say that the compiler doesn't need to lint on both of these expressions at once. As long as it lints on at least one of the expressions as well as both (*slice)[..] and (*slice).len() on their own, the presentation of one or multiple warnings isn't important.

    It's motivating that you only need to apply one of the suggestions; from a UI perspective that indicates that combining the lints would be preferable.

  16. tmandry commented on Oct 15, 2025

    @tmandry
    Member

    In summary, I think there is a potential for improvement here. Since much of the commentary on this issue has been responding to the framing of the suggestion as "wrong", I suggest opening a new issue framed in terms of improving the suggestion(s) and presentation of the lint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.I-lang-radarItems that are on lang's radar and will need eventual work or consideration.L-dangerous_implicit_autorefsLint: dangerous_implicit_autorefsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions