Also apply panic_fmt lint suggestions to debug_assert!().

This commit is contained in:
Mara Bos 2020-10-19 00:45:07 +02:00
parent 0a9330c7ef
commit d3b41497fe
5 changed files with 23 additions and 10 deletions

View File

@ -47,7 +47,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicFmt {
fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tcx hir::Expr<'tcx>) {
if let hir::ExprKind::Lit(lit) = &arg.kind {
if let ast::LitKind::Str(sym, _) = lit.node {
let expn = f.span.ctxt().outer_expn_data();
let mut expn = f.span.ctxt().outer_expn_data();
if let Some(id) = expn.macro_def_id {
if cx.tcx.is_diagnostic_item(sym::std_panic_macro, id)
|| cx.tcx.is_diagnostic_item(sym::core_panic_macro, id)
@ -59,19 +59,17 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
let s = s.replace("{{", "").replace("}}", "");
let looks_like_placeholder =
s.find('{').map_or(false, |i| s[i + 1..].contains('}'));
let expn = {
// Unwrap another level of macro expansion if this
// panic!() was expanded from assert!().
// Unwrap another level of macro expansion if this panic!()
// was expanded from assert!() or debug_assert!().
for &assert in &[sym::assert_macro, sym::debug_assert_macro] {
let parent = expn.call_site.ctxt().outer_expn_data();
if parent
.macro_def_id
.map_or(false, |id| cx.tcx.is_diagnostic_item(sym::assert_macro, id))
.map_or(false, |id| cx.tcx.is_diagnostic_item(assert, id))
{
parent
} else {
expn
expn = parent;
}
};
}
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");

View File

@ -418,6 +418,7 @@ symbols! {
dead_code,
dealloc,
debug,
debug_assert_macro,
debug_assertions,
debug_struct,
debug_trait,

View File

@ -163,6 +163,7 @@ macro_rules! assert_ne {
/// ```
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "debug_assert_macro")]
macro_rules! debug_assert {
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert!($($arg)*); })
}

View File

@ -6,4 +6,5 @@ fn main() {
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

@ -55,5 +55,17 @@ help: or add a "{}" format string to use the message literally
LL | assert!(false, "{}", "{:03x} bla");
| ^^^^^
warning: 4 warnings emitted
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
help: add a "{}" format string to use the message literally
|
LL | debug_assert!(false, "{}", "{{}} bla");
| ^^^^^
warning: 5 warnings emitted