Skip to content

needless_borrow: do not contradict dangerous_implicit_autorefs - #14810

Merged
blyxyas merged 1 commit into
rust-lang:masterfrom
samueltardieu:issue-14743
May 23, 2025
Merged

needless_borrow: do not contradict dangerous_implicit_autorefs#14810
blyxyas merged 1 commit into
rust-lang:masterfrom
samueltardieu:issue-14743

Conversation

@samueltardieu

@samueltardieu samueltardieu commented May 15, 2025

Copy link
Copy Markdown
Member

Rust 1.88 introduces the dangerous_implicit_autorefs lint which warns about using implicit autorefs on a place obtained from a raw pointer, as this may create aliasing issues.

Prevent clippy::needless_borrow from triggering in this case, by disabling the lint when taking a reference on a raw pointer dereference. There might be a better way for doing this in the long run with a finer way of distinguish the problematic cases, but this will prevent Clippy from contradicting the compiler in the meantime.

Fixes #14743

changelog: [needless_borrow]: do not contradict the compiler's dangerous_implicit_autorefs lint even though the refererences are not mandatory

@rustbot label +beta-nominated

Summary Notes

Generated by triagebot, see help for how to add more

@rustbot

rustbot commented May 15, 2025

Copy link
Copy Markdown
Collaborator

r? @blyxyas

rustbot has assigned @blyxyas.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties beta-nominated Nominated for backporting to the compiler in the beta channel. labels May 15, 2025
@samueltardieu

samueltardieu commented May 15, 2025

Copy link
Copy Markdown
Member Author

@Urgau Any other cases you might be thinking about that would not be covered here? After our discussion, I switched to ignoring references on dereferences of raw pointers altogether.

@samueltardieu
samueltardieu force-pushed the issue-14743 branch 2 times, most recently from 39ee603 to 969e514 Compare May 15, 2025 12:47
@samueltardieu

samueltardieu commented May 15, 2025

Copy link
Copy Markdown
Member Author

@rustbot note Beta nomination for 1.88

The dangerous_implicit_autorefs compiler lint that we contradict has already entered beta 1.88. We should also target the same version to avoid lints going back and forth.

@blyxyas blyxyas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! ❤️ Just what I assume is a typo.

