Also apply panic_fmt lint suggestions to debug_assert!().
This commit is contained in:
parent
0a9330c7ef
commit
d3b41497fe
5 changed files with 23 additions and 10 deletions
|
@ -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>) {
|
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 hir::ExprKind::Lit(lit) = &arg.kind {
|
||||||
if let ast::LitKind::Str(sym, _) = lit.node {
|
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 let Some(id) = expn.macro_def_id {
|
||||||
if cx.tcx.is_diagnostic_item(sym::std_panic_macro, id)
|
if cx.tcx.is_diagnostic_item(sym::std_panic_macro, id)
|
||||||
|| cx.tcx.is_diagnostic_item(sym::core_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 s = s.replace("{{", "").replace("}}", "");
|
||||||
let looks_like_placeholder =
|
let looks_like_placeholder =
|
||||||
s.find('{').map_or(false, |i| s[i + 1..].contains('}'));
|
s.find('{').map_or(false, |i| s[i + 1..].contains('}'));
|
||||||
let expn = {
|
// Unwrap another level of macro expansion if this panic!()
|
||||||
// Unwrap another level of macro expansion if this
|
// was expanded from assert!() or debug_assert!().
|
||||||
// panic!() was expanded from assert!().
|
for &assert in &[sym::assert_macro, sym::debug_assert_macro] {
|
||||||
let parent = expn.call_site.ctxt().outer_expn_data();
|
let parent = expn.call_site.ctxt().outer_expn_data();
|
||||||
if parent
|
if parent
|
||||||
.macro_def_id
|
.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
|
expn = parent;
|
||||||
} else {
|
|
||||||
expn
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
if looks_like_placeholder {
|
if looks_like_placeholder {
|
||||||
cx.struct_span_lint(PANIC_FMT, arg.span.source_callsite(), |lint| {
|
cx.struct_span_lint(PANIC_FMT, arg.span.source_callsite(), |lint| {
|
||||||
let mut l = lint.build("Panic message contains an unused formatting placeholder");
|
let mut l = lint.build("Panic message contains an unused formatting placeholder");
|
||||||
|
|
|
@ -418,6 +418,7 @@ symbols! {
|
||||||
dead_code,
|
dead_code,
|
||||||
dealloc,
|
dealloc,
|
||||||
debug,
|
debug,
|
||||||
|
debug_assert_macro,
|
||||||
debug_assertions,
|
debug_assertions,
|
||||||
debug_struct,
|
debug_struct,
|
||||||
debug_trait,
|
debug_trait,
|
||||||
|
|
|
@ -163,6 +163,7 @@ macro_rules! assert_ne {
|
||||||
/// ```
|
/// ```
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "debug_assert_macro")]
|
||||||
macro_rules! debug_assert {
|
macro_rules! debug_assert {
|
||||||
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert!($($arg)*); })
|
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert!($($arg)*); })
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,4 +6,5 @@ fn main() {
|
||||||
std::panic!("another one: }"); //~ 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
|
core::panic!("Hello {}"); //~ WARN Panic message contains an unused formatting placeholder
|
||||||
assert!(false, "{:03x} bla"); //~ 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
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,5 +55,17 @@ help: or add a "{}" format string to use the message literally
|
||||||
LL | assert!(false, "{}", "{:03x} bla");
|
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
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue