Small cleanups in assert!() and panic_fmt lint.

(From the PR feedback.)

Co-authored-by: Esteban Küber <esteban@kuber.com.ar>
This commit is contained in:
Mara Bos 2020-10-19 21:14:05 +02:00
parent ff8df0bbe3
commit 0f193d1a62
4 changed files with 48 additions and 50 deletions

View File

@ -27,37 +27,35 @@ pub fn expand_assert<'cx>(
// context to pick up whichever is currently in scope.
let sp = cx.with_call_site_ctxt(sp);
let panic_call = {
if let Some(tokens) = custom_message {
// Pass the custom message to panic!().
cx.expr(
sp,
ExprKind::MacCall(MacCall {
path: Path::from_ident(Ident::new(sym::panic, sp)),
args: P(MacArgs::Delimited(
DelimSpan::from_single(sp),
MacDelimiter::Parenthesis,
tokens,
)),
prior_type_ascription: None,
}),
)
} else {
// Pass our own message directly to $crate::panicking::panic(),
// because it might contain `{` and `}` that should always be
// passed literally.
cx.expr_call_global(
sp,
cx.std_path(&[sym::panicking, sym::panic]),
vec![cx.expr_str(
DUMMY_SP,
Symbol::intern(&format!(
"assertion failed: {}",
pprust::expr_to_string(&cond_expr).escape_debug()
)),
)],
)
}
let panic_call = if let Some(tokens) = custom_message {
// Pass the custom message to panic!().
cx.expr(
sp,
ExprKind::MacCall(MacCall {
path: Path::from_ident(Ident::new(sym::panic, sp)),
args: P(MacArgs::Delimited(
DelimSpan::from_single(sp),
MacDelimiter::Parenthesis,
tokens,
)),
prior_type_ascription: None,
}),
)
} else {
// Pass our own message directly to $crate::panicking::panic(),
// because it might contain `{` and `}` that should always be
// passed literally.
cx.expr_call_global(
sp,
cx.std_path(&[sym::panicking, sym::panic]),
vec![cx.expr_str(
DUMMY_SP,
Symbol::intern(&format!(
"assertion failed: {}",
pprust::expr_to_string(&cond_expr).escape_debug()
)),
)],
)
};
let if_expr =
cx.expr_if(sp, cx.expr(sp, ExprKind::Unary(UnOp::Not, cond_expr)), panic_call, None);

View File

@ -72,8 +72,8 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
}
if looks_like_placeholder {
cx.struct_span_lint(PANIC_FMT, arg.span.source_callsite(), |lint| {
let mut l = lint.build("Panic message contains an unused formatting placeholder");
l.note("This message is not used as a format string when given without arguments, but will be in a future Rust version");
let mut l = lint.build("panic message contains an unused formatting placeholder");
l.note("this message is not used as a format string when given without arguments, but will be in a future Rust version");
if expn.call_site.contains(arg.span) {
l.span_suggestion(
arg.span.shrink_to_hi(),
@ -92,8 +92,8 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
});
} else {
cx.struct_span_lint(PANIC_FMT, expn.call_site, |lint| {
let mut l = lint.build("Panic message contains a brace");
l.note("This message is not used as a format string, but will be in a future Rust version");
let mut l = lint.build("panic message contains a brace");
l.note("this message is not used as a format string, but will be in a future Rust version");
if expn.call_site.contains(arg.span) {
l.span_suggestion(
arg.span.shrink_to_lo(),

View File

@ -2,9 +2,9 @@
#[allow(unreachable_code)]
fn main() {
panic!("here's a brace: {"); //~ WARN Panic message contains a brace
std::panic!("another one: }"); //~ WARN Panic message contains a brace
core::panic!("Hello {}"); //~ WARN Panic message contains an unused formatting placeholder
assert!(false, "{:03x} bla"); //~ WARN Panic message contains an unused formatting placeholder
debug_assert!(false, "{{}} bla"); //~ WARN Panic message contains a brace
panic!("here's a brace: {"); //~ WARN panic message contains a brace
std::panic!("another one: }"); //~ WARN panic message contains a brace
core::panic!("Hello {}"); //~ WARN panic message contains an unused formatting placeholder
assert!(false, "{:03x} bla"); //~ WARN panic message contains an unused formatting placeholder
debug_assert!(false, "{{}} bla"); //~ WARN panic message contains a brace
}

View File

@ -1,35 +1,35 @@
warning: Panic message contains a brace
warning: panic message contains a brace
--> $DIR/panic-brace.rs:5:5
|
LL | panic!("here's a brace: {");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= 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 version
help: add a "{}" format string to use the message literally
|
LL | panic!("{}", "here's a brace: {");
| ^^^^^
warning: Panic message contains a brace
warning: panic message contains a brace
--> $DIR/panic-brace.rs:6:5
|
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 version
help: add a "{}" format string to use the message literally
|
LL | std::panic!("{}", "another one: }");
| ^^^^^
warning: Panic message contains an unused formatting placeholder
warning: panic message contains an unused formatting placeholder
--> $DIR/panic-brace.rs:7:18
|
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 version
help: add the missing argument(s)
|
LL | core::panic!("Hello {}", argument);
@ -39,13 +39,13 @@ help: or add a "{}" format string to use the message literally
LL | core::panic!("{}", "Hello {}");
| ^^^^^
warning: Panic message contains an unused formatting placeholder
warning: panic message contains an unused formatting placeholder
--> $DIR/panic-brace.rs:8:20
|
LL | assert!(false, "{:03x} 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 version
help: add the missing argument(s)
|
LL | assert!(false, "{:03x} bla", argument);
@ -55,13 +55,13 @@ help: or add a "{}" format string to use the message literally
LL | assert!(false, "{}", "{:03x} bla");
| ^^^^^
warning: Panic message contains a brace
warning: panic message contains a brace
--> $DIR/panic-brace.rs:9:5
|
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 version
help: add a "{}" format string to use the message literally
|
LL | debug_assert!(false, "{}", "{{}} bla");