Skip to content

Commit c0130e4

Browse files
committed
Auto merge of #10055 - taiki-e:uninlined_format_args, r=llogiq
uninlined_format_args: Ignore assert! and debug_assert! before 2021 edition Similar to #9605, but for `assert!` and `debug_assert!` macros. ([non_fmt_panics lint triggers them](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=18b20c408ec62a67f1503cd5d284424b)) changelog: [`uninlined_format_args`]: Do not inline `assert!` and `debug_assert!` macros before 2021 edition r? `@llogiq`
2 parents ef2018c + e5010c9 commit c0130e4

6 files changed

+45
-4
lines changed

clippy_lints/src/format_args.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::is_diag_trait_item;
33
use clippy_utils::macros::FormatParamKind::{Implicit, Named, NamedInline, Numbered, Starred};
44
use clippy_utils::macros::{
5-
is_format_macro, is_panic, root_macro_call, Count, FormatArg, FormatArgsExpn, FormatParam, FormatParamUsage,
5+
is_assert_macro, is_format_macro, is_panic, root_macro_call, Count, FormatArg, FormatArgsExpn, FormatParam,
6+
FormatParamUsage,
67
};
78
use clippy_utils::msrvs::{self, Msrv};
89
use clippy_utils::source::snippet_opt;
@@ -290,8 +291,9 @@ fn check_uninlined_args(
290291
if args.format_string.span.from_expansion() {
291292
return;
292293
}
293-
if call_site.edition() < Edition2021 && is_panic(cx, def_id) {
294-
// panic! before 2021 edition considers a single string argument as non-format
294+
if call_site.edition() < Edition2021 && (is_panic(cx, def_id) || is_assert_macro(cx, def_id)) {
295+
// panic!, assert!, and debug_assert! before 2021 edition considers a single string argument as
296+
// non-format
295297
return;
296298
}
297299

clippy_utils/src/macros.rs

+6
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ pub fn is_panic(cx: &LateContext<'_>, def_id: DefId) -> bool {
208208
)
209209
}
210210

211+
/// Is `def_id` of `assert!` or `debug_assert!`
212+
pub fn is_assert_macro(cx: &LateContext<'_>, def_id: DefId) -> bool {
213+
let Some(name) = cx.tcx.get_diagnostic_name(def_id) else { return false };
214+
matches!(name, sym::assert_macro | sym::debug_assert_macro)
215+
}
216+
211217
pub enum PanicExpn<'a> {
212218
/// No arguments - `panic!()`
213219
Empty,

tests/ui/uninlined_format_args_panic.edition2018.fixed

+3
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ fn main() {
2626
panic!("p4 {var}");
2727
}
2828
}
29+
30+
assert!(var == 1, "p5 {}", var);
31+
debug_assert!(var == 1, "p6 {}", var);
2932
}

tests/ui/uninlined_format_args_panic.edition2021.fixed

+3
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ fn main() {
2626
panic!("p4 {var}");
2727
}
2828
}
29+
30+
assert!(var == 1, "p5 {var}");
31+
debug_assert!(var == 1, "p6 {var}");
2932
}

tests/ui/uninlined_format_args_panic.edition2021.stderr

+25-1
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,29 @@ LL - panic!("p3 {var}", var = var);
4747
LL + panic!("p3 {var}");
4848
|
4949

50-
error: aborting due to 4 previous errors
50+
error: variables can be used directly in the `format!` string
51+
--> $DIR/uninlined_format_args_panic.rs:30:5
52+
|
53+
LL | assert!(var == 1, "p5 {}", var);
54+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
55+
|
56+
help: change this to
57+
|
58+
LL - assert!(var == 1, "p5 {}", var);
59+
LL + assert!(var == 1, "p5 {var}");
60+
|
61+
62+
error: variables can be used directly in the `format!` string
63+
--> $DIR/uninlined_format_args_panic.rs:31:5
64+
|
65+
LL | debug_assert!(var == 1, "p6 {}", var);
66+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67+
|
68+
help: change this to
69+
|
70+
LL - debug_assert!(var == 1, "p6 {}", var);
71+
LL + debug_assert!(var == 1, "p6 {var}");
72+
|
73+
74+
error: aborting due to 6 previous errors
5175

tests/ui/uninlined_format_args_panic.rs

+3
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ fn main() {
2626
panic!("p4 {var}");
2727
}
2828
}
29+
30+
assert!(var == 1, "p5 {}", var);
31+
debug_assert!(var == 1, "p6 {}", var);
2932
}

0 commit comments

Comments
 (0)