1
- use crate :: utils:: { match_type, method_chain_args, paths, snippet , snippet_with_applicability, span_lint_and_sugg } ;
1
+ use crate :: utils:: { match_type, method_chain_args, paths, snippet_with_applicability, span_lint_and_then } ;
2
2
use if_chain:: if_chain;
3
3
use rustc_errors:: Applicability ;
4
4
use rustc_hir:: * ;
@@ -42,7 +42,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet {
42
42
fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , expr : & ' tcx Expr < ' _ > ) {
43
43
if_chain ! { //begin checking variables
44
44
if let ExprKind :: Match ( ref op, ref body, source) = expr. kind; //test if expr is a match
45
- if let MatchSource :: IfLetDesugar { contains_else_clause } = source; //test if it is an If Let
45
+ if let MatchSource :: IfLetDesugar { .. } = source; //test if it is an If Let
46
46
if let ExprKind :: MethodCall ( _, ok_span, ref result_types) = op. kind; //check is expr.ok() has type Result<T,E>.ok()
47
47
if let PatKind :: TupleStruct ( QPath :: Resolved ( _, ref x) , ref y, _) = body[ 0 ] . pat. kind; //get operation
48
48
if method_chain_args( op, & [ "ok" ] ) . is_some( ) ; //test to see if using ok() methoduse std::marker::Sized;
@@ -53,24 +53,25 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet {
53
53
let trimed_ok_span = op. span. until( op. span. with_lo( ok_span. lo( ) - BytePos ( 1 ) ) ) ;
54
54
let some_expr_string = snippet_with_applicability( cx, y[ 0 ] . span, "" , & mut applicability) ;
55
55
let trimmed_ok = snippet_with_applicability( cx, trimed_ok_span, "" , & mut applicability) ;
56
- let mut sugg = format!(
57
- "if let Ok({}) = {} {} " ,
56
+ let sugg = format!(
57
+ "if let Ok({}) = {}" ,
58
58
some_expr_string,
59
59
trimmed_ok,
60
- snippet( cx, body[ 0 ] . span, ".." ) ,
61
60
) ;
62
- if contains_else_clause {
63
- sugg = format!( "{} else {}" , sugg, snippet( cx, body[ 1 ] . span, ".." ) ) ;
64
- }
65
61
if print:: to_string( print:: NO_ANN , |s| s. print_path( x, false ) ) == "Some" && is_result_type {
66
- span_lint_and_sugg (
62
+ span_lint_and_then (
67
63
cx,
68
64
IF_LET_SOME_RESULT ,
69
65
expr. span,
70
66
"Matching on `Some` with `ok()` is redundant" ,
71
- & format!( "Consider matching on `Ok({})` and removing the call to `ok` instead" , some_expr_string) ,
72
- sugg,
73
- applicability,
67
+ |db| {
68
+ db. span_suggestion(
69
+ expr. span. shrink_to_lo( ) . to( ok_span. with_hi( ok_span. hi( ) + BytePos ( 2 ) ) ) ,
70
+ & format!( "Consider matching on `Ok({})` and removing the call to `ok` instead" , some_expr_string) ,
71
+ sugg,
72
+ applicability,
73
+ ) ;
74
+ } ,
74
75
) ;
75
76
}
76
77
}
0 commit comments