Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions clippy_lints/src/matches/collapsible_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,30 +156,32 @@ fn check_arm<'tcx>(
inner_expr.span,
"this `if` can be collapsed into the outer `match`",
|diag| {
let outer_then_open_bracket = outer_then_body
.span
.split_at(1)
.0
.with_leading_whitespace(cx)
.into_span();
let outer_then_closing_bracket = {
let end = outer_then_body.span.shrink_to_hi();
end.with_lo(end.lo() - BytePos(1))
.with_leading_whitespace(cx)
.into_span()
};
let outer_arrow_end = if let Some(outer_guard) = outer_guard {
outer_guard.span.shrink_to_hi()
} else {
outer_pat.span.shrink_to_hi()
};
let (paren_start, inner_if_span, paren_end) = peel_parens(cx, inner_expr.span);
let inner_if = inner_if_span.split_at(2).0;
let mut sugg = vec![
(inner.then.span.shrink_to_lo(), "=> ".to_string()),
(outer_arrow_end.to(outer_then_open_bracket), String::new()),
(outer_then_closing_bracket, String::new()),
];
let mut sugg = vec![(inner.then.span.shrink_to_lo(), "=> ".to_string())];
if matches!(outer_then_body.kind, ExprKind::Block(..)) {
let outer_then_open_bracket = outer_then_body
.span
.split_at(1)
.0
.with_leading_whitespace(cx)
.into_span();
let outer_then_closing_bracket = {
let end = outer_then_body.span.shrink_to_hi();
end.with_lo(end.lo() - BytePos(1))
.with_leading_whitespace(cx)
.into_span()
};
sugg.push((outer_arrow_end.to(outer_then_open_bracket), String::new()));
sugg.push((outer_then_closing_bracket, String::new()));
} else {
sugg.push((outer_arrow_end.until(inner_if), " ".to_string()));
}

if let Some(outer_guard) = outer_guard {
sugg.extend(parens_around(outer_guard));
Expand Down
32 changes: 32 additions & 0 deletions tests/ui/collapsible_match_fixable.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,35 @@ fn issue16875(a: Option<&str>, b: i32) -> i32 {
}
res
}

#[rustfmt::skip]
fn issue16716(n: u32) {
match n {
0 | 1 if false => { println!("hello world"); },
//~^ collapsible_match
_ => (),
}

let opt = Some(1);
let _ = match opt {
Some(s) if s == 1 => { s }
//~^ collapsible_match
_ => 1,
};
}

#[rustfmt::skip]
fn issue16894() {
let a = 5_u8;
let b = a;
_ = match a {
11 => 0,
13 if a > b => { 1 }, _ => 0,
//~^ collapsible_match
};

_ = match a {
11 => 0, 12 if a == b => { 1 }, _ => 0,
//~^ collapsible_match
};
}
32 changes: 32 additions & 0 deletions tests/ui/collapsible_match_fixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,35 @@ fn issue16875(a: Option<&str>, b: i32) -> i32 {
}
res
}

#[rustfmt::skip]
fn issue16716(n: u32) {
match n {
0 | 1 => if false { println!("hello world"); },
//~^ collapsible_match
_ => (),
}

let opt = Some(1);
let _ = match opt {
Some(s) => if s == 1 { s } else { 1 }
//~^ collapsible_match
_ => 1,
};
}

#[rustfmt::skip]
fn issue16894() {
let a = 5_u8;
let b = a;
_ = match a {
11 => 0,
13 => if a > b { 1 } else { 0 }, _ => 0,
//~^ collapsible_match
};

_ = match a {
11 => 0, 12 => if a == b { 1 } else { 0 }, _ => 0,
//~^ collapsible_match
};
}
50 changes: 49 additions & 1 deletion tests/ui/collapsible_match_fixable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,53 @@ LL | res = 1;
LL ~ },
|

error: aborting due to 4 previous errors
error: this `if` can be collapsed into the outer `match`
--> tests/ui/collapsible_match_fixable.rs:52:18
|
LL | 0 | 1 => if false { println!("hello world"); },
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: collapse nested if block
|
LL - 0 | 1 => if false { println!("hello world"); },
LL + 0 | 1 if false => { println!("hello world"); },
|

error: this `if` can be collapsed into the outer `match`
--> tests/ui/collapsible_match_fixable.rs:59:20
|
LL | Some(s) => if s == 1 { s } else { 1 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: collapse nested if block
|
LL - Some(s) => if s == 1 { s } else { 1 }
LL + Some(s) if s == 1 => { s }
|

error: this `if` can be collapsed into the outer `match`
--> tests/ui/collapsible_match_fixable.rs:71:15
|
LL | 13 => if a > b { 1 } else { 0 }, _ => 0,
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: collapse nested if block
|
LL - 13 => if a > b { 1 } else { 0 }, _ => 0,
LL + 13 if a > b => { 1 }, _ => 0,
|

error: this `if` can be collapsed into the outer `match`
--> tests/ui/collapsible_match_fixable.rs:76:24
|
LL | 11 => 0, 12 => if a == b { 1 } else { 0 }, _ => 0,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: collapse nested if block
|
LL - 11 => 0, 12 => if a == b { 1 } else { 0 }, _ => 0,
LL + 11 => 0, 12 if a == b => { 1 }, _ => 0,
|

error: aborting due to 8 previous errors

Loading