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