Comment thread clippy_lints/src/dereference.rs Outdated
);
},
State::DerefedBorrow(state) => {
// Do not suggest removing a non-mandatory `&` in `&*rawptr` in an `unside` context,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a "unsafe" typo?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, fixed!

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 22, 2025
Rust 1.88 introduces the `dangerous_implicit_autorefs` lint which warns
about using implicit autorefs on a place obtained from a raw pointer,
as this may create aliasing issues.

Prevent `clippy::needless_borrow` from triggering in this case,
by disabling the lint when taking a reference on a raw pointer
dereference. There might be a better way for doing this in the long run
with a finer way of distinguish the problematic cases, but this will
prevent Clippy from contradicting the compiler in the meantime.
@samueltardieu
samueltardieu requested a review from blyxyas May 23, 2025 09:13
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 23, 2025

@blyxyas blyxyas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! ❤️

@blyxyas
blyxyas added this pull request to the merge queue May 23, 2025
Merged via the queue into rust-lang:master with commit 7ab910f May 23, 2025
@samueltardieu
samueltardieu deleted the issue-14743 branch May 23, 2025 21:20
@flip1995 flip1995 added beta-accepted Accepted for backporting to the compiler in the beta channel. and removed beta-nominated Nominated for backporting to the compiler in the beta channel. labels Jun 19, 2025
flip1995 pushed a commit to flip1995/rust that referenced this pull request Jun 19, 2025
…ust-lang#14810)

Rust 1.88 introduces the `dangerous_implicit_autorefs` lint which warns
about using implicit autorefs on a place obtained from a raw pointer, as
this may create aliasing issues.

Prevent `clippy::needless_borrow` from triggering in this case, by
disabling the lint when taking a reference on a raw pointer dereference.
There might be a better way for doing this in the long run with a finer
way of distinguish the problematic cases, but this will prevent Clippy
from contradicting the compiler in the meantime.

Fixes rust-lang/rust-clippy#14743

changelog: [`needless_borrow`]: do not contradict the compiler's
`dangerous_implicit_autorefs` lint even though the refererences are not
mandatory

@rustbot label +beta-nominated

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_SUMMARY_START -->

### Summary Notes

- [Beta nomination for
1.88](rust-lang/rust-clippy#14810 (comment))
by [samueltardieu](https://github.com/samueltardieu)

Generated by triagebot, see
[help](https://forge.rust-lang.org/triagebot/note.html) for how to add
more
<!--
TRIAGEBOT_SUMMARY_DATA_START$${"entries_by_url":{"https://github.com/rust-lang/rust-clippy/pull/14810#issuecomment-2883753957":{"title":"Beta
nomination for
1.88","comment_url":"https://github.com/rust-lang/rust-clippy/pull/14810#issuecomment-2883753957","author":"samueltardieu"}}}$$TRIAGEBOT_SUMMARY_DATA_END
-->

<!-- TRIAGEBOT_SUMMARY_END -->
<!-- TRIAGEBOT_END -->
@flip1995

Copy link
Copy Markdown
Member

rust-lang/rust#142725

flip1995 pushed a commit to flip1995/rust that referenced this pull request Jun 19, 2025
…ust-lang#14810)

Rust 1.88 introduces the `dangerous_implicit_autorefs` lint which warns
about using implicit autorefs on a place obtained from a raw pointer, as
this may create aliasing issues.

Prevent `clippy::needless_borrow` from triggering in this case, by
disabling the lint when taking a reference on a raw pointer dereference.
There might be a better way for doing this in the long run with a finer
way of distinguish the problematic cases, but this will prevent Clippy
from contradicting the compiler in the meantime.

Fixes rust-lang/rust-clippy#14743

changelog: [`needless_borrow`]: do not contradict the compiler's
`dangerous_implicit_autorefs` lint even though the refererences are not
mandatory

@rustbot label +beta-nominated

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_SUMMARY_START -->

### Summary Notes

- [Beta nomination for
1.88](rust-lang/rust-clippy#14810 (comment))
by [samueltardieu](https://github.com/samueltardieu)

Generated by triagebot, see
[help](https://forge.rust-lang.org/triagebot/note.html) for how to add
more
<!--
TRIAGEBOT_SUMMARY_DATA_START$${"entries_by_url":{"https://github.com/rust-lang/rust-clippy/pull/14810#issuecomment-2883753957":{"title":"Beta
nomination for
1.88","comment_url":"https://github.com/rust-lang/rust-clippy/pull/14810#issuecomment-2883753957","author":"samueltardieu"}}}$$TRIAGEBOT_SUMMARY_DATA_END
-->

<!-- TRIAGEBOT_SUMMARY_END -->
<!-- TRIAGEBOT_END -->
cuviper pushed a commit to cuviper/rust that referenced this pull request Jun 20, 2025
…ust-lang#14810)

Rust 1.88 introduces the `dangerous_implicit_autorefs` lint which warns
about using implicit autorefs on a place obtained from a raw pointer, as
this may create aliasing issues.

Prevent `clippy::needless_borrow` from triggering in this case, by
disabling the lint when taking a reference on a raw pointer dereference.
There might be a better way for doing this in the long run with a finer
way of distinguish the problematic cases, but this will prevent Clippy
from contradicting the compiler in the meantime.

Fixes rust-lang/rust-clippy#14743

changelog: [`needless_borrow`]: do not contradict the compiler's
`dangerous_implicit_autorefs` lint even though the refererences are not
mandatory

@rustbot label +beta-nominated

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_SUMMARY_START -->

### Summary Notes

- [Beta nomination for
1.88](rust-lang/rust-clippy#14810 (comment))
by [samueltardieu](https://github.com/samueltardieu)

Generated by triagebot, see
[help](https://forge.rust-lang.org/triagebot/note.html) for how to add
more
<!--
TRIAGEBOT_SUMMARY_DATA_START$${"entries_by_url":{"https://github.com/rust-lang/rust-clippy/pull/14810#issuecomment-2883753957":{"title":"Beta
nomination for
1.88","comment_url":"https://github.com/rust-lang/rust-clippy/pull/14810#issuecomment-2883753957","author":"samueltardieu"}}}$$TRIAGEBOT_SUMMARY_DATA_END
-->

<!-- TRIAGEBOT_SUMMARY_END -->
<!-- TRIAGEBOT_END -->
cuviper pushed a commit to cuviper/rust that referenced this pull request Jun 20, 2025
…ust-lang/rust-clippy#14810)

Rust 1.88 introduces the `dangerous_implicit_autorefs` lint which warns
about using implicit autorefs on a place obtained from a raw pointer, as
this may create aliasing issues.

Prevent `clippy::needless_borrow` from triggering in this case, by
disabling the lint when taking a reference on a raw pointer dereference.
There might be a better way for doing this in the long run with a finer
way of distinguish the problematic cases, but this will prevent Clippy
from contradicting the compiler in the meantime.

Fixes rust-lang/rust-clippy#14743

changelog: [`needless_borrow`]: do not contradict the compiler's
`dangerous_implicit_autorefs` lint even though the refererences are not
mandatory

@rustbot label +beta-nominated

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_SUMMARY_START -->


- [Beta nomination for
1.88](rust-lang/rust-clippy#14810 (comment))
by [samueltardieu](https://github.com/samueltardieu)

Generated by triagebot, see
[help](https://forge.rust-lang.org/triagebot/note.html) for how to add
more
<!--
TRIAGEBOT_SUMMARY_DATA_START$${"entries_by_url":{"https://github.com/rust-lang/rust-clippy/pull/14810#issuecomment-2883753957":{"title":"Beta
nomination for
1.88","comment_url":"https://github.com/rust-lang/rust-clippy/pull/14810#issuecomment-2883753957","author":"samueltardieu"}}}$$TRIAGEBOT_SUMMARY_DATA_END
-->

<!-- TRIAGEBOT_SUMMARY_END -->
<!-- TRIAGEBOT_END -->
bors added a commit to rust-lang/rust that referenced this pull request Jun 20, 2025
[beta] backports

- Make the assertion in `Ident::new` debug-only. #140880
- Avoid creating an empty identifer in `Symbol::to_ident_string`. #141318
- Backport rust-lang/stdarch#1818 for 1.88 #142694
- [beta] Clippy backport #142725
    - ICE:
        - rust-lang/rust-clippy#14776
    - Lint contradictions:
        - rust-lang/rust-clippy#14703
        - rust-lang/rust-clippy#14810
    - Smaller (in LoC changes) fixes:
        - rust-lang/rust-clippy#14733
        - rust-lang/rust-clippy#14730

r? cuviper
rust-bors Bot added a commit to rust-lang/rust that referenced this pull request Jun 20, 2025
[beta] backports

- Make the assertion in `Ident::new` debug-only. #140880
- Avoid creating an empty identifer in `Symbol::to_ident_string`. #141318
- Backport rust-lang/stdarch#1818 for 1.88 #142694
- [beta] Clippy backport #142725
    - ICE:
        - rust-lang/rust-clippy#14776
    - Lint contradictions:
        - rust-lang/rust-clippy#14703
        - rust-lang/rust-clippy#14810
    - Smaller (in LoC changes) fixes:
        - rust-lang/rust-clippy#14733
        - rust-lang/rust-clippy#14730

r? cuviper

try-job: x86_64-msvc-ext2
bors added a commit to rust-lang/rust that referenced this pull request Jun 21, 2025
[beta] backports

- Make the assertion in `Ident::new` debug-only. #140880
- Avoid creating an empty identifer in `Symbol::to_ident_string`. #141318
- Backport rust-lang/stdarch#1818 for 1.88 #142694
- [beta] Clippy backport #142725
    - ICE:
        - rust-lang/rust-clippy#14776
    - Lint contradictions:
        - rust-lang/rust-clippy#14703
        - rust-lang/rust-clippy#14810
    - Smaller (in LoC changes) fixes:
        - rust-lang/rust-clippy#14733
        - rust-lang/rust-clippy#14730
-  [win][ci] Update LLVM toolchain used to build LLVM to 20 #140757

r? cuviper
@flip1995 flip1995 removed the beta-accepted Accepted for backporting to the compiler in the beta channel. label Jun 26, 2025
skiefucker added a commit to skiefucker/rust-clippy that referenced this pull request Jul 19, 2026
…orefs`

Add Logic:
If the innermost expression of the borrow is a deref of a raw pointer, and the borrow after multiple Index/Field operations, is used to call a method with `#[rustc_no_implicit_autorefs]`, skip the lint.

Remove the narrow `&*raw_ptr` pattern check added in PR rust-lang#14810. The old guard only matched the literal `&*raw_ptr`.

Add Test `issue_17414` to `tests/ui/needless_borrow.rs` covering:
- `(&(*tuple_slice).field).method()` — the reported case
- `(&*raw_slice).len()` and `(&*raw_slice)[idx]` — direct deref
- nested tuple field access through a raw pointer
- array indexing via `Index::index` on a raw pointer
- trait method dispatch (`dyn T`) through a raw pointer

Problems Left:
- Index Op of slice/array: `(*slice)[0]` can compile, but index method of Index trait has `#[rustc_no_implicit_autorefs]`. (See codes in tests/ui/needless_borrow.rs)
- Is there any other operation between deref raw pointer and method with #[rustc_no_implicit_autorefs]?

changelog: [`needless_borrow`]: do not contradict `dangerous_implicit_autorefs`.
skiefucker added a commit to skiefucker/rust-clippy that referenced this pull request Jul 19, 2026
…orefs`

Add Logic:
If the innermost expression of the borrow is a deref of a raw pointer, and the borrow after multiple Index/Field operations, is used to call a method with `#[rustc_no_implicit_autorefs]`, skip the lint.

Remove the narrow `&*raw_ptr` pattern check added in PR rust-lang#14810. The old guard only matched the literal `&*raw_ptr`.

Add Test `issue_17414` to `tests/ui/needless_borrow.rs` covering:
- `(&(*tuple_slice).field).method()` — the reported case
- `(&*raw_slice).len()` and `(&*raw_slice)[idx]` — direct deref
- nested tuple field access through a raw pointer
- array indexing via `Index::index` on a raw pointer
- trait method dispatch (`dyn T`) through a raw pointer

Problems Left:
- Index Op of slice/array: `(*slice)[0]` can compile, but index method of Index trait has `#[rustc_no_implicit_autorefs]`. (See codes in tests/ui/needless_borrow.rs)
- Is there any other operation between deref raw pointer and method with #[rustc_no_implicit_autorefs]?

changelog: [`needless_borrow`]: do not contradict `dangerous_implicit_autorefs`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dangerous_implicit_autorefs lint suggestion triggers clippy::needless_borrow

4 participants