Improve panic_fmt lint messages.

(From the PR feedback.)

Co-authored-by: Esteban Küber <esteban@kuber.com.ar>
This commit is contained in:
Mara Bos 2020-10-29 19:44:06 +01:00
parent 5cefc3ce41
commit 9743f67684
2 changed files with 15 additions and 15 deletions

View File

@ -1,6 +1,6 @@
use crate::{LateContext, LateLintPass, LintContext}; use crate::{LateContext, LateLintPass, LintContext};
use rustc_ast as ast; use rustc_ast as ast;
use rustc_errors::Applicability; use rustc_errors::{pluralize, Applicability};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_middle::ty; use rustc_middle::ty;
use rustc_parse_format::{ParseMode, Parser, Piece}; use rustc_parse_format::{ParseMode, Parser, Piece};
@ -21,7 +21,7 @@ declare_lint! {
/// ///
/// `panic!("{}")` panics with the message `"{}"`, as a `panic!()` invocation /// `panic!("{}")` panics with the message `"{}"`, as a `panic!()` invocation
/// with a single argument does not use `format_args!()`. /// with a single argument does not use `format_args!()`.
/// A future version of Rust will interpret this string as format string, /// A future edition of Rust will interpret this string as format string,
/// which would break this. /// which would break this.
PANIC_FMT, PANIC_FMT,
Warn, Warn,
@ -97,11 +97,11 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
1 => "panic message contains an unused formatting placeholder", 1 => "panic message contains an unused formatting placeholder",
_ => "panic message contains unused formatting placeholders", _ => "panic message contains unused formatting placeholders",
}); });
l.note("this message is not used as a format string when given without arguments, but will be in a future Rust version"); l.note("this message is not used as a format string when given without arguments, but will be in a future Rust edition");
if expn.call_site.contains(arg.span) { if expn.call_site.contains(arg.span) {
l.span_suggestion( l.span_suggestion(
arg.span.shrink_to_hi(), arg.span.shrink_to_hi(),
"add the missing argument(s)", &format!("add the missing argument{}", pluralize!(n_arguments)),
", ...".into(), ", ...".into(),
Applicability::HasPlaceholders, Applicability::HasPlaceholders,
); );
@ -131,7 +131,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
}; };
cx.struct_span_lint(PANIC_FMT, brace_spans.unwrap_or(vec![expn.call_site]), |lint| { cx.struct_span_lint(PANIC_FMT, brace_spans.unwrap_or(vec![expn.call_site]), |lint| {
let mut l = lint.build(msg); let mut l = lint.build(msg);
l.note("this message is not used as a format string, but will be in a future Rust version"); l.note("this message is not used as a format string, but will be in a future Rust edition");
if expn.call_site.contains(arg.span) { if expn.call_site.contains(arg.span) {
l.span_suggestion( l.span_suggestion(
arg.span.shrink_to_lo(), arg.span.shrink_to_lo(),

View File

@ -5,7 +5,7 @@ LL | panic!("here's a brace: {");
| ^ | ^
| |
= note: `#[warn(panic_fmt)]` on by default = note: `#[warn(panic_fmt)]` on by default
= note: this message is not used as a format string, but will be in a future Rust version = note: this message is not used as a format string, but will be in a future Rust edition
help: add a "{}" format string to use the message literally help: add a "{}" format string to use the message literally
| |
LL | panic!("{}", "here's a brace: {"); LL | panic!("{}", "here's a brace: {");
@ -17,7 +17,7 @@ warning: panic message contains a brace
LL | std::panic!("another one: }"); LL | std::panic!("another one: }");
| ^ | ^
| |
= note: this message is not used as a format string, but will be in a future Rust version = note: this message is not used as a format string, but will be in a future Rust edition
help: add a "{}" format string to use the message literally help: add a "{}" format string to use the message literally
| |
LL | std::panic!("{}", "another one: }"); LL | std::panic!("{}", "another one: }");
@ -29,8 +29,8 @@ warning: panic message contains an unused formatting placeholder
LL | core::panic!("Hello {}"); LL | core::panic!("Hello {}");
| ^^ | ^^
| |
= note: this message is not used as a format string when given without arguments, but will be in a future Rust version = note: this message is not used as a format string when given without arguments, but will be in a future Rust edition
help: add the missing argument(s) help: add the missing argument
| |
LL | core::panic!("Hello {}", ...); LL | core::panic!("Hello {}", ...);
| ^^^^^ | ^^^^^
@ -45,8 +45,8 @@ warning: panic message contains unused formatting placeholders
LL | assert!(false, "{:03x} {test} bla"); LL | assert!(false, "{:03x} {test} bla");
| ^^^^^^ ^^^^^^ | ^^^^^^ ^^^^^^
| |
= note: this message is not used as a format string when given without arguments, but will be in a future Rust version = note: this message is not used as a format string when given without arguments, but will be in a future Rust edition
help: add the missing argument(s) help: add the missing arguments
| |
LL | assert!(false, "{:03x} {test} bla", ...); LL | assert!(false, "{:03x} {test} bla", ...);
| ^^^^^ | ^^^^^
@ -61,7 +61,7 @@ warning: panic message contains braces
LL | debug_assert!(false, "{{}} bla"); LL | debug_assert!(false, "{{}} bla");
| ^^^^ | ^^^^
| |
= note: this message is not used as a format string, but will be in a future Rust version = note: this message is not used as a format string, but will be in a future Rust edition
help: add a "{}" format string to use the message literally help: add a "{}" format string to use the message literally
| |
LL | debug_assert!(false, "{}", "{{}} bla"); LL | debug_assert!(false, "{}", "{{}} bla");
@ -73,8 +73,8 @@ warning: panic message contains an unused formatting placeholder
LL | panic!(concat!("{", "}")); LL | panic!(concat!("{", "}"));
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^
| |
= note: this message is not used as a format string when given without arguments, but will be in a future Rust version = note: this message is not used as a format string when given without arguments, but will be in a future Rust edition
help: add the missing argument(s) help: add the missing argument
| |
LL | panic!(concat!("{", "}"), ...); LL | panic!(concat!("{", "}"), ...);
| ^^^^^ | ^^^^^
@ -89,7 +89,7 @@ warning: panic message contains braces
LL | panic!(concat!("{", "{")); LL | panic!(concat!("{", "{"));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
= note: this message is not used as a format string, but will be in a future Rust version = note: this message is not used as a format string, but will be in a future Rust edition
help: add a "{}" format string to use the message literally help: add a "{}" format string to use the message literally
| |
LL | panic!("{}", concat!("{", "{")); LL | panic!("{}", concat!("{", "{"));