Skip to content

dangerous_implicit_autorefs lint suggestion triggers clippy::needless_borrow #140720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wyfo opened this issue May 6, 2025 · 4 comments
Closed
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. L-dangerous_implicit_autorefs Lint: dangerous_implicit_autorefs T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@wyfo
Copy link
Contributor

wyfo commented May 6, 2025

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.

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 6, 2025
@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. L-dangerous_implicit_autorefs Lint: dangerous_implicit_autorefs labels May 6, 2025
@Urgau
Copy link
Member

Urgau commented May 7, 2025

This issue is for you @rust-lang/clippy.

Btw, I'm unable to transfer the issue, would be great to enable triagebot [transfer] feature in your repo.

@wyfo
Copy link
Contributor Author

wyfo commented May 7, 2025

I've opened rust-lang/rust-clippy#14743 instead.

@wyfo wyfo closed this as completed May 7, 2025
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 7, 2025
@fmease
Copy link
Member

fmease commented May 7, 2025

Unless the impl has been made more strict since its introduction, Clippy doesn't need [transfer] enabled for @rustbot transfer rust-clippy to work from inside r-l/r since only the source repo needs that opt-in (the target only needs to have triagebot enabled which rust-clippy has).

@Urgau
Copy link
Member

Urgau commented May 7, 2025

For some reason I though it was the inverse, that rust-lang/rust-clippy needed to have the [transfer] feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. L-dangerous_implicit_autorefs Lint: dangerous_implicit_autorefs T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants