1
1
use clippy_utils:: diagnostics:: span_lint_and_sugg;
2
2
use clippy_utils:: macros:: { find_assert_args, root_macro_call_first_node, PanicExpn } ;
3
- use clippy_utils:: path_res;
4
3
use clippy_utils:: source:: snippet_with_context;
5
- use clippy_utils:: ty:: { implements_trait , is_copy, is_type_diagnostic_item} ;
4
+ use clippy_utils:: ty:: { has_debug_impl , is_copy, is_type_diagnostic_item} ;
6
5
use clippy_utils:: usage:: local_used_after_expr;
6
+ use clippy_utils:: { is_expr_final_block_expr, path_res} ;
7
7
use rustc_errors:: Applicability ;
8
8
use rustc_hir:: def:: Res ;
9
9
use rustc_hir:: { Expr , ExprKind } ;
@@ -58,6 +58,7 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
58
58
return ;
59
59
}
60
60
}
61
+ let semicolon = if is_expr_final_block_expr ( cx. tcx , e) { ";" } else { "" } ;
61
62
let mut app = Applicability :: MachineApplicable ;
62
63
match method_segment. ident . as_str ( ) {
63
64
"is_ok" if type_suitable_to_unwrap ( cx, substs. type_at ( 1 ) ) => {
@@ -68,8 +69,9 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
68
69
"called `assert!` with `Result::is_ok`" ,
69
70
"replace with" ,
70
71
format ! (
71
- "{}.unwrap()" ,
72
- snippet_with_context( cx, recv. span, condition. span. ctxt( ) , ".." , & mut app) . 0
72
+ "{}.unwrap(){}" ,
73
+ snippet_with_context( cx, recv. span, condition. span. ctxt( ) , ".." , & mut app) . 0 ,
74
+ semicolon
73
75
) ,
74
76
app,
75
77
) ;
@@ -82,8 +84,9 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
82
84
"called `assert!` with `Result::is_err`" ,
83
85
"replace with" ,
84
86
format ! (
85
- "{}.unwrap_err()" ,
86
- snippet_with_context( cx, recv. span, condition. span. ctxt( ) , ".." , & mut app) . 0
87
+ "{}.unwrap_err(){}" ,
88
+ snippet_with_context( cx, recv. span, condition. span. ctxt( ) , ".." , & mut app) . 0 ,
89
+ semicolon
87
90
) ,
88
91
app,
89
92
) ;
@@ -94,13 +97,6 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
94
97
}
95
98
}
96
99
97
- /// This checks whether a given type is known to implement Debug.
98
- fn has_debug_impl < ' tcx > ( cx : & LateContext < ' tcx > , ty : Ty < ' tcx > ) -> bool {
99
- cx. tcx
100
- . get_diagnostic_item ( sym:: Debug )
101
- . map_or ( false , |debug| implements_trait ( cx, ty, debug, & [ ] ) )
102
- }
103
-
104
100
fn type_suitable_to_unwrap < ' tcx > ( cx : & LateContext < ' tcx > , ty : Ty < ' tcx > ) -> bool {
105
101
has_debug_impl ( cx, ty) && !ty. is_unit ( ) && !ty. is_never ( )
106
102
}
0 commit comments