@@ -5,7 +5,7 @@ use clippy_utils::macros::{MacroCall, macro_backtrace};
5
5
use clippy_utils:: source:: snippet_with_applicability;
6
6
use rustc_data_structures:: fx:: FxHashSet ;
7
7
use rustc_errors:: Applicability ;
8
- use rustc_hir:: { Expr , ExprKind , Node } ;
8
+ use rustc_hir:: { Closure , ClosureKind , CoroutineKind , Expr , ExprKind , LetStmt , LocalSource , Node , Stmt , StmtKind } ;
9
9
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
10
10
use rustc_session:: impl_lint_pass;
11
11
use rustc_span:: { Span , SyntaxContext , sym} ;
@@ -60,6 +60,8 @@ impl LateLintPass<'_> for DbgMacro {
60
60
if cur_syntax_ctxt != self . prev_ctxt &&
61
61
let Some ( macro_call) = first_dbg_macro_in_expansion ( cx, expr. span ) &&
62
62
!macro_call. span . in_external_macro ( cx. sess ( ) . source_map ( ) ) &&
63
+ // avoids exprs generated by the desugaring of coroutines
64
+ !is_coroutine_desugar ( expr) &&
63
65
self . checked_dbg_call_site . insert ( macro_call. span ) &&
64
66
// allows `dbg!` in test code if allow-dbg-in-test is set to true in clippy.toml
65
67
!( self . allow_dbg_in_tests && is_in_test ( cx. tcx , expr. hir_id ) )
@@ -73,50 +75,51 @@ impl LateLintPass<'_> for DbgMacro {
73
75
"the `dbg!` macro is intended as a debugging tool" ,
74
76
|diag| {
75
77
let mut applicability = Applicability :: MachineApplicable ;
76
-
77
- let ( sugg_span, suggestion) = match expr. peel_drop_temps ( ) . kind {
78
- // dbg!()
79
- ExprKind :: Block ( ..) => {
80
- // If the `dbg!` macro is a "free" statement and not contained within other expressions,
81
- // remove the whole statement.
82
- if let Node :: Stmt ( _) = cx. tcx . parent_hir_node ( expr. hir_id )
83
- && let Some ( semi_span) = cx. sess ( ) . source_map ( ) . mac_call_stmt_semi_span ( macro_call. span )
84
- {
85
- ( macro_call. span . to ( semi_span) , String :: new ( ) )
86
- } else {
87
- ( macro_call. span , String :: from ( "()" ) )
88
- }
89
- } ,
90
- // dbg!(1)
91
- ExprKind :: Match ( val, ..) => (
92
- macro_call. span ,
93
- snippet_with_applicability ( cx, val. span . source_callsite ( ) , ".." , & mut applicability)
94
- . to_string ( ) ,
95
- ) ,
96
- // dbg!(2, 3)
97
- ExprKind :: Tup (
98
- [
99
- Expr {
100
- kind : ExprKind :: Match ( first, ..) ,
101
- ..
102
- } ,
103
- ..,
104
- Expr {
105
- kind : ExprKind :: Match ( last, ..) ,
106
- ..
107
- } ,
108
- ] ,
109
- ) => {
110
- let snippet = snippet_with_applicability (
111
- cx,
112
- first. span . source_callsite ( ) . to ( last. span . source_callsite ( ) ) ,
113
- ".." ,
114
- & mut applicability,
115
- ) ;
116
- ( macro_call. span , format ! ( "({snippet})" ) )
117
- } ,
118
- _ => unreachable ! ( ) ,
119
- } ;
78
+ let ( sugg_span, suggestion) =
79
+ match is_async_move_desugar ( expr) . unwrap_or ( expr) . peel_drop_temps ( ) . kind {
80
+ // dbg!()
81
+ ExprKind :: Block ( ..) => {
82
+ // If the `dbg!` macro is a "free" statement and not contained within other expressions,
83
+ // remove the whole statement.
84
+ if let Node :: Stmt ( _) = cx. tcx . parent_hir_node ( expr. hir_id )
85
+ && let Some ( semi_span) =
86
+ cx. sess ( ) . source_map ( ) . mac_call_stmt_semi_span ( macro_call. span )
87
+ {
88
+ ( macro_call. span . to ( semi_span) , String :: new ( ) )
89
+ } else {
90
+ ( macro_call. span , String :: from ( "()" ) )
91
+ }
92
+ } ,
93
+ // dbg!(1)
94
+ ExprKind :: Match ( val, ..) => (
95
+ macro_call. span ,
96
+ snippet_with_applicability ( cx, val. span . source_callsite ( ) , ".." , & mut applicability)
97
+ . to_string ( ) ,
98
+ ) ,
99
+ // dbg!(2, 3)
100
+ ExprKind :: Tup (
101
+ [
102
+ Expr {
103
+ kind : ExprKind :: Match ( first, ..) ,
104
+ ..
105
+ } ,
106
+ ..,
107
+ Expr {
108
+ kind : ExprKind :: Match ( last, ..) ,
109
+ ..
110
+ } ,
111
+ ] ,
112
+ ) => {
113
+ let snippet = snippet_with_applicability (
114
+ cx,
115
+ first. span . source_callsite ( ) . to ( last. span . source_callsite ( ) ) ,
116
+ ".." ,
117
+ & mut applicability,
118
+ ) ;
119
+ ( macro_call. span , format ! ( "({snippet})" ) )
120
+ } ,
121
+ _ => unreachable ! ( ) ,
122
+ } ;
120
123
121
124
diag. span_suggestion (
122
125
sugg_span,
@@ -134,6 +137,35 @@ impl LateLintPass<'_> for DbgMacro {
134
137
}
135
138
}
136
139
140
+ fn is_coroutine_desugar ( expr : & Expr < ' _ > ) -> bool {
141
+ matches ! (
142
+ expr. kind,
143
+ ExprKind :: Closure ( Closure {
144
+ kind: ClosureKind :: Coroutine ( CoroutineKind :: Desugared ( ..) ) | ClosureKind :: CoroutineClosure ( ..) ,
145
+ ..
146
+ } )
147
+ )
148
+ }
149
+
150
+ fn is_async_move_desugar < ' tcx > ( expr : & ' tcx Expr < ' tcx > ) -> Option < & ' tcx Expr < ' tcx > > {
151
+ if let ExprKind :: Block ( block, _) = expr. kind
152
+ && let [
153
+ Stmt {
154
+ kind :
155
+ StmtKind :: Let ( LetStmt {
156
+ source : LocalSource :: AsyncFn ,
157
+ ..
158
+ } ) ,
159
+ ..
160
+ } ,
161
+ ] = block. stmts
162
+ {
163
+ return block. expr ;
164
+ }
165
+
166
+ None
167
+ }
168
+
137
169
fn first_dbg_macro_in_expansion ( cx : & LateContext < ' _ > , span : Span ) -> Option < MacroCall > {
138
170
macro_backtrace ( span) . find ( |mc| cx. tcx . is_diagnostic_item ( sym:: dbg_macro, mc. def_id ) )
139
171
}
0 commit comments