@@ -6,7 +6,7 @@ use rustc_session::declare_lint_pass;
66use rustc_span:: Span ;
77
88use clippy_utils:: diagnostics:: span_lint_and_then;
9- use clippy_utils:: source:: snippet_with_applicability;
9+ use clippy_utils:: source:: { snippet_with_applicability, snippet_with_context } ;
1010use clippy_utils:: ty:: has_iter_method;
1111use clippy_utils:: { is_trait_method, sym} ;
1212
@@ -101,18 +101,23 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessForEach {
101101
102102 let body_param_sugg = snippet_with_applicability ( cx, body. params [ 0 ] . pat . span , ".." , & mut applicability) ;
103103 let for_each_rev_sugg = snippet_with_applicability ( cx, for_each_recv. span , ".." , & mut applicability) ;
104- let body_value_sugg = snippet_with_applicability ( cx, body. value . span , ".." , & mut applicability) ;
104+ let ( body_value_sugg, is_macro_call) =
105+ snippet_with_context ( cx, body. value . span , for_each_recv. span . ctxt ( ) , ".." , & mut applicability) ;
105106
106107 let sugg = format ! (
107108 "for {} in {} {}" ,
108109 body_param_sugg,
109110 for_each_rev_sugg,
110- match body. value. kind {
111- ExprKind :: Block ( block, _) if is_let_desugar( block) => {
112- format!( "{{ {body_value_sugg} }}" )
113- } ,
114- ExprKind :: Block ( _, _) => body_value_sugg. to_string( ) ,
115- _ => format!( "{{ {body_value_sugg}; }}" ) ,
111+ if is_macro_call {
112+ format!( "{{ {body_value_sugg}; }}" )
113+ } else {
114+ match body. value. kind {
115+ ExprKind :: Block ( block, _) if is_let_desugar( block) => {
116+ format!( "{{ {body_value_sugg} }}" )
117+ } ,
118+ ExprKind :: Block ( _, _) => body_value_sugg. to_string( ) ,
119+ _ => format!( "{{ {body_value_sugg}; }}" ) ,
120+ }
116121 }
117122 ) ;
118123
0 commit comments