diff --git a/clippy_lints/src/redundant_pattern_matching.rs b/clippy_lints/src/redundant_pattern_matching.rs index b8d1ea3851f..8fea20dba67 100644 --- a/clippy_lints/src/redundant_pattern_matching.rs +++ b/clippy_lints/src/redundant_pattern_matching.rs @@ -49,14 +49,22 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { if let ExprKind::Match(ref op, ref arms, ref match_source) = expr.node { match match_source { MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms), - MatchSource::IfLetDesugar { .. } => find_sugg_for_if_let(cx, expr, op, arms), + MatchSource::IfLetDesugar { contains_else_clause } => { + find_sugg_for_if_let(cx, expr, op, arms, *contains_else_clause) + }, _ => return, } } } } -fn find_sugg_for_if_let<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, op: &P, arms: &HirVec) { +fn find_sugg_for_if_let<'a, 'tcx>( + cx: &LateContext<'a, 'tcx>, + expr: &'tcx Expr, + op: &P, + arms: &HirVec, + has_else: bool, +) { let good_method = match arms[0].pat.node { PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => { if let PatKind::Wild = patterns[0].node { @@ -79,6 +87,8 @@ fn find_sugg_for_if_let<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, _ => return, }; + let maybe_semi = if has_else { "" } else { ";" }; + span_lint_and_then( cx, REDUNDANT_PATTERN_MATCHING, @@ -89,7 +99,7 @@ fn find_sugg_for_if_let<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, db.span_suggestion( span, "try this", - format!("{}.{}", snippet(cx, op.span, "_"), good_method), + format!("{}.{}{}", snippet(cx, op.span, "_"), good_method, maybe_semi), Applicability::MaybeIncorrect, // snippet ); }, diff --git a/tests/ui/redundant_pattern_matching.fixed b/tests/ui/redundant_pattern_matching.fixed new file mode 100644 index 00000000000..776c9444566 --- /dev/null +++ b/tests/ui/redundant_pattern_matching.fixed @@ -0,0 +1,60 @@ +// run-rustfix + +#![warn(clippy::all)] +#![warn(clippy::redundant_pattern_matching)] +#![allow(clippy::unit_arg, clippy::let_unit_value, unused_must_use)] + +fn main() { + Ok::(42).is_ok(); + + Err::(42).is_err(); + + None::<()>.is_none(); + + Some(42).is_some(); + + if Ok::(42).is_ok() {} + + if Err::(42).is_err() {} + + if None::.is_none() {} + + if Some(42).is_some() {} + + if let Ok(x) = Ok::(42) { + println!("{}", x); + } + + Ok::(42).is_ok(); + + Ok::(42).is_err(); + + Err::(42).is_err(); + + Err::(42).is_ok(); + + Some(42).is_some(); + + None::<()>.is_none(); + + let _ = None::<()>.is_none(); + + let _ = Ok::(4).is_ok(); + + let _ = does_something(); + let _ = returns_unit(); + + let opt = Some(false); + let x = opt.is_some(); + takes_bool(x); +} + +fn takes_bool(_: bool) {} + +fn does_something() -> bool { + Ok::(4).is_ok() +} + +fn returns_unit() { + Ok::(4).is_ok(); +} diff --git a/tests/ui/redundant_pattern_matching.rs b/tests/ui/redundant_pattern_matching.rs index 2bea7d8d961..2b2d5b1c1ec 100644 --- a/tests/ui/redundant_pattern_matching.rs +++ b/tests/ui/redundant_pattern_matching.rs @@ -1,6 +1,8 @@ +// run-rustfix + #![warn(clippy::all)] #![warn(clippy::redundant_pattern_matching)] -#![allow(clippy::unit_arg, clippy::let_unit_value)] +#![allow(clippy::unit_arg, clippy::let_unit_value, unused_must_use)] fn main() { if let Ok(_) = Ok::(42) {} @@ -66,12 +68,9 @@ fn main() { let opt = Some(false); let x = if let Some(_) = opt { true } else { false }; takes_bool(x); - let y = if let Some(_) = opt {}; - takes_unit(y); } -fn takes_bool(x: bool) {} -fn takes_unit(x: ()) {} +fn takes_bool(_: bool) {} fn does_something() -> bool { if let Ok(_) = Ok::(4) { diff --git a/tests/ui/redundant_pattern_matching.stderr b/tests/ui/redundant_pattern_matching.stderr index df12b7e169a..5a4a69b1220 100644 --- a/tests/ui/redundant_pattern_matching.stderr +++ b/tests/ui/redundant_pattern_matching.stderr @@ -1,31 +1,31 @@ error: redundant pattern matching, consider using `is_ok()` - --> $DIR/redundant_pattern_matching.rs:6:12 + --> $DIR/redundant_pattern_matching.rs:8:12 | LL | if let Ok(_) = Ok::(42) {} - | -------^^^^^------------------------ help: try this: `Ok::(42).is_ok()` + | -------^^^^^------------------------ help: try this: `Ok::(42).is_ok();` | = note: `-D clippy::redundant-pattern-matching` implied by `-D warnings` error: redundant pattern matching, consider using `is_err()` - --> $DIR/redundant_pattern_matching.rs:8:12 - | -LL | if let Err(_) = Err::(42) {} - | -------^^^^^^------------------------- help: try this: `Err::(42).is_err()` - -error: redundant pattern matching, consider using `is_none()` --> $DIR/redundant_pattern_matching.rs:10:12 | -LL | if let None = None::<()> {} - | -------^^^^---------------- help: try this: `None::<()>.is_none()` +LL | if let Err(_) = Err::(42) {} + | -------^^^^^^------------------------- help: try this: `Err::(42).is_err();` -error: redundant pattern matching, consider using `is_some()` +error: redundant pattern matching, consider using `is_none()` --> $DIR/redundant_pattern_matching.rs:12:12 | +LL | if let None = None::<()> {} + | -------^^^^---------------- help: try this: `None::<()>.is_none();` + +error: redundant pattern matching, consider using `is_some()` + --> $DIR/redundant_pattern_matching.rs:14:12 + | LL | if let Some(_) = Some(42) {} - | -------^^^^^^^-------------- help: try this: `Some(42).is_some()` + | -------^^^^^^^-------------- help: try this: `Some(42).is_some();` error: redundant pattern matching, consider using `is_ok()` - --> $DIR/redundant_pattern_matching.rs:26:5 + --> $DIR/redundant_pattern_matching.rs:28:5 | LL | / match Ok::(42) { LL | | Ok(_) => true, @@ -34,7 +34,7 @@ LL | | }; | |_____^ help: try this: `Ok::(42).is_ok()` error: redundant pattern matching, consider using `is_err()` - --> $DIR/redundant_pattern_matching.rs:31:5 + --> $DIR/redundant_pattern_matching.rs:33:5 | LL | / match Ok::(42) { LL | | Ok(_) => false, @@ -43,7 +43,7 @@ LL | | }; | |_____^ help: try this: `Ok::(42).is_err()` error: redundant pattern matching, consider using `is_err()` - --> $DIR/redundant_pattern_matching.rs:36:5 + --> $DIR/redundant_pattern_matching.rs:38:5 | LL | / match Err::(42) { LL | | Ok(_) => false, @@ -52,7 +52,7 @@ LL | | }; | |_____^ help: try this: `Err::(42).is_err()` error: redundant pattern matching, consider using `is_ok()` - --> $DIR/redundant_pattern_matching.rs:41:5 + --> $DIR/redundant_pattern_matching.rs:43:5 | LL | / match Err::(42) { LL | | Ok(_) => true, @@ -61,7 +61,7 @@ LL | | }; | |_____^ help: try this: `Err::(42).is_ok()` error: redundant pattern matching, consider using `is_some()` - --> $DIR/redundant_pattern_matching.rs:46:5 + --> $DIR/redundant_pattern_matching.rs:48:5 | LL | / match Some(42) { LL | | Some(_) => true, @@ -70,7 +70,7 @@ LL | | }; | |_____^ help: try this: `Some(42).is_some()` error: redundant pattern matching, consider using `is_none()` - --> $DIR/redundant_pattern_matching.rs:51:5 + --> $DIR/redundant_pattern_matching.rs:53:5 | LL | / match None::<()> { LL | | Some(_) => false, @@ -79,7 +79,7 @@ LL | | }; | |_____^ help: try this: `None::<()>.is_none()` error: redundant pattern matching, consider using `is_none()` - --> $DIR/redundant_pattern_matching.rs:56:13 + --> $DIR/redundant_pattern_matching.rs:58:13 | LL | let _ = match None::<()> { | _____________^ @@ -89,25 +89,19 @@ LL | | }; | |_____^ help: try this: `None::<()>.is_none()` error: redundant pattern matching, consider using `is_ok()` - --> $DIR/redundant_pattern_matching.rs:61:20 + --> $DIR/redundant_pattern_matching.rs:63:20 | LL | let _ = if let Ok(_) = Ok::(4) { true } else { false }; | -------^^^^^--------------------------------------------- help: try this: `Ok::(4).is_ok()` error: redundant pattern matching, consider using `is_some()` - --> $DIR/redundant_pattern_matching.rs:67:20 + --> $DIR/redundant_pattern_matching.rs:69:20 | LL | let x = if let Some(_) = opt { true } else { false }; | -------^^^^^^^------------------------------ help: try this: `opt.is_some()` -error: redundant pattern matching, consider using `is_some()` - --> $DIR/redundant_pattern_matching.rs:69:20 - | -LL | let y = if let Some(_) = opt {}; - | -------^^^^^^^--------- help: try this: `opt.is_some()` - error: redundant pattern matching, consider using `is_ok()` - --> $DIR/redundant_pattern_matching.rs:77:12 + --> $DIR/redundant_pattern_matching.rs:76:12 | LL | if let Ok(_) = Ok::(4) { | _____- ^^^^^ @@ -118,7 +112,7 @@ LL | | } | |_____- help: try this: `Ok::(4).is_ok()` error: redundant pattern matching, consider using `is_ok()` - --> $DIR/redundant_pattern_matching.rs:85:12 + --> $DIR/redundant_pattern_matching.rs:84:12 | LL | if let Ok(_) = Ok::(4) { | _____- ^^^^^ @@ -128,5 +122,5 @@ LL | | false LL | | }; | |_____- help: try this: `Ok::(4).is_ok()` -error: aborting due to 16 previous errors +error: aborting due to 15 previous errors