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