1
- use crate :: utils:: { match_qpath, paths, snippet, span_lint_and_then } ;
1
+ use crate :: utils:: { match_qpath, paths, snippet, span_lint_and_sugg } ;
2
2
use if_chain:: if_chain;
3
3
use rustc:: hir:: * ;
4
4
use rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
@@ -17,17 +17,17 @@ declare_clippy_lint! {
17
17
/// **Known problems:** None.
18
18
///
19
19
/// **Example:**
20
- ///
21
- /// ```rust,ignore
22
- /// // Bad
20
+ /// ```rust
23
21
/// fn foo(fail: bool) -> Result<i32, String> {
24
22
/// if fail {
25
23
/// Err("failed")?;
26
24
/// }
27
25
/// Ok(0)
28
26
/// }
27
+ /// ```
28
+ /// Could be written:
29
29
///
30
- /// // Good
30
+ /// ```rust
31
31
/// fn foo(fail: bool) -> Result<i32, String> {
32
32
/// if fail {
33
33
/// return Err("failed".into());
@@ -57,7 +57,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
57
57
if let ExprKind :: Match ( ref match_arg, _, MatchSource :: TryDesugar ) = expr. node;
58
58
if let ExprKind :: Call ( ref match_fun, ref try_args) = match_arg. node;
59
59
if let ExprKind :: Path ( ref match_fun_path) = match_fun. node;
60
- if match_qpath( match_fun_path, & [ "std" , "ops" , "Try" , "into_result" ] ) ;
60
+ if match_qpath( match_fun_path, & paths :: TRY_INTO_RESULT ) ;
61
61
if let Some ( ref try_arg) = try_args. get( 0 ) ;
62
62
if let ExprKind :: Call ( ref err_fun, ref err_args) = try_arg. node;
63
63
if let Some ( ref err_arg) = err_args. get( 0 ) ;
@@ -73,19 +73,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
73
73
format!( "return Err({}.into())" , snippet( cx, err_arg. span, "_" ) )
74
74
} ;
75
75
76
- span_lint_and_then (
76
+ span_lint_and_sugg (
77
77
cx,
78
78
TRY_ERR ,
79
79
expr. span,
80
- & format!( "confusing error return, consider using `{}`" , suggestion) ,
81
- |db| {
82
- db. span_suggestion(
83
- expr. span,
84
- "try this" ,
85
- suggestion,
86
- Applicability :: MaybeIncorrect
87
- ) ;
88
- } ,
80
+ "returning an `Err(_)` with the `?` operator" ,
81
+ "try this" ,
82
+ suggestion,
83
+ Applicability :: MaybeIncorrect
89
84
) ;
90
85
}
91
86
}
@@ -97,7 +92,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
97
92
// its output type.
98
93
fn find_err_return_type < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , expr : & ' tcx ExprKind ) -> Option < Ty < ' tcx > > {
99
94
if let ExprKind :: Match ( _, ref arms, MatchSource :: TryDesugar ) = expr {
100
- arms. iter ( ) . filter_map ( |ty| find_err_return_type_arm ( cx, ty) ) . nth ( 0 )
95
+ arms. iter ( ) . find_map ( |ty| find_err_return_type_arm ( cx, ty) )
101
96
} else {
102
97
None
103
98
}
@@ -109,7 +104,7 @@ fn find_err_return_type_arm<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arm: &'tcx Arm
109
104
if let ExprKind :: Ret ( Some ( ref err_ret) ) = arm. body. node;
110
105
if let ExprKind :: Call ( ref from_error_path, ref from_error_args) = err_ret. node;
111
106
if let ExprKind :: Path ( ref from_error_fn) = from_error_path. node;
112
- if match_qpath( from_error_fn, & [ "std" , "ops" , "Try" , "from_error" ] ) ;
107
+ if match_qpath( from_error_fn, & paths :: TRY_FROM_ERROR ) ;
113
108
if let Some ( from_error_arg) = from_error_args. get( 0 ) ;
114
109
then {
115
110
Some ( cx. tables. expr_ty( from_error_arg) )
0 commit comments