@@ -4,7 +4,7 @@ use rustc_errors::{pluralize, Applicability};
44use rustc_hir as hir;
55use rustc_middle:: ty;
66use 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 } ;
88
99declare_lint ! {
1010 /// 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
6767
6868 // The argument is *not* a string literal.
6969
70- let ( span, panic) = panic_call ( cx, f) ;
70+ let ( span, panic, symbol_str ) = panic_call ( cx, f) ;
7171
7272 // Find the span of the argument to `panic!()`, before expansion in the
7373 // case of `panic!(some_macro!())`.
@@ -95,7 +95,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
9595 }
9696 if arg_macro. map_or ( false , |id| cx. tcx . is_diagnostic_item ( sym:: format_macro, id) ) {
9797 // 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 ( ) ) ;
9999 if let Some ( ( open, close, _) ) = find_delimiters ( cx, arg_span) {
100100 l. multipart_suggestion (
101101 "remove the `format!(..)` macro call" ,
@@ -160,7 +160,7 @@ fn check_panic_str<'tcx>(
160160 Parser :: new ( fmt. as_ref ( ) , style, snippet. clone ( ) , false , ParseMode :: Format ) ;
161161 let n_arguments = ( & mut fmt_parser) . filter ( |a| matches ! ( a, Piece :: NextArgument ( _) ) ) . count ( ) ;
162162
163- let ( span, _) = panic_call ( cx, f) ;
163+ let ( span, _, _ ) = panic_call ( cx, f) ;
164164
165165 if n_arguments > 0 && fmt_parser. errors . is_empty ( ) {
166166 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
230230 ) )
231231}
232232
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 ) {
234234 let mut expn = f. span . ctxt ( ) . outer_expn_data ( ) ;
235235
236236 let mut panic_macro = kw:: Empty ;
@@ -248,5 +248,10 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span,
248248 }
249249 }
250250
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 ( ) )
252257}
0 commit comments