Skip to content

new lint: unnecessary_box_pin - #13598

Open
y21 wants to merge 1 commit into
rust-lang:masterfrom
y21:unnecessary_pin_box
Open

y21 wants to merge 1 commit into
rust-lang:masterfrom
y21:unnecessary_pin_box

Conversation

@y21

@y21 y21 commented Oct 23, 2024

Copy link
Copy Markdown
Member

This adds a new lint that looks for Box::pin calls that could be replaced with the pin! macro.
I've found that there's a lot of potential for false positives here, so for now this is fairly conservative and only lints if all uses go through .as_mut(). It does mean that there's a few false negatives I can think of, but hopefully no FPs.

changelog: [unnecessary_box_pin]: new lint

@rustbot

rustbot commented Oct 23, 2024

Copy link
Copy Markdown
Collaborator

r? @Alexendoo

rustbot has assigned @Alexendoo.
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 the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 23, 2024
Comment thread README.md Outdated
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.

[There are over 700 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
[There are over 750 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)

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.

Should this (and the similar line in book/src/README.md) be captured in this patch?

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.

Wdym by captured? cargo dev update_lints automatically updates this and fails CI if this lint count is not up to date.

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.

Ok, I didn't know that the CI would fail. I would consider this a problem though, as several proposed PR contain this mandatory change right now, while others don't and might need it if they are to be merged first.

Wouldn't it be better if this was updated at release time rather than when adding a lint? (and yes, this is unrelated to this particular PR)

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.

I don't think it's as much of a problem. Whichever PR gets merged first "wins" and all other PRs can just drop their change during the rebase. New lint PRs getting merged often create merge conflicts on most other lint PRs anyway so you often have to rebase either way.
Old PRs that don't have the lint count updated but need it will still fail when r+'d as bors makes sure that CI still passes with the PR integrated into master, and fixing it is very easy with cargo dev update_lints.

It also happens rather infrequently (last change on that line was 10 months ago) that I don't know if it's worth complicating this process (this number replacement is very little code in update_lints which already handles all other kinds of content generation).

But if you want to discuss it more, I'd suggest opening a thread on Zulip

@bors

bors commented Nov 2, 2024

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #13376) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot

This comment has been minimized.

@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 Mar 31, 2025
@y21 y21 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 Apr 3, 2025
@Jarcho

Jarcho commented Sep 17, 2025

Copy link
Copy Markdown
Contributor

Ping @Alexendoo from triage. This is still waiting on a review.

@Jarcho

Jarcho commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Ping @y21 do you still have time to work on this?

r? Jarcho

@rustbot rustbot assigned Jarcho and unassigned Alexendoo Aug 30, 2026
@y21

y21 commented Sep 11, 2026

Copy link
Copy Markdown
Member Author

Ping @y21 do you still have time to work on this?

Oh yeah, sorry. This is gonna need to be rebased and probably cleaned up a little from being over a year behind master but I'll try to do that over the weekend.

@y21
y21 force-pushed the unnecessary_pin_box branch from 68e9a60 to 1fcc911 Compare September 12, 2026 00:35
@rustbot rustbot added the needs-fcp PRs that add, remove, or rename lints and need an FCP label Sep 12, 2026
@rustbot

rustbot commented Sep 12, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master 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.

@y21
y21 force-pushed the unnecessary_pin_box branch from 1fcc911 to fed638d Compare September 12, 2026 00:37
@Jarcho

Jarcho commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

@rustbot label lint-nominated

@rustbot rustbot added the lint-nominated Create an FCP-thread on Zulip for this PR label Sep 20, 2026
@rustbot

rustbot commented Sep 20, 2026

Copy link
Copy Markdown
Collaborator

This lint has been nominated for inclusion.

A FCP topic has been created on Zulip.

match cx.tcx.parent_hir_node(expr.hir_id) {
Node::Expr(as_mut_expr)
if let ExprKind::MethodCall(segment, recv, [], span) = as_mut_expr.kind
&& recv.hir_id == expr.hir_id

@Jarcho Jarcho Sep 20, 2026 •

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 isn't needed. There's only one expression which can be a direct child.

View changes since the review

for_each_local_use_after_expr(cx, local_id, expr.hir_id, |expr| {
if check_pin_box_use(cx, expr, true, enclosing_body).is_continue()
// Make sure the `Pin` is not captured by a closure.
&& cx.tcx.hir_enclosing_body_owner(expr.hir_id) == enclosing_body

@Jarcho Jarcho Sep 20, 2026 •

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.

Why are closures not fine?

View changes since the review

fn check_pin_box_use<'tcx>(
cx: &LateContext<'tcx>,
expr: &'tcx Expr<'tcx>,
moved: bool,

@Jarcho Jarcho Sep 20, 2026 •

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 just makes things harder to read. Inlining just the relevant part into the for_each_local closure would be nicer. It adds two lines to the closure, but getting rid of the mode switch is worth a fair bit more.

for_each_local_use_after_expr(cx, local_id, expr.hir_id, |expr| {
    if let Node::Expr(parent) = cx.tcx.parent_hir_node(expr.hir_id)
        && let ExprKind::MethodCall(segment, _, [], _) = parent.kind
        && segment.ident.name == sym::as_mut
        // Make sure the `Pin` is not captured by a closure.
        && cx.tcx.hir_enclosing_body_owner(expr.hir_id) == enclosing_body
    {
        ControlFlow::Continue(())
    } else {
        ControlFlow::Break(())
    }
})?;

View changes since the review

@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 Sep 20, 2026
@rustbot

rustbot commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #16953) made this pull request unmergeable. Please resolve the merge conflicts.

This branch has not been deployed

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

Labels

lint-nominated Create an FCP-thread on Zulip for this PR needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants