Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve "panic message is not a string literal" warning #85075

Merged
merged 1 commit into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions compiler/rustc_lint/src/non_fmt_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_errors::{pluralize, Applicability};
use rustc_hir as hir;
use rustc_middle::ty;
use rustc_parse_format::{ParseMode, Parser, Piece};
use rustc_span::{sym, symbol::kw, InnerSpan, Span, Symbol};
use rustc_span::{hygiene, sym, symbol::kw, symbol::SymbolStr, InnerSpan, Span, Symbol};

declare_lint! {
/// The `non_fmt_panic` lint detects `panic!(..)` invocations where the first
Expand Down Expand Up @@ -67,7 +67,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc

// The argument is *not* a string literal.

let (span, panic) = panic_call(cx, f);
let (span, panic, symbol_str) = panic_call(cx, f);

// Find the span of the argument to `panic!()`, before expansion in the
// case of `panic!(some_macro!())`.
Expand Down Expand Up @@ -95,7 +95,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
}
if arg_macro.map_or(false, |id| cx.tcx.is_diagnostic_item(sym::format_macro, id)) {
// A case of `panic!(format!(..))`.
l.note("the panic!() macro supports formatting, so there's no need for the format!() macro here");
l.note(format!("the {}!() macro supports formatting, so there's no need for the format!() macro here", symbol_str).as_str());
if let Some((open, close, _)) = find_delimiters(cx, arg_span) {
l.multipart_suggestion(
"remove the `format!(..)` macro call",
Expand Down Expand Up @@ -160,7 +160,7 @@ fn check_panic_str<'tcx>(
Parser::new(fmt.as_ref(), style, snippet.clone(), false, ParseMode::Format);
let n_arguments = (&mut fmt_parser).filter(|a| matches!(a, Piece::NextArgument(_))).count();

let (span, _) = panic_call(cx, f);
let (span, _, _) = panic_call(cx, f);

if n_arguments > 0 && fmt_parser.errors.is_empty() {
let arg_spans: Vec<_> = match &fmt_parser.arg_places[..] {
Expand Down Expand Up @@ -230,7 +230,7 @@ fn find_delimiters<'tcx>(cx: &LateContext<'tcx>, span: Span) -> Option<(Span, Sp
))
}

fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span, Symbol) {
fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span, Symbol, SymbolStr) {
let mut expn = f.span.ctxt().outer_expn_data();

let mut panic_macro = kw::Empty;
Expand All @@ -248,5 +248,10 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span,
}
}

(expn.call_site, panic_macro)
let macro_symbol = if let hygiene::ExpnKind::Macro(_, symbol) = expn.kind {
symbol
} else {
Symbol::intern("panic")
};
(expn.call_site, panic_macro, macro_symbol.as_str())
}
2 changes: 2 additions & 0 deletions src/test/ui/non-fmt-panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ fn main() {
panic!(a!()); //~ WARN panic message is not a string literal

panic!(format!("{}", 1)); //~ WARN panic message is not a string literal
assert!(false, format!("{}", 1)); //~ WARN panic message is not a string literal
debug_assert!(false, format!("{}", 1)); //~ WARN panic message is not a string literal

panic![123]; //~ WARN panic message is not a string literal
panic!{123}; //~ WARN panic message is not a string literal
Expand Down
32 changes: 29 additions & 3 deletions src/test/ui/non-fmt-panic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,33 @@ LL | panic!("{}", 1);
| -- --

warning: panic message is not a string literal
--> $DIR/non-fmt-panic.rs:40:12
--> $DIR/non-fmt-panic.rs:39:20
|
LL | assert!(false, format!("{}", 1));
| ^^^^^^^^^^^^^^^^
|
= note: this is no longer accepted in Rust 2021
= note: the assert!() macro supports formatting, so there's no need for the format!() macro here
help: remove the `format!(..)` macro call
|
LL | assert!(false, "{}", 1);
| -- --

warning: panic message is not a string literal
--> $DIR/non-fmt-panic.rs:40:26
|
LL | debug_assert!(false, format!("{}", 1));
| ^^^^^^^^^^^^^^^^
|
= note: this is no longer accepted in Rust 2021
= note: the debug_assert!() macro supports formatting, so there's no need for the format!() macro here
help: remove the `format!(..)` macro call
|
LL | debug_assert!(false, "{}", 1);
| -- --

warning: panic message is not a string literal
--> $DIR/non-fmt-panic.rs:42:12
|
LL | panic![123];
| ^^^
Expand All @@ -229,7 +255,7 @@ LL | std::panic::panic_any(123);
| ^^^^^^^^^^^^^^^^^^^^^^ ^

warning: panic message is not a string literal
--> $DIR/non-fmt-panic.rs:41:12
--> $DIR/non-fmt-panic.rs:43:12
|
LL | panic!{123};
| ^^^
Expand All @@ -244,5 +270,5 @@ help: or use std::panic::panic_any instead
LL | std::panic::panic_any(123);
| ^^^^^^^^^^^^^^^^^^^^^^ ^

warning: 18 warnings emitted
warning: 20 warnings emitted