@@ -19,6 +19,9 @@ declare_clippy_lint! {
1919 /// ### Why is this bad?
2020 /// An assertion failure cannot output an useful message of the error.
2121 ///
22+ /// ### Known problems
23+ /// The suggested replacement decreases the readability of code and log output.
24+ ///
2225 /// ### Example
2326 /// ```rust,ignore
2427 /// # let r = Ok::<_, ()>(());
@@ -28,7 +31,7 @@ declare_clippy_lint! {
2831 /// ```
2932 #[ clippy:: version = "1.64.0" ]
3033 pub ASSERTIONS_ON_RESULT_STATES ,
31- style ,
34+ restriction ,
3235 "`assert!(r.is_ok())`/`assert!(r.is_err())` gives worse error message than directly calling `r.unwrap()`/`r.unwrap_err()`"
3336}
3437
@@ -56,7 +59,7 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
5659 }
5760 let mut app = Applicability :: MachineApplicable ;
5861 match method_segment. ident . as_str ( ) {
59- "is_ok" if has_debug_impl ( cx, substs. type_at ( 1 ) ) => {
62+ "is_ok" if type_suitable_to_unwrap ( cx, substs. type_at ( 1 ) ) => {
6063 span_lint_and_sugg (
6164 cx,
6265 ASSERTIONS_ON_RESULT_STATES ,
@@ -70,7 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
7073 app,
7174 ) ;
7275 }
73- "is_err" if has_debug_impl ( cx, substs. type_at ( 0 ) ) => {
76+ "is_err" if type_suitable_to_unwrap ( cx, substs. type_at ( 0 ) ) => {
7477 span_lint_and_sugg (
7578 cx,
7679 ASSERTIONS_ON_RESULT_STATES ,
@@ -96,3 +99,15 @@ fn has_debug_impl<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
9699 . get_diagnostic_item ( sym:: Debug )
97100 . map_or ( false , |debug| implements_trait ( cx, ty, debug, & [ ] ) )
98101}
102+
103+ fn is_unit_type ( ty : Ty < ' _ > ) -> bool {
104+ match ty. kind ( ) {
105+ ty:: Tuple ( slice) => slice. is_empty ( ) ,
106+ ty:: Never => true ,
107+ _ => false ,
108+ }
109+ }
110+
111+ fn type_suitable_to_unwrap < ' tcx > ( cx : & LateContext < ' tcx > , ty : Ty < ' tcx > ) -> bool {
112+ has_debug_impl ( cx, ty) && !is_unit_type ( ty)
113+ }
0 commit comments