@@ -4,6 +4,7 @@ use rustc_errors::Applicability;
44use rustc_hir:: * ;
55use rustc_lint:: { LateContext , LateLintPass } ;
66use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
7+ use rustc_span:: BytePos ;
78
89declare_clippy_lint ! {
910 /// **What it does:*** Checks for unnecessary `ok()` in if let.
@@ -40,22 +41,22 @@ declare_lint_pass!(OkIfLet => [IF_LET_SOME_RESULT]);
4041impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for OkIfLet {
4142 fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , expr : & ' tcx Expr < ' _ > ) {
4243 if_chain ! { //begin checking variables
43- if let ExprKind :: Match ( ref op, ref body, ref source) = expr. kind; //test if expr is a match
44- if let MatchSource :: IfLetDesugar { contains_else_clause } = * source; //test if it is an If Let
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
4546 if let ExprKind :: MethodCall ( _, ok_span, ref result_types) = op. kind; //check is expr.ok() has type Result<T,E>.ok()
4647 if let PatKind :: TupleStruct ( QPath :: Resolved ( _, ref x) , ref y, _) = body[ 0 ] . pat. kind; //get operation
4748 if method_chain_args( op, & [ "ok" ] ) . is_some( ) ; //test to see if using ok() methoduse std::marker::Sized;
4849
4950 then {
5051 let is_result_type = match_type( cx, cx. tables. expr_ty( & result_types[ 0 ] ) , & paths:: RESULT ) ;
5152 let mut applicability = Applicability :: MachineApplicable ;
53+ let trimed_ok_span = op. span. until( op. span. with_lo( ok_span. lo( ) - BytePos ( 1 ) ) ) ;
5254 let some_expr_string = snippet_with_applicability( cx, y[ 0 ] . span, "" , & mut applicability) ;
53- let trimmed_ok = snippet_with_applicability( cx, op . span . until ( ok_span ) , "" , & mut applicability) ;
55+ let trimmed_ok = snippet_with_applicability( cx, trimed_ok_span , "" , & mut applicability) ;
5456 let mut sugg = format!(
5557 "if let Ok({}) = {} {}" ,
5658 some_expr_string,
57- // FIXME(JohnTitor): this trimming is hacky, probably can improve it
58- trimmed_ok. trim_matches( '.' ) ,
59+ trimmed_ok,
5960 snippet( cx, body[ 0 ] . span, ".." ) ,
6061 ) ;
6162 if contains_else_clause {
0 commit comments