Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uninlined_format_args: Ignore assert! and debug_assert! before 2021 edition #10055

Merged
merged 1 commit into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions clippy_lints/src/format_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions clippy_utils/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/uninlined_format_args_panic.edition2018.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ fn main() {
panic!("p4 {var}");
}
}

assert!(var == 1, "p5 {}", var);
debug_assert!(var == 1, "p6 {}", var);
}
3 changes: 3 additions & 0 deletions tests/ui/uninlined_format_args_panic.edition2021.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ fn main() {
panic!("p4 {var}");
}
}

assert!(var == 1, "p5 {var}");
debug_assert!(var == 1, "p6 {var}");
}
26 changes: 25 additions & 1 deletion tests/ui/uninlined_format_args_panic.edition2021.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

3 changes: 3 additions & 0 deletions tests/ui/uninlined_format_args_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ fn main() {
panic!("p4 {var}");
}
}

assert!(var == 1, "p5 {}", var);
debug_assert!(var == 1, "p6 {}", var);
}