redundant_pattern_matching: make rustfixable

This commit is contained in:
Manish Goregaokar 2019-09-25 10:01:21 -07:00
parent a83a8dccba
commit d28dacb33a
4 changed files with 101 additions and 38 deletions

View File

@ -49,14 +49,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantPatternMatching {
if let ExprKind::Match(ref op, ref arms, ref match_source) = expr.node { if let ExprKind::Match(ref op, ref arms, ref match_source) = expr.node {
match match_source { match match_source {
MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms), 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, _ => return,
} }
} }
} }
} }
fn find_sugg_for_if_let<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, op: &P<Expr>, arms: &HirVec<Arm>) { fn find_sugg_for_if_let<'a, 'tcx>(
cx: &LateContext<'a, 'tcx>,
expr: &'tcx Expr,
op: &P<Expr>,
arms: &HirVec<Arm>,
has_else: bool,
) {
let good_method = match arms[0].pat.node { let good_method = match arms[0].pat.node {
PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => { PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => {
if let PatKind::Wild = patterns[0].node { 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, _ => return,
}; };
let maybe_semi = if has_else { "" } else { ";" };
span_lint_and_then( span_lint_and_then(
cx, cx,
REDUNDANT_PATTERN_MATCHING, 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( db.span_suggestion(
span, span,
"try this", "try this",
format!("{}.{}", snippet(cx, op.span, "_"), good_method), format!("{}.{}{}", snippet(cx, op.span, "_"), good_method, maybe_semi),
Applicability::MaybeIncorrect, // snippet Applicability::MaybeIncorrect, // snippet
); );
}, },

View File

@ -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::<i32, i32>(42).is_ok();
Err::<i32, i32>(42).is_err();
None::<()>.is_none();
Some(42).is_some();
if Ok::<i32, i32>(42).is_ok() {}
if Err::<i32, i32>(42).is_err() {}
if None::<i32>.is_none() {}
if Some(42).is_some() {}
if let Ok(x) = Ok::<i32, i32>(42) {
println!("{}", x);
}
Ok::<i32, i32>(42).is_ok();
Ok::<i32, i32>(42).is_err();
Err::<i32, i32>(42).is_err();
Err::<i32, i32>(42).is_ok();
Some(42).is_some();
None::<()>.is_none();
let _ = None::<()>.is_none();
let _ = Ok::<usize, ()>(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::<i32, i32>(4).is_ok()
}
fn returns_unit() {
Ok::<i32, i32>(4).is_ok();
}

View File

@ -1,6 +1,8 @@
// run-rustfix
#![warn(clippy::all)] #![warn(clippy::all)]
#![warn(clippy::redundant_pattern_matching)] #![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() { fn main() {
if let Ok(_) = Ok::<i32, i32>(42) {} if let Ok(_) = Ok::<i32, i32>(42) {}
@ -66,12 +68,9 @@ fn main() {
let opt = Some(false); let opt = Some(false);
let x = if let Some(_) = opt { true } else { false }; let x = if let Some(_) = opt { true } else { false };
takes_bool(x); takes_bool(x);
let y = if let Some(_) = opt {};
takes_unit(y);
} }
fn takes_bool(x: bool) {} fn takes_bool(_: bool) {}
fn takes_unit(x: ()) {}
fn does_something() -> bool { fn does_something() -> bool {
if let Ok(_) = Ok::<i32, i32>(4) { if let Ok(_) = Ok::<i32, i32>(4) {

View File

@ -1,31 +1,31 @@
error: redundant pattern matching, consider using `is_ok()` 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::<i32, i32>(42) {} LL | if let Ok(_) = Ok::<i32, i32>(42) {}
| -------^^^^^------------------------ help: try this: `Ok::<i32, i32>(42).is_ok()` | -------^^^^^------------------------ help: try this: `Ok::<i32, i32>(42).is_ok();`
| |
= note: `-D clippy::redundant-pattern-matching` implied by `-D warnings` = note: `-D clippy::redundant-pattern-matching` implied by `-D warnings`
error: redundant pattern matching, consider using `is_err()` error: redundant pattern matching, consider using `is_err()`
--> $DIR/redundant_pattern_matching.rs:8:12
|
LL | if let Err(_) = Err::<i32, i32>(42) {}
| -------^^^^^^------------------------- help: try this: `Err::<i32, i32>(42).is_err()`
error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching.rs:10:12 --> $DIR/redundant_pattern_matching.rs:10:12
| |
LL | if let None = None::<()> {} LL | if let Err(_) = Err::<i32, i32>(42) {}
| -------^^^^---------------- help: try this: `None::<()>.is_none()` | -------^^^^^^------------------------- help: try this: `Err::<i32, i32>(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 --> $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) {} 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()` 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::<i32, i32>(42) { LL | / match Ok::<i32, i32>(42) {
LL | | Ok(_) => true, LL | | Ok(_) => true,
@ -34,7 +34,7 @@ LL | | };
| |_____^ help: try this: `Ok::<i32, i32>(42).is_ok()` | |_____^ help: try this: `Ok::<i32, i32>(42).is_ok()`
error: redundant pattern matching, consider using `is_err()` 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::<i32, i32>(42) { LL | / match Ok::<i32, i32>(42) {
LL | | Ok(_) => false, LL | | Ok(_) => false,
@ -43,7 +43,7 @@ LL | | };
| |_____^ help: try this: `Ok::<i32, i32>(42).is_err()` | |_____^ help: try this: `Ok::<i32, i32>(42).is_err()`
error: redundant pattern matching, consider using `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::<i32, i32>(42) { LL | / match Err::<i32, i32>(42) {
LL | | Ok(_) => false, LL | | Ok(_) => false,
@ -52,7 +52,7 @@ LL | | };
| |_____^ help: try this: `Err::<i32, i32>(42).is_err()` | |_____^ help: try this: `Err::<i32, i32>(42).is_err()`
error: redundant pattern matching, consider using `is_ok()` 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::<i32, i32>(42) { LL | / match Err::<i32, i32>(42) {
LL | | Ok(_) => true, LL | | Ok(_) => true,
@ -61,7 +61,7 @@ LL | | };
| |_____^ help: try this: `Err::<i32, i32>(42).is_ok()` | |_____^ help: try this: `Err::<i32, i32>(42).is_ok()`
error: redundant pattern matching, consider using `is_some()` 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 | / match Some(42) {
LL | | Some(_) => true, LL | | Some(_) => true,
@ -70,7 +70,7 @@ LL | | };
| |_____^ help: try this: `Some(42).is_some()` | |_____^ help: try this: `Some(42).is_some()`
error: redundant pattern matching, consider using `is_none()` 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 | / match None::<()> {
LL | | Some(_) => false, LL | | Some(_) => false,
@ -79,7 +79,7 @@ LL | | };
| |_____^ help: try this: `None::<()>.is_none()` | |_____^ help: try this: `None::<()>.is_none()`
error: redundant pattern matching, consider using `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::<()> { LL | let _ = match None::<()> {
| _____________^ | _____________^
@ -89,25 +89,19 @@ LL | | };
| |_____^ help: try this: `None::<()>.is_none()` | |_____^ help: try this: `None::<()>.is_none()`
error: redundant pattern matching, consider using `is_ok()` 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::<usize, ()>(4) { true } else { false }; LL | let _ = if let Ok(_) = Ok::<usize, ()>(4) { true } else { false };
| -------^^^^^--------------------------------------------- help: try this: `Ok::<usize, ()>(4).is_ok()` | -------^^^^^--------------------------------------------- help: try this: `Ok::<usize, ()>(4).is_ok()`
error: redundant pattern matching, consider using `is_some()` 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 }; LL | let x = if let Some(_) = opt { true } else { false };
| -------^^^^^^^------------------------------ help: try this: `opt.is_some()` | -------^^^^^^^------------------------------ 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()` 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::<i32, i32>(4) { LL | if let Ok(_) = Ok::<i32, i32>(4) {
| _____- ^^^^^ | _____- ^^^^^
@ -118,7 +112,7 @@ LL | | }
| |_____- help: try this: `Ok::<i32, i32>(4).is_ok()` | |_____- help: try this: `Ok::<i32, i32>(4).is_ok()`
error: redundant pattern matching, consider using `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::<i32, i32>(4) { LL | if let Ok(_) = Ok::<i32, i32>(4) {
| _____- ^^^^^ | _____- ^^^^^
@ -128,5 +122,5 @@ LL | | false
LL | | }; LL | | };
| |_____- help: try this: `Ok::<i32, i32>(4).is_ok()` | |_____- help: try this: `Ok::<i32, i32>(4).is_ok()`
error: aborting due to 16 previous errors error: aborting due to 15 previous errors