-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add block_scrutinee lint
#16855
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
Add block_scrutinee lint
#16855
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| use clippy_utils::diagnostics::span_lint_and_then; | ||
| use clippy_utils::source::{indent_of, snippet}; | ||
| use rustc_errors::Applicability; | ||
| use rustc_hir::{BlockCheckMode, Expr, ExprKind, LoopSource}; | ||
| use rustc_lint::{LateContext, LateLintPass}; | ||
| use rustc_session::declare_lint_pass; | ||
| use rustc_span::edition::Edition; | ||
|
|
||
| declare_clippy_lint! { | ||
| /// ### What it does | ||
| /// Warns when a match, if let, or while let scrutinee is wrapped in a block. | ||
| /// This lint only triggers on the 2021 edition and older. | ||
| /// | ||
| /// ### 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. | ||
|
llogiq marked this conversation as resolved.
|
||
| /// | ||
| /// ### Example | ||
| /// ```rust,ignore | ||
| /// if let Some(x) = { my_function() } { .. } | ||
| /// ``` | ||
| #[clippy::version = "1.98.0"] | ||
| pub BLOCK_SCRUTINEE, | ||
| suspicious, | ||
| "warns when the scrutinee is wrapped in a block in older editions" | ||
| } | ||
|
|
||
| declare_lint_pass!(BlockScrutinee => [BLOCK_SCRUTINEE]); | ||
|
|
||
| impl<'tcx> LateLintPass<'tcx> for BlockScrutinee { | ||
| fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { | ||
| if cx.tcx.sess.edition() >= Edition::Edition2024 { | ||
| return; | ||
| } | ||
|
|
||
| let (scrutinee, keyword_fallback) = match expr.kind { | ||
| ExprKind::Match(scrutinee, _, _) => (scrutinee, "`match`"), | ||
| ExprKind::Let(let_expr) => (let_expr.init, "`if let` / `while let`"), | ||
| _ => return, | ||
| }; | ||
|
|
||
| if scrutinee.span.from_expansion() || expr.span.from_expansion() { | ||
| return; | ||
| } | ||
|
|
||
| if let ExprKind::Block(block, _) = scrutinee.kind | ||
| && matches!(block.rules, BlockCheckMode::DefaultBlock) | ||
| && block.stmts.is_empty() | ||
| && let Some(inner_expr) = block.expr | ||
| { | ||
| let inner_snippet = snippet(cx, inner_expr.span, ".."); | ||
|
|
||
| let main_msg = "this scrutinee is wrapped in a block"; | ||
|
|
||
| span_lint_and_then(cx, BLOCK_SCRUTINEE, scrutinee.span, main_msg, |diag| { | ||
| let mut keyword = keyword_fallback; | ||
| let mut outer_span = expr.span; | ||
|
|
||
| if let ExprKind::Let(_) = expr.kind { | ||
| keyword = "`if let`"; | ||
| for (_, node) in cx.tcx.hir_parent_iter(expr.hir_id) { | ||
| if let rustc_hir::Node::Expr(e) = node { | ||
| if let ExprKind::If(..) = e.kind { | ||
| if keyword == "`if let`" { | ||
| outer_span = e.span; | ||
| } | ||
| } else if let ExprKind::Loop(_, _, LoopSource::While, _) = e.kind { | ||
| keyword = "`while let`"; | ||
| outer_span = e.span; | ||
| break; | ||
| } | ||
| } else if matches!(node, rustc_hir::Node::Item(..) | rustc_hir::Node::ImplItem(..)) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| diag.note(format!( | ||
| "temporary values in this block-wrapped scrutinee will be dropped after the body of the {keyword} statement" | ||
| )); | ||
|
|
||
| diag.note("starting with the 2024 edition, temporaries within a block's final expression are dropped immediately at the end of the block"); | ||
|
|
||
| let suggestion_msg = format!("to drop temporaries after the surrounding {keyword}, remove the block"); | ||
|
|
||
| diag.span_suggestion( | ||
| scrutinee.span, | ||
| suggestion_msg, | ||
| inner_snippet.to_string(), | ||
| Applicability::MaybeIncorrect, | ||
| ); | ||
|
|
||
| let indent = indent_of(cx, outer_span).unwrap_or(0); | ||
| let pad = " ".repeat(indent); | ||
|
|
||
| diag.multipart_suggestion( | ||
| "to drop temporaries early, move them to a separate local binding (or update your `Cargo.toml` to the 2024 edition)", | ||
| vec![ | ||
| (outer_span.shrink_to_lo(), format!("let res = {inner_snippet};\n{pad}")), | ||
| (scrutinee.span, "res".to_string()), | ||
| ], | ||
| Applicability::MaybeIncorrect, | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| //@ edition: 2021 | ||
| #![warn(clippy::block_scrutinee)] | ||
| #![allow(clippy::blocks_in_conditions)] | ||
| #![allow(clippy::let_and_return)] | ||
|
|
||
| fn my_function() -> Option<i32> { | ||
| Some(1) | ||
| } | ||
|
|
||
| fn main() { | ||
| if let Some(x) = my_function() { | ||
| //~^ ERROR: this scrutinee is wrapped in a block | ||
| let _ = x; | ||
| } | ||
|
|
||
| match my_function() { | ||
| //~^ ERROR: this scrutinee is wrapped in a block | ||
| Some(1) => println!("one"), | ||
| Some(_) => println!("other"), | ||
| None => println!("none"), | ||
| } | ||
|
|
||
| let mut v = vec![1, 2, 3]; | ||
| while let Some(x) = v.pop() { | ||
| //~^ ERROR: this scrutinee is wrapped in a block | ||
| let _ = x; | ||
| } | ||
|
|
||
| if let Some(x) = my_function() { | ||
| let _ = x; | ||
| } | ||
|
|
||
| if let Some(x) = { | ||
| let _y = 2; | ||
| my_function() | ||
| } { | ||
| let _ = x; | ||
| } | ||
|
|
||
| //~v ERROR: this scrutinee is wrapped in a block | ||
| if let Some(x) = v.pop() { | ||
| let _ = x; | ||
| } | ||
|
|
||
| macro_rules! get_val { | ||
| () => {{ my_function() }}; | ||
| } | ||
| if let Some(x) = get_val!() { | ||
| let _ = x; | ||
| } | ||
|
|
||
| // Test that `unsafe` blocks are ignored | ||
| unsafe fn my_unsafe_fn() -> Option<i32> { | ||
| Some(1) | ||
| } | ||
|
|
||
| if let Some(x) = unsafe { my_unsafe_fn() } { | ||
| let _ = x; | ||
| } | ||
| } |
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,64 @@ | ||
| //@ edition: 2021 | ||
| #![warn(clippy::block_scrutinee)] | ||
| #![allow(clippy::blocks_in_conditions)] | ||
| #![allow(clippy::let_and_return)] | ||
|
|
||
| fn my_function() -> Option<i32> { | ||
| Some(1) | ||
| } | ||
|
|
||
| fn main() { | ||
| let res = my_function(); | ||
| if let Some(x) = res { | ||
| //~^ ERROR: this scrutinee is wrapped in a block | ||
| let _ = x; | ||
| } | ||
|
|
||
| let res = my_function(); | ||
| match res { | ||
| //~^ ERROR: this scrutinee is wrapped in a block | ||
| Some(1) => println!("one"), | ||
| Some(_) => println!("other"), | ||
| None => println!("none"), | ||
| } | ||
|
|
||
| let mut v = vec![1, 2, 3]; | ||
| let res = v.pop(); | ||
| while let Some(x) = res { | ||
| //~^ ERROR: this scrutinee is wrapped in a block | ||
| let _ = x; | ||
| } | ||
|
|
||
| if let Some(x) = my_function() { | ||
| let _ = x; | ||
| } | ||
|
|
||
| if let Some(x) = { | ||
| let _y = 2; | ||
| my_function() | ||
| } { | ||
| let _ = x; | ||
| } | ||
|
|
||
| //~v ERROR: this scrutinee is wrapped in a block | ||
| let res = v.pop(); | ||
| if let Some(x) = res { | ||
| let _ = x; | ||
| } | ||
|
|
||
| macro_rules! get_val { | ||
| () => {{ my_function() }}; | ||
| } | ||
| if let Some(x) = get_val!() { | ||
| let _ = x; | ||
| } | ||
|
|
||
| // Test that `unsafe` blocks are ignored | ||
| unsafe fn my_unsafe_fn() -> Option<i32> { | ||
| Some(1) | ||
| } | ||
|
|
||
| if let Some(x) = unsafe { my_unsafe_fn() } { | ||
| let _ = x; | ||
| } | ||
| } |
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,63 @@ | ||
| //@ edition: 2021 | ||
| #![warn(clippy::block_scrutinee)] | ||
| #![allow(clippy::blocks_in_conditions)] | ||
| #![allow(clippy::let_and_return)] | ||
|
|
||
| fn my_function() -> Option<i32> { | ||
| Some(1) | ||
| } | ||
|
|
||
| fn main() { | ||
| if let Some(x) = { my_function() } { | ||
| //~^ ERROR: this scrutinee is wrapped in a block | ||
| let _ = x; | ||
| } | ||
|
|
||
| match { my_function() } { | ||
| //~^ ERROR: this scrutinee is wrapped in a block | ||
| Some(1) => println!("one"), | ||
| Some(_) => println!("other"), | ||
| None => println!("none"), | ||
| } | ||
|
|
||
| let mut v = vec![1, 2, 3]; | ||
| while let Some(x) = { v.pop() } { | ||
| //~^ ERROR: this scrutinee is wrapped in a block | ||
| let _ = x; | ||
| } | ||
|
|
||
| if let Some(x) = my_function() { | ||
| let _ = x; | ||
| } | ||
|
|
||
| if let Some(x) = { | ||
| let _y = 2; | ||
| my_function() | ||
| } { | ||
| let _ = x; | ||
| } | ||
|
|
||
| //~v ERROR: this scrutinee is wrapped in a block | ||
| if let Some(x) = { | ||
| // We are popping a value | ||
| v.pop() | ||
| } { | ||
| let _ = x; | ||
| } | ||
|
|
||
| macro_rules! get_val { | ||
| () => {{ my_function() }}; | ||
| } | ||
| if let Some(x) = get_val!() { | ||
| let _ = x; | ||
| } | ||
|
|
||
| // Test that `unsafe` blocks are ignored | ||
| unsafe fn my_unsafe_fn() -> Option<i32> { | ||
| Some(1) | ||
| } | ||
|
|
||
| if let Some(x) = unsafe { my_unsafe_fn() } { | ||
| let _ = x; | ||
| } | ||
| } | ||
|
llogiq marked this conversation as resolved.
|
||
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.