2018-10-06 11:18:06 -05:00
|
|
|
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
|
2018-09-15 02:21:58 -05:00
|
|
|
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use crate::rustc::{declare_tool_lint, lint_array};
|
|
|
|
use crate::rustc::hir::*;
|
2018-05-30 03:15:50 -05:00
|
|
|
use crate::utils::{match_qpath, paths, snippet, span_lint_and_then};
|
2018-09-15 11:14:08 -05:00
|
|
|
use crate::rustc_errors::Applicability;
|
2016-10-29 11:56:12 -05:00
|
|
|
|
2018-06-15 02:19:19 -05:00
|
|
|
/// **What it does:** Lint for redundant pattern matching over `Result` or
|
2017-08-09 02:30:56 -05:00
|
|
|
/// `Option`
|
2016-10-29 11:56:12 -05:00
|
|
|
///
|
2017-08-09 02:30:56 -05:00
|
|
|
/// **Why is this bad?** It's more concise and clear to just use the proper
|
|
|
|
/// utility function
|
2016-10-29 11:56:12 -05:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// if let Ok(_) = Ok::<i32, i32>(42) {}
|
|
|
|
/// if let Err(_) = Err::<i32, i32>(42) {}
|
|
|
|
/// if let None = None::<()> {}
|
|
|
|
/// if let Some(_) = Some(42) {}
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The more idiomatic use would be:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// if Ok::<i32, i32>(42).is_ok() {}
|
|
|
|
/// if Err::<i32, i32>(42).is_err() {}
|
|
|
|
/// if None::<()>.is_none() {}
|
|
|
|
/// if Some(42).is_some() {}
|
|
|
|
/// ```
|
|
|
|
///
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2016-10-29 11:56:12 -05:00
|
|
|
pub IF_LET_REDUNDANT_PATTERN_MATCHING,
|
2018-03-28 08:24:26 -05:00
|
|
|
style,
|
2016-10-29 11:56:12 -05:00
|
|
|
"use the proper utility function avoiding an `if let`"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct Pass;
|
|
|
|
|
|
|
|
impl LintPass for Pass {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(IF_LET_REDUNDANT_PATTERN_MATCHING)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
2018-10-11 17:18:58 -05:00
|
|
|
#[allow(clippy::similar_names)]
|
2016-12-07 06:13:40 -06:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
2018-07-12 02:30:57 -05:00
|
|
|
if let ExprKind::Match(ref op, ref arms, MatchSource::IfLetDesugar { .. }) = expr.node {
|
2016-10-29 11:56:12 -05:00
|
|
|
if arms[0].pats.len() == 1 {
|
|
|
|
let good_method = match arms[0].pats[0].node {
|
2018-07-14 17:00:27 -05:00
|
|
|
PatKind::TupleStruct(ref path, ref pats, _) if pats.len() == 1 => {
|
|
|
|
if let PatKind::Wild = pats[0].node {
|
|
|
|
if match_qpath(path, &paths::RESULT_OK) {
|
|
|
|
"is_ok()"
|
|
|
|
} else if match_qpath(path, &paths::RESULT_ERR) {
|
|
|
|
"is_err()"
|
|
|
|
} else if match_qpath(path, &paths::OPTION_SOME) {
|
|
|
|
"is_some()"
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-29 11:56:12 -05:00
|
|
|
} else {
|
2016-12-20 11:21:30 -06:00
|
|
|
return;
|
2016-10-29 11:56:12 -05:00
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-10-29 11:56:12 -05:00
|
|
|
|
2017-08-24 17:21:46 -05:00
|
|
|
PatKind::Path(ref path) if match_qpath(path, &paths::OPTION_NONE) => "is_none()",
|
2016-10-29 11:56:12 -05:00
|
|
|
|
2016-12-20 11:21:30 -06:00
|
|
|
_ => return,
|
2016-10-29 11:56:12 -05:00
|
|
|
};
|
|
|
|
|
2017-09-05 04:33:04 -05:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
IF_LET_REDUNDANT_PATTERN_MATCHING,
|
|
|
|
arms[0].pats[0].span,
|
|
|
|
&format!("redundant pattern matching, consider using `{}`", good_method),
|
|
|
|
|db| {
|
2018-09-28 14:09:57 -05:00
|
|
|
let span = expr.span.to(op.span);
|
2018-09-15 11:14:08 -05:00
|
|
|
db.span_suggestion_with_applicability(
|
2017-09-05 04:33:04 -05:00
|
|
|
span,
|
|
|
|
"try this",
|
|
|
|
format!("if {}.{}", snippet(cx, op.span, "_"), good_method),
|
2018-09-18 12:01:17 -05:00
|
|
|
Applicability::MachineApplicable, // snippet
|
2017-09-05 04:33:04 -05:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2016-10-29 11:56:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|