@@ -4,7 +4,7 @@ use rustc_errors::{pluralize, Applicability};
4
4
use rustc_hir as hir;
5
5
use rustc_middle:: ty;
6
6
use rustc_parse_format:: { ParseMode , Parser , Piece } ;
7
- use rustc_span:: { sym, symbol:: kw, InnerSpan , Span , Symbol } ;
7
+ use rustc_span:: { hygiene , sym, symbol:: kw, symbol :: SymbolStr , InnerSpan , Span , Symbol } ;
8
8
9
9
declare_lint ! {
10
10
/// The `non_fmt_panic` lint detects `panic!(..)` invocations where the first
@@ -67,7 +67,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
67
67
68
68
// The argument is *not* a string literal.
69
69
70
- let ( span, panic) = panic_call ( cx, f) ;
70
+ let ( span, panic, symbol_str ) = panic_call ( cx, f) ;
71
71
72
72
// Find the span of the argument to `panic!()`, before expansion in the
73
73
// case of `panic!(some_macro!())`.
@@ -95,7 +95,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
95
95
}
96
96
if arg_macro. map_or ( false , |id| cx. tcx . is_diagnostic_item ( sym:: format_macro, id) ) {
97
97
// A case of `panic!(format!(..))`.
98
- l. note ( "the panic !() macro supports formatting, so there's no need for the format!() macro here" ) ;
98
+ l. note ( format ! ( "the {} !() macro supports formatting, so there's no need for the format!() macro here" , symbol_str ) . as_str ( ) ) ;
99
99
if let Some ( ( open, close, _) ) = find_delimiters ( cx, arg_span) {
100
100
l. multipart_suggestion (
101
101
"remove the `format!(..)` macro call" ,
@@ -160,7 +160,7 @@ fn check_panic_str<'tcx>(
160
160
Parser :: new ( fmt. as_ref ( ) , style, snippet. clone ( ) , false , ParseMode :: Format ) ;
161
161
let n_arguments = ( & mut fmt_parser) . filter ( |a| matches ! ( a, Piece :: NextArgument ( _) ) ) . count ( ) ;
162
162
163
- let ( span, _) = panic_call ( cx, f) ;
163
+ let ( span, _, _ ) = panic_call ( cx, f) ;
164
164
165
165
if n_arguments > 0 && fmt_parser. errors . is_empty ( ) {
166
166
let arg_spans: Vec < _ > = match & fmt_parser. arg_places [ ..] {
@@ -230,7 +230,7 @@ fn find_delimiters<'tcx>(cx: &LateContext<'tcx>, span: Span) -> Option<(Span, Sp
230
230
) )
231
231
}
232
232
233
- fn panic_call < ' tcx > ( cx : & LateContext < ' tcx > , f : & ' tcx hir:: Expr < ' tcx > ) -> ( Span , Symbol ) {
233
+ fn panic_call < ' tcx > ( cx : & LateContext < ' tcx > , f : & ' tcx hir:: Expr < ' tcx > ) -> ( Span , Symbol , SymbolStr ) {
234
234
let mut expn = f. span . ctxt ( ) . outer_expn_data ( ) ;
235
235
236
236
let mut panic_macro = kw:: Empty ;
@@ -248,5 +248,10 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span,
248
248
}
249
249
}
250
250
251
- ( expn. call_site , panic_macro)
251
+ let macro_symbol = if let hygiene:: ExpnKind :: Macro ( _, symbol) = expn. kind {
252
+ symbol
253
+ } else {
254
+ Symbol :: intern ( "panic" )
255
+ } ;
256
+ ( expn. call_site , panic_macro, macro_symbol. as_str ( ) )
252
257
}
0 commit comments