2018-11-27 08:13:57 -06:00
|
|
|
|
use super::utils::{get_arg_name, match_var, remove_blocks, snippet_with_applicability, span_lint_and_sugg};
|
2018-07-19 03:00:54 -05:00
|
|
|
|
use if_chain::if_chain;
|
2019-12-03 17:16:03 -06:00
|
|
|
|
use rustc::declare_lint_pass;
|
2018-12-29 09:04:45 -06:00
|
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
|
use rustc_errors::Applicability;
|
2020-01-06 10:39:50 -06:00
|
|
|
|
use rustc_hir::*;
|
2019-12-03 17:16:03 -06:00
|
|
|
|
use rustc_session::declare_tool_lint;
|
2018-04-19 14:34:31 -05:00
|
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
|
/// **What it does:** Checks for matches being used to destructure a single-variant enum
|
|
|
|
|
/// or tuple struct where a `let` will suffice.
|
|
|
|
|
///
|
|
|
|
|
/// **Why is this bad?** Just readability – `let` doesn't nest, whereas a `match` does.
|
|
|
|
|
///
|
|
|
|
|
/// **Known problems:** None.
|
|
|
|
|
///
|
|
|
|
|
/// **Example:**
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// enum Wrapper {
|
|
|
|
|
/// Data(i32),
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// let wrapper = Wrapper::Data(42);
|
|
|
|
|
///
|
|
|
|
|
/// let data = match wrapper {
|
|
|
|
|
/// Wrapper::Data(i) => i,
|
|
|
|
|
/// };
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// The correct use would be:
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// enum Wrapper {
|
|
|
|
|
/// Data(i32),
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// let wrapper = Wrapper::Data(42);
|
|
|
|
|
/// let Wrapper::Data(data) = wrapper;
|
|
|
|
|
/// ```
|
2018-04-19 14:34:31 -05:00
|
|
|
|
pub INFALLIBLE_DESTRUCTURING_MATCH,
|
|
|
|
|
style,
|
2020-01-06 00:30:43 -06:00
|
|
|
|
"a `match` statement with a single infallible arm instead of a `let`"
|
2018-04-19 14:34:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
|
declare_lint_pass!(InfallibleDestructingMatch => [INFALLIBLE_DESTRUCTURING_MATCH]);
|
2018-04-19 14:34:31 -05:00
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InfallibleDestructingMatch {
|
2019-12-27 01:12:26 -06:00
|
|
|
|
fn check_local(&mut self, cx: &LateContext<'a, 'tcx>, local: &'tcx Local<'_>) {
|
2018-04-19 14:34:31 -05:00
|
|
|
|
if_chain! {
|
|
|
|
|
if let Some(ref expr) = local.init;
|
2019-09-27 10:16:06 -05:00
|
|
|
|
if let ExprKind::Match(ref target, ref arms, MatchSource::Normal) = expr.kind;
|
2019-09-25 14:00:17 -05:00
|
|
|
|
if arms.len() == 1 && arms[0].guard.is_none();
|
2019-09-27 10:16:06 -05:00
|
|
|
|
if let PatKind::TupleStruct(QPath::Resolved(None, ref variant_name), ref args, _) = arms[0].pat.kind;
|
2018-04-19 14:34:31 -05:00
|
|
|
|
if args.len() == 1;
|
|
|
|
|
if let Some(arg) = get_arg_name(&args[0]);
|
|
|
|
|
let body = remove_blocks(&arms[0].body);
|
|
|
|
|
if match_var(body, arg);
|
|
|
|
|
|
|
|
|
|
then {
|
2018-11-27 08:13:57 -06:00
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
2018-04-19 14:34:31 -05:00
|
|
|
|
span_lint_and_sugg(
|
|
|
|
|
cx,
|
|
|
|
|
INFALLIBLE_DESTRUCTURING_MATCH,
|
|
|
|
|
local.span,
|
2020-01-06 00:30:43 -06:00
|
|
|
|
"you seem to be trying to use `match` to destructure a single infallible pattern. \
|
2018-04-19 14:34:31 -05:00
|
|
|
|
Consider using `let`",
|
|
|
|
|
"try this",
|
|
|
|
|
format!(
|
|
|
|
|
"let {}({}) = {};",
|
2018-11-27 08:13:57 -06:00
|
|
|
|
snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
|
|
|
|
|
snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
|
|
|
|
|
snippet_with_applicability(cx, target.span, "..", &mut applicability),
|
2018-04-19 14:34:31 -05:00
|
|
|
|
),
|
2018-11-27 08:13:57 -06:00
|
|
|
|
applicability,
|
2018-04-19 14:34:31 -05:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|