Skip to content

Add let_underscore_drop_guard lint. - #164414

Open
theemathas wants to merge 1 commit into
rust-lang:mainfrom
theemathas:let_underscore_drop_guard
Open

theemathas wants to merge 1 commit into
rust-lang:mainfrom
theemathas:let_underscore_drop_guard

Conversation

@theemathas

@theemathas theemathas commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

This lint is similar to the existing let_underscore_lock lint, but applies to DropGuard instead of MutexGuard and similar types. It is deny-by-default, like let_underscore_lock. (Note that the related let_underscore_drop lint is allow-by-default.)

Relevant discussion: #t-libs > Linting `let _ = DropGuard::new(....);`

Note that DropGuard is a newly stabilized type, currently in 1.100 beta, and will be on the 1.100 stable release on November 12.

Tracking issue for DropGuard: #144426

This lint is similar to the existing `let_underscore_lock` lint,
but applies to `DropGuard` instead of `MutexGuard` and similar types.
@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 16, 2026
@rustbot

rustbot commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

r? @petrochenkov

rustbot has assigned @petrochenkov.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 77 candidates
  • Random selection from 18 candidates

@theemathas theemathas added the I-lang-nominated Nominated for discussion during a lang team meeting. label Oct 16, 2026
@theemathas

theemathas commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Lang-nominating to ask for approval for adding a new lint. Also, does it make sense to beta-backport this so that we have the lint at the same time as DropGuard being stabilized? (Although based on zulip discussion, it seems to me like T-libs doesn't want this to be backported, which I find a bit surprising.)

@Jules-Bertholet

Copy link
Copy Markdown
Contributor

As discussed in the Zulip thread, perhaps we want a more general mechanism here?

@scottmcm

scottmcm commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

I agree it makes sense to have a lint to catch this kind of problem with drop guards for the same reason as mutex guards.

@rfcbot fcp merge lang

I see the note that a more general mechanism might be nice, and I don't disagree, but we can always remove any behaviour from this lint in future should the general mechanism be created, so I don't see a need to reject this one. If we end up with another dead name in the removed-lints list eventually, so be it.


EDIT, speaking just for me not as part of the FCP: I wish we had a way to approve the general intent here -- the drop is meaningful so not doing it early is important and thus linting is fruitful -- but leave the details of how that's accomplished out of FCPing so that others could tune the details over time independently. I don't think lang needs to decide "one lint or multiple", for example.

@rust-rfcbot rust-rfcbot added the T-lang Relevant to the language team label Oct 16, 2026
@rust-rfcbot

rust-rfcbot commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

@scottmcm has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
cc @rust-lang/libs-ping: FCP proposed for libs, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Oct 16, 2026
@programmerjake

programmerjake commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Although based on zulip discussion, it seems to me like T-libs doesn't want this to be backported, which I find a bit surprising.

I think this should be backported. Do you have a link to where some of T-libs disagrees? (I happen to be on T-libs but not on the FCP list.)
Edit: the link #t-libs > Linting `let _ = DropGuard::new(....);` @ 💬

@petrochenkov

Copy link
Copy Markdown
Contributor

As discussed in the Zulip thread, perhaps we want a more general mechanism here?

I agree with this, MutexGuard previously, DropGuard now, some other type next, but they are all the same pattern.

@petrochenkov petrochenkov added S-waiting-on-t-lang Status: Awaiting decision from T-lang and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 16, 2026
@traviscross traviscross added the P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang label Oct 21, 2026
/// If you do actually want to drop the expression immediately, then
/// calling `std::mem::drop` on the expression is clearer and helps convey
/// intent.
pub LET_UNDERSCORE_DROP_GUARD,

@traviscross traviscross Oct 21, 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.

Suggested change
pub LET_UNDERSCORE_DROP_GUARD,
pub LET_UNDERSCORE_DROP_GUARDS,

Given our RFC 0344 guidance, this should be named in the plural. (I.e., it reads more naturally as "allow let underscore drop guards".)

Though, here, annoyingly, that puts it at odds with let_underscore_lock. Still, I'd err on the side of following our convention and treating the other one as a miss.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I thought about this already and I disagree. Adding an s there implies that there are multiple drop guards. The thing that's logically "plural" is "let statements", but I don't think we should needlessly add the word "statements" either.

@traviscross traviscross Oct 21, 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.

Thanks. Here's how I'd frame what I understand to be your reading and, conversely, how I read it.

You're, I think, reading this as a transcription of the statement form: let _: DropGuard. In this framing, the part on the right names a token-like part of one construct rather than the class. We see this also in while_true, dyn_drop, unused_unsafe, break_with_label_and_loop, etc. So there's good precedent for it. And it's correct that those should not be pluralized.

The way I was reading it is as follows: "drop guard" is the head noun (i.e., the main thing) and "let underscore" is the attributive modifier (i.e., the adjective, essentially). (In unused_variables, "variables" is the head noun and "unused" is the attributive modifier.) So I read "drop guards (in let underscore position)" as the thing that we're allowing, which leads to my sense of why, in this reading, it should be plural, since it does name the class.

But I buy your reading here too, so I'll check a box.

@traviscross

Copy link
Copy Markdown
Contributor

Thanks @theemathas.

One thing I wanted to check here is that this doesn't fire on let _ = &DropGuard::new(…);, since then the guard isn't actually dropped at the semicolon due to temporary lifetime extension. It's not, in my testing. It might be good to add a test for this.

And I notice, in my testing, that it does fire, as a false positive, when _ refers to a place with a DropGuard (where the guard also isn't dropped). Do we expect any problems from that?

@rfcbot reviewed

@nikomatsakis

Copy link
Copy Markdown
Contributor

@rfcbot reviewed

@tmandry

tmandry commented Oct 21, 2026

Copy link
Copy Markdown
Member

Happy to have this lint. For backport: Sure, but it would be fine not to. It's not lang's call ultimately. I also don't think we need to delay stabilization of DropGuard simply because of this lint not being merged.

@rfcbot reviewed

@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 Oct 21, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

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

@traviscross traviscross added I-lang-radar Items that are on lang's radar and will need eventual work or consideration. and 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 Oct 21, 2026
@theemathas

Copy link
Copy Markdown
Contributor Author

@traviscross

One thing I wanted to check here is that this doesn't fire on let _ = &DropGuard::new(…);, since then the guard isn't actually dropped at the semicolon due to temporary lifetime extension. It's not, in my testing. It might be good to add a test for this.

I'm not particularly convinced that this is a useful test. The lint fires when the type of the _ pattern is equal to DropGuard<_, _>. It doesn't fire here because &DropGuard<_, _> is not DropGuard<_, _>, regardless of whether there's lifetime extension or not. For the same reason, it won't fire on an Option<DropGuard<_, _>> even though there's an argument to be made that maybe it should lint there.

And I notice, in my testing, that it does fire, as a false positive, when _ refers to a place with a DropGuard (where the guard also isn't dropped). Do we expect any problems from that?

This is a preexisting issue. I've filed #164746. I don't think such cases are likely to occur in real code, and the suggestion given by the lint isn't actively bad in some subtle way.

@theemathas theemathas added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Oct 22, 2026
@theemathas

Copy link
Copy Markdown
Contributor Author

Beta-nominating because there appears to be a disagreement within T-libs on whether this should be backported or not.

@rustbot

rustbot commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

beta backport declined as per libs team on Zulip.

@rustbot rustbot removed the beta-nominated Nominated for backporting to the compiler in the beta channel. label Oct 27, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

As mentioned in meeting: we are fine with the feature landing in 1.100 and the lint landing in 1.101. We also would like to see some issues with closure generic explosion (see: fallout from #161702) potentially fixed in the compiler end and @Amanieu in particular had some ideas. Perhaps this could be part of the polymorphisation project as well as some low-hanging fruit to reduce function instantiations.

Either way, this does not need to be backported.

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

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. S-waiting-on-t-lang Status: Awaiting decision from T-lang T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.