Skip to content

Add raw_borrows_via_references lint#138230

Open
obeis wants to merge 2 commits into
rust-lang:mainfrom
obeis:lint-unnecessary-reference
Open

Add raw_borrows_via_references lint#138230
obeis wants to merge 2 commits into
rust-lang:mainfrom
obeis:lint-unnecessary-reference

Conversation

@obeis

@obeis obeis commented Mar 8, 2025

Copy link
Copy Markdown
Contributor

@rustbot

rustbot commented Mar 8, 2025

Copy link
Copy Markdown
Collaborator

r? @Noratrieb

rustbot has assigned @Noratrieb.
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 8, 2025
@rustbot

rustbot commented Mar 8, 2025

Copy link
Copy Markdown
Collaborator

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

@rust-log-analyzer

This comment has been minimized.

@compiler-errors compiler-errors left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This lint seems really specific to a single kind of expression, and there are plenty of other cases where unnecessary references are created when the user is trying to create a raw pointer. Unless this can be greatly generalized, it doesn't really seem worth adding this.

@compiler-errors

Copy link
Copy Markdown
Contributor

r? RalfJung

@rustbot rustbot assigned RalfJung and unassigned Noratrieb Mar 8, 2025
@obeis

obeis commented Mar 8, 2025

Copy link
Copy Markdown
Contributor Author

I agree, and my intention is to generalize this lint to cover all unnecessarily created references. Could you please list other cases that you think should be included? I can update this PR to cover them or create an issue to track these cases and mention that they should be added to the unnecessary_refs lint.

@RalfJung

RalfJung commented Mar 9, 2025 via email

Copy link
Copy Markdown
Member

@rustbot rustbot assigned SparrowLii and unassigned RalfJung Mar 9, 2025
@traviscross traviscross added T-lang Relevant to the language team needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Mar 9, 2025

@SparrowLii SparrowLii 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.

I know little about lints' impls so r? compiler

@rustbot rustbot assigned Nadrieril and unassigned SparrowLii Mar 10, 2025
@RalfJung

Copy link
Copy Markdown
Member

Do we have some people who are our "linting experts"?

@RalfJung

Copy link
Copy Markdown
Member

This lint seems really specific to a single kind of expression, and there are plenty of other cases where unnecessary references are created when the user is trying to create a raw pointer. Unless this can be greatly generalized, it doesn't really seem worth adding this.

Which examples did you have in mind?

A starting point might be to uplift https://rust-lang.github.io/rust-clippy/master/index.html#borrow_as_ptr from cliippy to rustc, as you mention. It is not clear to me what the difference is between that lint and this one.

@traviscross

Copy link
Copy Markdown
Contributor

Do we have some people who are our "linting experts"?

cc @Urgau

@Urgau

Urgau commented Mar 10, 2025

Copy link
Copy Markdown
Member

Happy to take over the review. If @Nadrieril doesn't want to review it of course.

As for the lint it-self, I join @RalfJung that this is lint is currently clippy::borrow_as_ptr, which will need to be dropped from clippy. Look at 1fee1a4 for the changes needed.

As a drive-by, rustc_hir_pretty should not be necessary, a multi-part suggestion should be used instead, with some span manipulation to get the correct spans.

@Nadrieril

Copy link
Copy Markdown
Member

Much appreciated :) r? @Urgau

@rustbot rustbot assigned Urgau and unassigned Nadrieril Mar 11, 2025
@Urgau Urgau added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 12, 2025
Comment thread compiler/rustc_lint/src/unnecessary_refs.rs Outdated
Comment thread compiler/rustc_lint/src/lints.rs Outdated
Comment thread compiler/rustc_lint/src/unnecessary_refs.rs Outdated
Comment thread compiler/rustc_lint/src/unnecessary_refs.rs Outdated
Comment thread compiler/rustc_lint/messages.ftl Outdated
@obeis
obeis force-pushed the lint-unnecessary-reference branch from 1e686f1 to e113827 Compare March 18, 2025 18:26
@joshtriplett

Copy link
Copy Markdown
Member

@rfcbot reviewed

@traviscross traviscross removed the S-waiting-on-t-lang Status: Awaiting decision from T-lang label Jul 15, 2026
@traviscross

Copy link
Copy Markdown
Contributor

@rustbot author

Let me know when it's been updated with the new name and description and I'll resolve the concern.

@rustbot

rustbot commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 15, 2026
@traviscross traviscross removed I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang labels Jul 15, 2026
@obeis
obeis force-pushed the lint-unnecessary-reference branch from b6eda97 to 5da497a Compare July 18, 2026 11:39
@obeis
obeis requested a review from Urgau July 18, 2026 11:39
@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 (such as code changes or more information) from the author. labels Jul 18, 2026
@obeis obeis changed the title Unnecessary references lint Add raw_borrows_via_references lint Jul 18, 2026
@obeis

obeis commented Jul 18, 2026

Copy link
Copy Markdown
Contributor Author

I updated with the new name and description.

CC @traviscross

@obeis
obeis force-pushed the lint-unnecessary-reference branch from 5da497a to cf38d82 Compare July 18, 2026 11:44
@rust-log-analyzer

This comment has been minimized.

@obeis
obeis force-pushed the lint-unnecessary-reference branch from cf38d82 to eaf90b1 Compare July 18, 2026 13:18

@Urgau Urgau 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.

Looks good to me. r=me after FCP finishes

View changes since this review

@Urgau Urgau added S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 18, 2026
Comment thread compiler/rustc_lint/src/raw_borrows_via_references.rs Outdated
Comment thread compiler/rustc_lint/src/raw_borrows_via_references.rs Outdated
@traviscross

Copy link
Copy Markdown
Contributor

Thanks @obeis and @Urgau. And thanks to @kpreid for the naming suggestion.

@rfcbot resolve name-and-scope

@rust-rfcbot rust-rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Jul 20, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

obeis added 2 commits July 20, 2026 20:18
Signed-off-by: Obei Sideg <obei.sideg@gmail.com>
Signed-off-by: Obei Sideg <obei.sideg@gmail.com>
@obeis
obeis force-pushed the lint-unnecessary-reference branch from eaf90b1 to ce2fc22 Compare July 20, 2026 17:19
@rustbot

rustbot commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@obeis
obeis requested a review from traviscross July 20, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. O-windows Operating system: Windows S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lint against expressions that unnecessarily create references