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 } ;
22use if_chain:: if_chain;
33use rustc_errors:: Applicability ;
44use rustc_hir:: * ;
@@ -42,7 +42,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet {
4242 fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , expr : & ' tcx Expr < ' _ > ) {
4343 if_chain ! { //begin checking variables
4444 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
4646 if let ExprKind :: MethodCall ( _, ok_span, ref result_types) = op. kind; //check is expr.ok() has type Result<T,E>.ok()
4747 if let PatKind :: TupleStruct ( QPath :: Resolved ( _, ref x) , ref y, _) = body[ 0 ] . pat. kind; //get operation
4848 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 {
5353 let trimed_ok_span = op. span. until( op. span. with_lo( ok_span. lo( ) - BytePos ( 1 ) ) ) ;
5454 let some_expr_string = snippet_with_applicability( cx, y[ 0 ] . span, "" , & mut applicability) ;
5555 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({}) = {}" ,
5858 some_expr_string,
5959 trimmed_ok,
60- snippet( cx, body[ 0 ] . span, ".." ) ,
6160 ) ;
62- if contains_else_clause {
63- sugg = format!( "{} else {}" , sugg, snippet( cx, body[ 1 ] . span, ".." ) ) ;
64- }
6561 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 (
6763 cx,
6864 IF_LET_SOME_RESULT ,
6965 expr. span,
7066 "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+ } ,
7475 ) ;
7576 }
7677 }
0 commit comments