@@ -4,6 +4,7 @@ use rustc_errors::Applicability;
4
4
use rustc_hir:: * ;
5
5
use rustc_lint:: { LateContext , LateLintPass } ;
6
6
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
7
+ use rustc_span:: BytePos ;
7
8
8
9
declare_clippy_lint ! {
9
10
/// **What it does:*** Checks for unnecessary `ok()` in if let.
@@ -40,22 +41,22 @@ declare_lint_pass!(OkIfLet => [IF_LET_SOME_RESULT]);
40
41
impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for OkIfLet {
41
42
fn check_expr ( & mut self , cx : & LateContext < ' a , ' tcx > , expr : & ' tcx Expr < ' _ > ) {
42
43
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
45
46
if let ExprKind :: MethodCall ( _, ok_span, ref result_types) = op. kind; //check is expr.ok() has type Result<T,E>.ok()
46
47
if let PatKind :: TupleStruct ( QPath :: Resolved ( _, ref x) , ref y, _) = body[ 0 ] . pat. kind; //get operation
47
48
if method_chain_args( op, & [ "ok" ] ) . is_some( ) ; //test to see if using ok() methoduse std::marker::Sized;
48
49
49
50
then {
50
51
let is_result_type = match_type( cx, cx. tables. expr_ty( & result_types[ 0 ] ) , & paths:: RESULT ) ;
51
52
let mut applicability = Applicability :: MachineApplicable ;
53
+ let trimed_ok_span = op. span. until( op. span. with_lo( ok_span. lo( ) - BytePos ( 1 ) ) ) ;
52
54
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) ;
54
56
let mut sugg = format!(
55
57
"if let Ok({}) = {} {}" ,
56
58
some_expr_string,
57
- // FIXME(JohnTitor): this trimming is hacky, probably can improve it
58
- trimmed_ok. trim_matches( '.' ) ,
59
+ trimmed_ok,
59
60
snippet( cx, body[ 0 ] . span, ".." ) ,
60
61
) ;
61
62
if contains_else_clause {
0 commit comments