Skip to content

Commit

Permalink
Auto merge of rust-lang#10470 - Alexendoo:match-single-binding-semico…
Browse files Browse the repository at this point in the history
…lon, r=giraffate

Fix semicolon insertion in `match_single_binding`

changelog: [`match_single_binding`]: Fix missing semicolon after the suggestion

Fixes rust-lang#10447

Also fixes an edge case for unit returning macros in expression contexts:

```rust
f(match 1 {
    _ => println!("foo"),
});
```

would suggest

```rust
f(println!("foo"););
```
  • Loading branch information
bors committed Mar 13, 2023
2 parents e65ad6f + 555f568 commit 945e42f
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 44 deletions.
21 changes: 13 additions & 8 deletions clippy_lints/src/matches/match_single_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clippy_utils::macros::HirNode;
use clippy_utils::source::{indent_of, snippet, snippet_block_with_context, snippet_with_applicability};
use clippy_utils::{get_parent_expr, is_refutable, peel_blocks};
use rustc_errors::Applicability;
use rustc_hir::{Arm, Expr, ExprKind, Node, PatKind};
use rustc_hir::{Arm, Expr, ExprKind, Node, PatKind, StmtKind};
use rustc_lint::LateContext;
use rustc_span::Span;

Expand All @@ -24,22 +24,27 @@ pub(crate) fn check<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], e
let bind_names = arms[0].pat.span;
let match_body = peel_blocks(arms[0].body);
let mut app = Applicability::MaybeIncorrect;
let (snippet_body, from_macro) = snippet_block_with_context(
let mut snippet_body = snippet_block_with_context(
cx,
match_body.span,
arms[0].span.ctxt(),
"..",
Some(expr.span),
&mut app,
);
let mut snippet_body = snippet_body.to_string();
)
.0
.to_string();

// Do we need to add ';' to suggestion ?
if matches!(match_body.kind, ExprKind::Block(..)) {
// macro + expr_ty(body) == ()
if from_macro && cx.typeck_results().expr_ty(match_body).is_unit() {
snippet_body.push(';');
if let Node::Stmt(stmt) = cx.tcx.hir().get_parent(expr.hir_id)
&& let StmtKind::Expr(_) = stmt.kind
&& match match_body.kind {
// We don't need to add a ; to blocks, unless that block is from a macro expansion
ExprKind::Block(block, _) => block.span.from_expansion(),
_ => true,
}
{
snippet_body.push(';');
}

match arms[0].pat.kind {
Expand Down
41 changes: 33 additions & 8 deletions tests/ui/match_single_binding.fixed
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// run-rustfix
#![warn(clippy::match_single_binding)]
#![allow(unused_variables)]
#![allow(clippy::toplevel_ref_arg, clippy::uninlined_format_args)]
#![allow(
unused,
clippy::let_unit_value,
clippy::no_effect,
clippy::toplevel_ref_arg,
clippy::uninlined_format_args
)]

struct Point {
x: i32,
Expand Down Expand Up @@ -109,10 +114,9 @@ fn main() {

// Lint
let x = 1;
println!("Not an array index start");
println!("Not an array index start")
}

#[allow(dead_code)]
fn issue_8723() {
let (mut val, idx) = ("a b", 1);

Expand All @@ -125,16 +129,15 @@ fn issue_8723() {
let _ = val;
}

#[allow(dead_code)]
fn side_effects() {}

fn issue_9575() {
fn side_effects() {}
let _ = || {
side_effects();
println!("Needs curlies");
println!("Needs curlies")
};
}

#[allow(dead_code)]
fn issue_9725(r: Option<u32>) {
let x = r;
match x {
Expand All @@ -146,3 +149,25 @@ fn issue_9725(r: Option<u32>) {
},
};
}

fn issue_10447() -> usize {
();

let a = ();

side_effects();

let b = side_effects();

println!("1");

let c = println!("1");

let in_expr = [
(),
side_effects(),
println!("1"),
];

2
}
55 changes: 49 additions & 6 deletions tests/ui/match_single_binding.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// run-rustfix
#![warn(clippy::match_single_binding)]
#![allow(unused_variables)]
#![allow(clippy::toplevel_ref_arg, clippy::uninlined_format_args)]
#![allow(
unused,
clippy::let_unit_value,
clippy::no_effect,
clippy::toplevel_ref_arg,
clippy::uninlined_format_args
)]

struct Point {
x: i32,
Expand Down Expand Up @@ -127,7 +132,6 @@ fn main() {
}
}

#[allow(dead_code)]
fn issue_8723() {
let (mut val, idx) = ("a b", 1);

Expand All @@ -141,15 +145,14 @@ fn issue_8723() {
let _ = val;
}

#[allow(dead_code)]
fn side_effects() {}

fn issue_9575() {
fn side_effects() {}
let _ = || match side_effects() {
_ => println!("Needs curlies"),
};
}

#[allow(dead_code)]
fn issue_9725(r: Option<u32>) {
match r {
x => match x {
Expand All @@ -162,3 +165,43 @@ fn issue_9725(r: Option<u32>) {
},
};
}

fn issue_10447() -> usize {
match 1 {
_ => (),
}

let a = match 1 {
_ => (),
};

match 1 {
_ => side_effects(),
}

let b = match 1 {
_ => side_effects(),
};

match 1 {
_ => println!("1"),
}

let c = match 1 {
_ => println!("1"),
};

let in_expr = [
match 1 {
_ => (),
},
match 1 {
_ => side_effects(),
},
match 1 {
_ => println!("1"),
},
];

2
}
Loading

0 comments on commit 945e42f

Please sign in to comment.