replaced pat_to_string with snippet

This commit is contained in:
Kit Freddura 2016-10-02 14:15:24 -07:00
parent 1188f59102
commit 3573114764
1 changed files with 4 additions and 4 deletions

View File

@ -1,10 +1,10 @@
use rustc::lint::*;
use rustc::hir::*;
use utils::{paths, method_chain_args, span_help_and_lint, match_type};
use utils::{paths, method_chain_args, span_help_and_lint, match_type, snippet_opt};
/// **What it does:*** Checks for unnecessary `ok()` in if let.
///
/// **Why is this bad?** Calling `ok()` in if let is unnecessary, instead match on `Ok(x`
/// **Why is this bad?** Calling `ok()` in if let is unnecessary, instead match on `Ok(pat)`
///
/// **Known problems:** None.
///
@ -19,7 +19,7 @@ use utils::{paths, method_chain_args, span_help_and_lint, match_type};
declare_lint! {
pub IF_LET_SOME_RESULT,
Warn,
"usage of `ok()` in `if let Some(x)` statements is unnecessary, match on `Ok(expr)` instead"
"usage of `ok()` in `if let Some(pat)` statements is unnecessary, match on `Ok(pat)` instead"
}
#[derive(Copy, Clone)]
@ -38,11 +38,11 @@ impl LateLintPass for OkIfLetPass {
let MatchSource::IfLetDesugar { .. } = *source, //test if it is an If Let
let ExprMethodCall(_, _, ref result_types) = op.node, //check is expr.ok() has type Result<T,E>.ok()
let PatKind::TupleStruct(ref x, ref y, _) = body[0].pats[0].node, //get operation
let Some(some_expr_string) = snippet_opt(cx, y[0].span),
let Some(_) = method_chain_args(op, &["ok"]) //test to see if using ok() methoduse std::marker::Sized;
], {
let is_result_type = match_type(cx, cx.tcx.expr_ty(&result_types[0]), &paths::RESULT);
let some_expr_string = print::pat_to_string(&y[0]);
if print::path_to_string(x) == "Some" && is_result_type {
span_help_and_lint(cx, IF_LET_SOME_RESULT, expr.span,
"Matching on `Some` with `ok()` is redundant",