uninlined_format_args: Ignore assert! and debug_assert! before 2021 edition

This commit is contained in:
Taiki Endo 2022-12-10 18:35:24 +09:00
parent ef2018cc49
commit e5010c996e
6 changed files with 45 additions and 4 deletions

View file

@ -2,7 +2,8 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::is_diag_trait_item;
use clippy_utils::macros::FormatParamKind::{Implicit, Named, NamedInline, Numbered, Starred};
use clippy_utils::macros::{
is_format_macro, is_panic, root_macro_call, Count, FormatArg, FormatArgsExpn, FormatParam, FormatParamUsage,
is_assert_macro, is_format_macro, is_panic, root_macro_call, Count, FormatArg, FormatArgsExpn, FormatParam,
FormatParamUsage,
};
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::snippet_opt;
@ -290,8 +291,9 @@ fn check_uninlined_args(
if args.format_string.span.from_expansion() {
return;
}
if call_site.edition() < Edition2021 && is_panic(cx, def_id) {
// panic! before 2021 edition considers a single string argument as non-format
if call_site.edition() < Edition2021 && (is_panic(cx, def_id) || is_assert_macro(cx, def_id)) {
// panic!, assert!, and debug_assert! before 2021 edition considers a single string argument as
// non-format
return;
}

View file

@ -208,6 +208,12 @@ pub fn is_panic(cx: &LateContext<'_>, def_id: DefId) -> bool {
)
}
/// Is `def_id` of `assert!` or `debug_assert!`
pub fn is_assert_macro(cx: &LateContext<'_>, def_id: DefId) -> bool {
let Some(name) = cx.tcx.get_diagnostic_name(def_id) else { return false };
matches!(name, sym::assert_macro | sym::debug_assert_macro)
}
pub enum PanicExpn<'a> {
/// No arguments - `panic!()`
Empty,

View file

@ -26,4 +26,7 @@ fn main() {
panic!("p4 {var}");
}
}
assert!(var == 1, "p5 {}", var);
debug_assert!(var == 1, "p6 {}", var);
}

View file

@ -26,4 +26,7 @@ fn main() {
panic!("p4 {var}");
}
}
assert!(var == 1, "p5 {var}");
debug_assert!(var == 1, "p6 {var}");
}

View file

@ -47,5 +47,29 @@ LL - panic!("p3 {var}", var = var);
LL + panic!("p3 {var}");
|
error: aborting due to 4 previous errors
error: variables can be used directly in the `format!` string
--> $DIR/uninlined_format_args_panic.rs:30:5
|
LL | assert!(var == 1, "p5 {}", var);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: change this to
|
LL - assert!(var == 1, "p5 {}", var);
LL + assert!(var == 1, "p5 {var}");
|
error: variables can be used directly in the `format!` string
--> $DIR/uninlined_format_args_panic.rs:31:5
|
LL | debug_assert!(var == 1, "p6 {}", var);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: change this to
|
LL - debug_assert!(var == 1, "p6 {}", var);
LL + debug_assert!(var == 1, "p6 {var}");
|
error: aborting due to 6 previous errors

View file

@ -26,4 +26,7 @@ fn main() {
panic!("p4 {var}");
}
}
assert!(var == 1, "p5 {}", var);
debug_assert!(var == 1, "p6 {}", var);
}