1
1
use clippy_utils:: diagnostics:: { span_lint, span_lint_and_then} ;
2
- use clippy_utils:: macros:: { root_macro_call_first_node, FormatArgsExpn , MacroCall } ;
2
+ use clippy_utils:: macros:: { root_macro_call_first_node, FormatArgsArg , FormatArgsExpn , MacroCall } ;
3
3
use clippy_utils:: source:: snippet_opt;
4
4
use rustc_ast:: LitKind ;
5
5
use rustc_errors:: Applicability ;
@@ -275,6 +275,9 @@ impl<'tcx> LateLintPass<'tcx> for Write {
275
275
}
276
276
277
277
let Some ( format_args) = FormatArgsExpn :: find_nested ( cx, expr, macro_call. expn ) else { return } ;
278
+ if format_args. format_string_span . from_expansion ( ) {
279
+ return ;
280
+ }
278
281
279
282
match diag_name {
280
283
sym:: print_macro | sym:: eprint_macro | sym:: write_macro => {
@@ -286,20 +289,15 @@ impl<'tcx> LateLintPass<'tcx> for Write {
286
289
_ => { } ,
287
290
}
288
291
289
- check_literal ( cx, & format_args, name) ;
292
+ let Some ( args) = format_args. args ( cx) else { return } ;
293
+ check_literal ( cx, & args, name, format_args. is_raw ( cx) ) ;
290
294
291
- if !self . in_debug_impl
292
- && format_args
293
- . formatters
294
- . iter ( )
295
- . any ( |& ( _, formatter) | formatter == sym:: Debug )
296
- {
297
- span_lint (
298
- cx,
299
- USE_DEBUG ,
300
- format_args. format_string_span ,
301
- "use of `Debug`-based formatting" ,
302
- ) ;
295
+ if !self . in_debug_impl {
296
+ for arg in args {
297
+ if arg. format_trait == sym:: Debug {
298
+ span_lint ( cx, USE_DEBUG , arg. span , "use of `Debug`-based formatting" ) ;
299
+ }
300
+ }
303
301
}
304
302
}
305
303
}
@@ -335,7 +333,6 @@ fn check_newline(cx: &LateContext<'_>, format_args: &FormatArgsExpn<'_>, macro_c
335
333
} ;
336
334
337
335
if_chain ! {
338
- if !format_string_span. from_expansion( ) ;
339
336
if last. as_str( ) . ends_with( '\n' ) ;
340
337
341
338
// ignore format strings with other internal vertical whitespace
@@ -401,9 +398,6 @@ fn check_empty_string(cx: &LateContext<'_>, format_args: &FormatArgsExpn<'_>, ma
401
398
let mut span = format_args. format_string_span;
402
399
403
400
if part. as_str( ) == "\n " ;
404
- // `println!()` is also represented with an "\n" format string part, but
405
- // its span is from an expansion
406
- if !span. from_expansion( ) ;
407
401
then {
408
402
let lint = if name == "writeln" {
409
403
span = expand_past_previous_comma( cx, span) ;
@@ -432,15 +426,9 @@ fn check_empty_string(cx: &LateContext<'_>, format_args: &FormatArgsExpn<'_>, ma
432
426
}
433
427
}
434
428
435
- fn check_literal < ' tcx > ( cx : & LateContext < ' tcx > , format_args : & FormatArgsExpn < ' tcx > , name : & str ) {
436
- if format_args. format_string_span . from_expansion ( ) {
437
- return ;
438
- }
439
- let raw = format_args. is_raw ( cx) ;
440
-
441
- let Some ( args) = format_args. args ( cx) else { return } ;
429
+ fn check_literal ( cx : & LateContext < ' _ > , args : & [ FormatArgsArg < ' _ > ] , name : & str , raw : bool ) {
442
430
let mut counts = HirIdMap :: < usize > :: default ( ) ;
443
- for arg in & args {
431
+ for arg in args {
444
432
* counts. entry ( arg. value . hir_id ) . or_default ( ) += 1 ;
445
433
}
446
434
0 commit comments