Skip to content

Commit 0dd36a7

Browse files
hovinenbcopybara-github
authored andcommitted
Fix the use of format string arguments in the fail macro.
The recent change to add #[must_use] to the fail macro put the generated code inside a function. This unfortunately does not work with captured format arguments -- that would require a closure rather than a function. This change moves the creation of the message out of the function to the function call site, so nothing needs to be captured. It also adjusts the existing test to exercise the aforementioned issue. PiperOrigin-RevId: 504214102
1 parent 190cd1c commit 0dd36a7

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

googletest/integration_tests/failure_due_to_fail_macro_with_format_arguments.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ mod tests {
2020

2121
#[google_test]
2222
fn just_fails() -> Result<()> {
23-
fail!("Failure message with argument: {}", "An argument").and_log_failure();
23+
let argument = "An argument";
24+
fail!("Failure message with argument: {argument}").and_log_failure();
2425
Ok(())
2526
}
2627
}

googletest/src/assertions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,18 @@ macro_rules! fail {
186186
// We wrap this in a function so that we can annotate it with the must_use attribute.
187187
// must_use on expressions is still experimental.
188188
#[must_use = "The assertion result must be evaluated to affect the test result."]
189-
fn create_fail_result() -> $crate::Result<()> {
189+
fn create_fail_result(message: String) -> $crate::Result<()> {
190190
Err($crate::internal::test_outcome::TestAssertionFailure::create(format!(
191191
"{}\n{}",
192-
format!($($message),*),
192+
message,
193193
$crate::internal::source_location::SourceLocation::new(
194194
file!(),
195195
line!(),
196196
column!(),
197197
),
198198
)))
199199
}
200-
create_fail_result()
200+
create_fail_result(format!($($message),*))
201201
}};
202202

203203
() => { fail!("Test failed") };

0 commit comments

Comments
 (0)