Specialize panic_fmt lint for the {core,std}::panic!() macros.
It now only reacts to expansion of those macros, and suggests inserting `"{}", ` in the right place.
This commit is contained in:
parent
462ee9c1b5
commit
da66a501f6
2 changed files with 24 additions and 5 deletions
|
@ -1,7 +1,9 @@
|
||||||
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_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
|
use rustc_span::sym;
|
||||||
|
|
||||||
declare_lint! {
|
declare_lint! {
|
||||||
/// The `panic_fmt` lint detects `panic!("..")` with `{` or `}` in the string literal.
|
/// The `panic_fmt` lint detects `panic!("..")` with `{` or `}` in the string literal.
|
||||||
|
@ -46,11 +48,26 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
|
||||||
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 {
|
||||||
if sym.as_str().contains(&['{', '}'][..]) {
|
if sym.as_str().contains(&['{', '}'][..]) {
|
||||||
cx.struct_span_lint(PANIC_FMT, f.span, |lint| {
|
let expn = f.span.ctxt().outer_expn_data();
|
||||||
lint.build("Panic message contains a brace")
|
if let Some(id) = expn.macro_def_id {
|
||||||
.note("This message is not used as a format string, but will be in a future Rust version")
|
if cx.tcx.is_diagnostic_item(sym::std_panic_macro, id)
|
||||||
.emit();
|
|| cx.tcx.is_diagnostic_item(sym::core_panic_macro, id)
|
||||||
});
|
{
|
||||||
|
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");
|
||||||
|
if expn.call_site.contains(arg.span) {
|
||||||
|
l.span_suggestion(
|
||||||
|
arg.span.shrink_to_lo(),
|
||||||
|
"add a \"{}\" format string to use the message literally",
|
||||||
|
"\"{}\", ".into(),
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
l.emit();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -393,6 +393,7 @@ symbols! {
|
||||||
copysignf64,
|
copysignf64,
|
||||||
core,
|
core,
|
||||||
core_intrinsics,
|
core_intrinsics,
|
||||||
|
core_panic_macro,
|
||||||
cosf32,
|
cosf32,
|
||||||
cosf64,
|
cosf64,
|
||||||
crate_id,
|
crate_id,
|
||||||
|
@ -1064,6 +1065,7 @@ symbols! {
|
||||||
staticlib,
|
staticlib,
|
||||||
std,
|
std,
|
||||||
std_inject,
|
std_inject,
|
||||||
|
std_panic_macro,
|
||||||
stmt,
|
stmt,
|
||||||
stmt_expr_attributes,
|
stmt_expr_attributes,
|
||||||
stop_after_dataflow,
|
stop_after_dataflow,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue