Repository navigation
Add let_underscore_drop_guard lint.
#164414
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -208,6 +208,7 @@ symbols! { | |
| Deref, | ||
| DispatchFromDyn, | ||
| Display, | ||
| DropGuard, | ||
| Dual, | ||
| DualOnly, | ||
| Dualv, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| use std::mem::DropGuard; | ||
|
|
||
| struct Struct<T> { | ||
| a: T, | ||
| } | ||
|
|
||
| fn main() { | ||
| let _ = DropGuard::new((), |_| {}); | ||
| //~^ERROR non-binding let on a `DropGuard` | ||
| _ = DropGuard::new((), |_| {}); | ||
| //~^ERROR non-binding let on a `DropGuard` | ||
|
|
||
| let (_, _) = (DropGuard::new((), |_| {}), 1); | ||
| //~^ERROR non-binding let on a `DropGuard` | ||
| (_, _) = (DropGuard::new((), |_| {}), 1); | ||
| //~^ERROR non-binding let on a `DropGuard` | ||
|
|
||
| let (_a, Struct { a: _ }) = (0, Struct { a: DropGuard::new((), |_| {}) }); | ||
| //~^ERROR non-binding let on a `DropGuard` | ||
| let _b; | ||
| (_b, Struct { a: _ }) = (0, Struct { a: DropGuard::new((), |_| {}) }); | ||
| //~^ERROR non-binding let on a `DropGuard` | ||
| } |
86 changes: 86 additions & 0 deletions
86
tests/ui/lint/let_underscore/let_underscore_drop_guard.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| error: non-binding let on a `DropGuard` | ||
| --> $DIR/let_underscore_drop_guard.rs:8:9 | ||
| | | ||
| LL | let _ = DropGuard::new((), |_| {}); | ||
| | ^ this `DropGuard` is not assigned to a binding and is immediately dropped | ||
| | | ||
| = note: `#[deny(let_underscore_drop_guard)]` on by default | ||
| help: consider binding to an unused variable to avoid immediately dropping the value | ||
| | | ||
| LL | let _unused = DropGuard::new((), |_| {}); | ||
| | ++++++ | ||
| help: consider immediately dropping the value | ||
| | | ||
| LL - let _ = DropGuard::new((), |_| {}); | ||
| LL + drop(DropGuard::new((), |_| {})); | ||
| | | ||
|
|
||
| error: non-binding let on a `DropGuard` | ||
| --> $DIR/let_underscore_drop_guard.rs:10:5 | ||
| | | ||
| LL | _ = DropGuard::new((), |_| {}); | ||
| | ^ this `DropGuard` is not assigned to a binding and is immediately dropped | ||
| | | ||
| help: consider binding to an unused variable to avoid immediately dropping the value | ||
| | | ||
| LL - _ = DropGuard::new((), |_| {}); | ||
| LL + let _unused = DropGuard::new((), |_| {}); | ||
| | | ||
| help: consider immediately dropping the value | ||
| | | ||
| LL - _ = DropGuard::new((), |_| {}); | ||
| LL + drop(DropGuard::new((), |_| {})); | ||
| | | ||
|
|
||
| error: non-binding let on a `DropGuard` | ||
| --> $DIR/let_underscore_drop_guard.rs:13:10 | ||
| | | ||
| LL | let (_, _) = (DropGuard::new((), |_| {}), 1); | ||
| | ^ this `DropGuard` is not assigned to a binding and is immediately dropped | ||
| | | ||
| = help: consider immediately dropping the value using `drop(..)` after the `let` statement | ||
| help: consider binding to an unused variable to avoid immediately dropping the value | ||
| | | ||
| LL | let (_unused, _) = (DropGuard::new((), |_| {}), 1); | ||
| | ++++++ | ||
|
|
||
| error: non-binding let on a `DropGuard` | ||
| --> $DIR/let_underscore_drop_guard.rs:15:6 | ||
| | | ||
| LL | (_, _) = (DropGuard::new((), |_| {}), 1); | ||
| | ^ this `DropGuard` is not assigned to a binding and is immediately dropped | ||
| | | ||
| help: consider binding to an unused variable to avoid immediately dropping the value | ||
| --> $DIR/let_underscore_drop_guard.rs:15:6 | ||
| | | ||
| LL | (_, _) = (DropGuard::new((), |_| {}), 1); | ||
| | ^ | ||
| = help: consider immediately dropping the value using `drop(..)` after the `let` statement | ||
|
|
||
| error: non-binding let on a `DropGuard` | ||
| --> $DIR/let_underscore_drop_guard.rs:18:26 | ||
| | | ||
| LL | let (_a, Struct { a: _ }) = (0, Struct { a: DropGuard::new((), |_| {}) }); | ||
| | ^ this `DropGuard` is not assigned to a binding and is immediately dropped | ||
| | | ||
| = help: consider immediately dropping the value using `drop(..)` after the `let` statement | ||
| help: consider binding to an unused variable to avoid immediately dropping the value | ||
| | | ||
| LL | let (_a, Struct { a: _unused }) = (0, Struct { a: DropGuard::new((), |_| {}) }); | ||
| | ++++++ | ||
|
|
||
| error: non-binding let on a `DropGuard` | ||
| --> $DIR/let_underscore_drop_guard.rs:21:22 | ||
| | | ||
| LL | (_b, Struct { a: _ }) = (0, Struct { a: DropGuard::new((), |_| {}) }); | ||
| | ^ this `DropGuard` is not assigned to a binding and is immediately dropped | ||
| | | ||
| help: consider binding to an unused variable to avoid immediately dropping the value | ||
| --> $DIR/let_underscore_drop_guard.rs:21:22 | ||
| | | ||
| LL | (_b, Struct { a: _ }) = (0, Struct { a: DropGuard::new((), |_| {}) }); | ||
| | ^ | ||
| = help: consider immediately dropping the value using `drop(..)` after the `let` statement | ||
|
|
||
| error: aborting due to 6 previous errors | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 inwhile_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.