Skip to content

Commit

Permalink
Treat more strange pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnTitor committed Jan 19, 2020
1 parent 7e76a19 commit c9f8d03
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
29 changes: 11 additions & 18 deletions clippy_lints/src/if_let_some_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use rustc_errors::Applicability;
use rustc_hir::*;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::BytePos;

declare_clippy_lint! {
/// **What it does:*** Checks for unnecessary `ok()` in if let.
Expand Down Expand Up @@ -46,33 +45,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet {
if let ExprKind::MethodCall(_, ok_span, ref result_types) = op.kind; //check is expr.ok() has type Result<T,E>.ok()
if let PatKind::TupleStruct(QPath::Resolved(_, ref x), ref y, _) = body[0].pat.kind; //get operation
if method_chain_args(op, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized;
let is_result_type = match_type(cx, cx.tables.expr_ty(&result_types[0]), &paths::RESULT);
if print::to_string(print::NO_ANN, |s| s.print_path(x, false)) == "Some" && is_result_type;

then {
let is_result_type = match_type(cx, cx.tables.expr_ty(&result_types[0]), &paths::RESULT);
let mut applicability = Applicability::MachineApplicable;
// ok_span = `ok`
// op.span = `x.parse() . ok()`
// op.span.until(op.span.with_lo(ok_span.lo() - BytePos(1))) = `x.parse() .`
// op.span.with_lo(ok_span.lo() - BytePos(1)) = ` ok()`
// op.span.with_hi(ok_span.hi() - BytePos(1)) = `x.parse() . o`
let some_expr_string = snippet_with_applicability(cx, y[0].span, "", &mut applicability);
let trimmed_ok = snippet_with_applicability(cx, op.span.until(ok_span), "", &mut applicability);
let sugg = format!(
"if let Ok({}) = {}",
some_expr_string,
trimmed_ok.trim().trim_end_matches('.'),
);
if print::to_string(print::NO_ANN, |s| s.print_path(x, false)) == "Some" && is_result_type {
span_lint_and_sugg(
cx,
IF_LET_SOME_RESULT,
expr.span.with_hi(ok_span.hi() + BytePos(2)),
"Matching on `Some` with `ok()` is redundant",
&format!("Consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
sugg,
applicability,
);
}
span_lint_and_sugg(
cx,
IF_LET_SOME_RESULT,
expr.span.with_hi(op.span.hi()),
"Matching on `Some` with `ok()` is redundant",
&format!("Consider matching on `Ok({})` and removing the call to `ok` instead", some_expr_string),
sugg,
applicability,
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/if_let_some_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn str_to_int_ok(x: &str) -> i32 {
#[rustfmt::skip]
fn strange_some_no_else(x: &str) -> i32 {
{
if let Some(y) = x . parse() . ok() {
if let Some(y) = x . parse() . ok () {
return y;
};
0
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/if_let_some_result.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ LL | if let Ok(y) = x.parse() {
error: Matching on `Some` with `ok()` is redundant
--> $DIR/if_let_some_result.rs:24:9
|
LL | if let Some(y) = x . parse() . ok() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | if let Some(y) = x . parse() . ok () {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: Consider matching on `Ok(y)` and removing the call to `ok` instead
|
Expand Down

0 comments on commit c9f8d03

Please sign in to comment.