@@ -6,6 +6,7 @@ use rustc_data_structures::packed::Pu128;
66use rustc_errors:: Applicability ;
77use rustc_hir:: { ConstArgKind , ExprKind , Node } ;
88use rustc_lint:: { LateContext , LateLintPass } ;
9+ use rustc_middle:: ty:: IsSuggestable ;
910use rustc_session:: declare_lint_pass;
1011
1112declare_clippy_lint ! {
@@ -72,6 +73,7 @@ fn inner_check(cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>, inner_expr:
7273 // check if expr is a call or has a call inside it
7374 if inner_expr. can_have_side_effects ( ) {
7475 let parent_hir_node = cx. tcx . parent_hir_node ( expr. hir_id ) ;
76+ let inner_expr_ty = cx. typeck_results ( ) . expr_ty ( inner_expr) ;
7577 let return_type = cx. typeck_results ( ) . expr_ty ( expr) ;
7678
7779 let inner_expr = snippet ( cx, inner_expr. span . source_callsite ( ) , ".." ) ;
@@ -94,18 +96,25 @@ fn inner_check(cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>, inner_expr:
9496 ) ,
9597 _ => ( expr. span , format ! ( "{{ {inner_expr}; {vec}[] as {return_type} }}" ) ) ,
9698 } ;
99+ let span = span. source_callsite ( ) ;
97100 span_lint_and_then (
98101 cx,
99102 ZERO_REPEAT_SIDE_EFFECTS ,
100- span. source_callsite ( ) ,
103+ span,
101104 "expression with side effects as the initial value in a zero-sized array initializer" ,
102105 |diag| {
103- diag. span_suggestion_verbose (
104- span. source_callsite ( ) ,
105- "consider performing the side effect separately" ,
106- sugg,
107- Applicability :: Unspecified ,
108- ) ;
106+ if ( !inner_expr_ty. is_never ( ) || cx. tcx . features ( ) . never_type ( ) )
107+ && return_type. is_suggestable ( cx. tcx , true )
108+ {
109+ diag. span_suggestion_verbose (
110+ span,
111+ "consider performing the side effect separately" ,
112+ sugg,
113+ Applicability :: Unspecified ,
114+ ) ;
115+ } else {
116+ diag. help ( "consider performing the side effect separately" ) ;
117+ }
109118 } ,
110119 ) ;
111120 }
0 commit comments