Conversation
|
r? @llogiq rustbot has assigned @llogiq. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
Lintcheck changes for 89131a5
This comment will be updated if you push new changes |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
b6468ef to
eed7c9c
Compare
This comment has been minimized.
This comment has been minimized.
eed7c9c to
475876f
Compare
|
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. |
|
What is the behavior in the current edition? Would it make sense to check whether the block survives a lifetime check if the edition was updated? |
|
Hi @llogiq, thanks for taking a look!
because of this, we should warn unconditionally on older editions to catch these false assumptions without needing deeper type/lifetime checking. Let me know if you think we should still try to narrow the lint down! |
|
|
||
| declare_clippy_lint! { | ||
| /// ### What it does | ||
| /// Warns when a match, if let, or while let scrutinee is wrapped in a block. |
There was a problem hiding this comment.
Should clarify that it only triggers on 2021 edition.
| /// ### Why is this bad? | ||
| /// Prior to the 2024 edition, wrapping the scrutinee in a block did not drop | ||
| /// temporaries before the body executes. |
There was a problem hiding this comment.
| /// ### Why is this bad? | |
| /// Prior to the 2024 edition, wrapping the scrutinee in a block did not drop | |
| /// temporaries before the body executes. | |
| /// ### Why is this bad? | |
| /// It is unusual to write `{ expr }` when you could just have written | |
| /// `expr`, and it is unlikely that anyone would write that for any reason | |
| /// other than wanting temporaries in `expr` to be dropped before executing | |
| /// the body of the `match`/`if let`/`while` statement. However, prior to | |
| /// the 2024 edition, wrapping the scrutinee in a block did not drop | |
| /// temporaries before the body executes. |
| cx, | ||
| BLOCK_SCRUTINEE, | ||
| scrutinee.span, | ||
| "scrutinee is wrapped in a block which will not drop temporaries until the end of the statement in this edition", |
There was a problem hiding this comment.
| "scrutinee is wrapped in a block which will not drop temporaries until the end of the statement in this edition", | |
| "scrutinee is wrapped in a block which will not drop temporaries until the end of the statement on this edition", |
There was a problem hiding this comment.
changed this error message to the new improved one
There was a problem hiding this comment.
I think this error message would look better if changed along these lines.
error: temporary values in this block-wrapped scrutinee will not be dropped until the end of the `if let` statement
--> tests/ui/block_scrutinee.rs:10:22
|
LL | if let Some(x) = { my_function() } {
| ^^^^^^^^^^^^^^^^^
|
= note: this behavior is specific to Rust editions prior to 2024
= note: in Rust 2024, temporaries in a block scrutinee drop immediately before the match arm or block body
= note: `-D clippy::block-scrutinee` implied by `-D warnings`
help: remove the block to yield the same behavior but with cleaner code
|
LL | if let Some(x) = my_function() {
| ~~~~~~~~~~~~~
help: if you intended to drop temporaries early, move the expression to a separate local binding
|
LL | if let Some(x) = { let res = my_function(); res } {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Please include a test that verifies that this does not trigger on the 2024 edition. |
The behavior of
I don't think so. Nobody is going to write |
|
@Darksonn Thanks for suggesting the improvements, I will update the PR to include them. |
475876f to
89131a5
Compare
|
Ok. I think the lint should suggest updating the edition in case the user actually wants the early drop – or moving the block into its own let stmt. |
This PR introduces a new late pass lint to catch scrutinees unnecessarily wrapped in blocks on older editions, preventing unintended behavior regarding temporary lifetimes.
Fixes #16827
changelog: new lint: [
block_scrutinee] to warn on scrutinees wrapped in blocks in older editions