11use clippy_utils:: diagnostics:: span_lint_and_sugg;
22use clippy_utils:: macros:: { find_assert_args, root_macro_call_first_node, PanicExpn } ;
3- use clippy_utils:: path_res;
43use 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} ;
65use clippy_utils:: usage:: local_used_after_expr;
6+ use clippy_utils:: { is_expr_final_block_expr, path_res} ;
77use rustc_errors:: Applicability ;
88use rustc_hir:: def:: Res ;
99use rustc_hir:: { Expr , ExprKind } ;
@@ -58,6 +58,7 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
5858 return ;
5959 }
6060 }
61+ let semicolon = if is_expr_final_block_expr ( cx. tcx , e) { ";" } else { "" } ;
6162 let mut app = Applicability :: MachineApplicable ;
6263 match method_segment. ident . as_str ( ) {
6364 "is_ok" if type_suitable_to_unwrap ( cx, substs. type_at ( 1 ) ) => {
@@ -68,8 +69,9 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
6869 "called `assert!` with `Result::is_ok`" ,
6970 "replace with" ,
7071 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
7375 ) ,
7476 app,
7577 ) ;
@@ -82,8 +84,9 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
8284 "called `assert!` with `Result::is_err`" ,
8385 "replace with" ,
8486 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
8790 ) ,
8891 app,
8992 ) ;
@@ -94,13 +97,6 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
9497 }
9598}
9699
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-
104100fn type_suitable_to_unwrap < ' tcx > ( cx : & LateContext < ' tcx > , ty : Ty < ' tcx > ) -> bool {
105101 has_debug_impl ( cx, ty) && !ty. is_unit ( ) && !ty. is_never ( )
106102}
0 commit comments