1- use crate :: utils:: { match_qpath, paths, snippet, span_lint_and_then } ;
1+ use crate :: utils:: { match_qpath, paths, snippet, span_lint_and_sugg } ;
22use if_chain:: if_chain;
33use rustc:: hir:: * ;
44use rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
@@ -17,17 +17,17 @@ declare_clippy_lint! {
1717 /// **Known problems:** None.
1818 ///
1919 /// **Example:**
20- ///
21- /// ```rust,ignore
22- /// // Bad
20+ /// ```rust
2321 /// fn foo(fail: bool) -> Result<i32, String> {
2422 /// if fail {
2523 /// Err("failed")?;
2624 /// }
2725 /// Ok(0)
2826 /// }
27+ /// ```
28+ /// Could be written:
2929 ///
30- /// // Good
30+ /// ```rust
3131 /// fn foo(fail: bool) -> Result<i32, String> {
3232 /// if fail {
3333 /// return Err("failed".into());
@@ -57,7 +57,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
5757 if let ExprKind :: Match ( ref match_arg, _, MatchSource :: TryDesugar ) = expr. node;
5858 if let ExprKind :: Call ( ref match_fun, ref try_args) = match_arg. node;
5959 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 ) ;
6161 if let Some ( ref try_arg) = try_args. get( 0 ) ;
6262 if let ExprKind :: Call ( ref err_fun, ref err_args) = try_arg. node;
6363 if let Some ( ref err_arg) = err_args. get( 0 ) ;
@@ -73,19 +73,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
7373 format!( "return Err({}.into())" , snippet( cx, err_arg. span, "_" ) )
7474 } ;
7575
76- span_lint_and_then (
76+ span_lint_and_sugg (
7777 cx,
7878 TRY_ERR ,
7979 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
8984 ) ;
9085 }
9186 }
@@ -97,7 +92,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
9792// its output type.
9893fn find_err_return_type < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , expr : & ' tcx ExprKind ) -> Option < Ty < ' tcx > > {
9994 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) )
10196 } else {
10297 None
10398 }
@@ -109,7 +104,7 @@ fn find_err_return_type_arm<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arm: &'tcx Arm
109104 if let ExprKind :: Ret ( Some ( ref err_ret) ) = arm. body. node;
110105 if let ExprKind :: Call ( ref from_error_path, ref from_error_args) = err_ret. node;
111106 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 ) ;
113108 if let Some ( from_error_arg) = from_error_args. get( 0 ) ;
114109 then {
115110 Some ( cx. tables. expr_ty( from_error_arg) )
0 commit comments