From 78325817028096ca134b7c2bbd3bc6ca000a8f3e Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 11 Feb 2023 14:33:17 +0200 Subject: chore: use variables in Rust format strings Change this: format!("{}", foo) into this: format!("{foo}") Support for this feature was added in Rust 1.58 (see https://github.com/rust-lang/rust/releases/tag/1.58.0) and in 1.67 clippy suggests about this. Because the new style seems to be where the Rust ecosystem is going, I think Subplot should follow to avoid being needlessly different from most other projects. Sponsored-by: author --- subplotlib/helpers/subplotlib_impl.rs | 2 +- subplotlib/src/scenario.rs | 4 ++-- subplotlib/src/step.rs | 6 +++--- subplotlib/src/steplibrary/datadir.rs | 3 +-- subplotlib/src/steplibrary/files.rs | 2 +- subplotlib/src/steplibrary/runcmd.rs | 26 +++++++++++++------------- subplotlib/subplot-rust-support.rs | 35 ++++++++++++----------------------- 7 files changed, 33 insertions(+), 45 deletions(-) (limited to 'subplotlib') diff --git a/subplotlib/helpers/subplotlib_impl.rs b/subplotlib/helpers/subplotlib_impl.rs index fac54ab..768337a 100644 --- a/subplotlib/helpers/subplotlib_impl.rs +++ b/subplotlib/helpers/subplotlib_impl.rs @@ -36,7 +36,7 @@ fn remember_target(context: &mut Context, somename: &str) { if let Some(file) = context.files.get(somename) { context.this_file = Some(file.clone()); } else { - throw!(format!("Unknown file {}", somename)); + throw!(format!("Unknown file {somename}")); } } diff --git a/subplotlib/src/scenario.rs b/subplotlib/src/scenario.rs index 7a78ae5..8c734b9 100644 --- a/subplotlib/src/scenario.rs +++ b/subplotlib/src/scenario.rs @@ -174,9 +174,9 @@ impl DebuggedContext { C: Debug, { let body = if alternate { - format!("{:#?}", obj) + format!("{obj:#?}") } else { - format!("{:?}", obj) + format!("{obj:?}") }; self.body.push(body); } diff --git a/subplotlib/src/step.rs b/subplotlib/src/step.rs index 7a70e23..12b4ddf 100644 --- a/subplotlib/src/step.rs +++ b/subplotlib/src/step.rs @@ -57,11 +57,11 @@ impl ScenarioStep { /// a formatted string then we won't be able to render it sadly. fn render_panic(name: &str, err: Box) -> String { if let Some(msg) = err.downcast_ref::<&str>() { - format!("step {} panic'd: {}", name, msg) + format!("step {name} panic'd: {msg}") } else if let Some(msg) = err.downcast_ref::() { - format!("step {} panic'd: {}", name, msg) + format!("step {name} panic'd: {msg}") } else { - format!("step {} panic'd", name) + format!("step {name} panic'd") } } diff --git a/subplotlib/src/steplibrary/datadir.rs b/subplotlib/src/steplibrary/datadir.rs index 5a344df..5060f63 100644 --- a/subplotlib/src/steplibrary/datadir.rs +++ b/subplotlib/src/steplibrary/datadir.rs @@ -150,8 +150,7 @@ pub fn datadir_has_enough_space(datadir: &Datadir, bytes: u64) { let available = fs2::available_space(datadir.base_path())?; if available < bytes { throw!(format!( - "Available space check failed, wanted {} bytes, but only {} were available", - bytes, available + "Available space check failed, wanted {bytes} bytes, but only {available} were available", )); } } diff --git a/subplotlib/src/steplibrary/files.rs b/subplotlib/src/steplibrary/files.rs index 8abe546..c60358f 100644 --- a/subplotlib/src/steplibrary/files.rs +++ b/subplotlib/src/steplibrary/files.rs @@ -90,7 +90,7 @@ pub fn touch_with_timestamp(context: &Datadir, filename: &Path, mtime: &str) { let fd = format_description!( "[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour]:[offset_minute]" ); - let full_time = format!("{} +00:00", mtime); + let full_time = format!("{mtime} +00:00"); let ts = OffsetDateTime::parse(&full_time, &fd)?; let (secs, nanos) = (ts.unix_timestamp(), 0); let mtime = FileTime::from_unix_time(secs, nanos); diff --git a/subplotlib/src/steplibrary/runcmd.rs b/subplotlib/src/steplibrary/runcmd.rs index 6692441..99605a3 100644 --- a/subplotlib/src/steplibrary/runcmd.rs +++ b/subplotlib/src/steplibrary/runcmd.rs @@ -353,7 +353,7 @@ pub fn exit_code_is(context: &Runcmd, exit: i32) { #[step] pub fn exit_code_is_not(context: &Runcmd, exit: i32) { if context.exitcode.is_none() || context.exitcode == Some(exit) { - throw!(format!("Expected exit code to not equal {}", exit)); + throw!(format!("Expected exit code to not equal {exit}")); } } @@ -422,7 +422,7 @@ fn check_matches(runcmd: &Runcmd, which: Stream, how: MatchKind, against: &str) #[step] pub fn stdout_is(runcmd: &Runcmd, text: &str) { if !check_matches(runcmd, Stream::Stdout, MatchKind::Exact, text)? { - throw!(format!("stdout is not {:?}", text)); + throw!(format!("stdout is not {text:?}")); } } @@ -435,7 +435,7 @@ pub fn stdout_is(runcmd: &Runcmd, text: &str) { #[step] pub fn stdout_isnt(runcmd: &Runcmd, text: &str) { if check_matches(runcmd, Stream::Stdout, MatchKind::Exact, text)? { - throw!(format!("stdout is exactly {:?}", text)); + throw!(format!("stdout is exactly {text:?}")); } } @@ -448,7 +448,7 @@ pub fn stdout_isnt(runcmd: &Runcmd, text: &str) { #[step] pub fn stderr_is(runcmd: &Runcmd, text: &str) { if !check_matches(runcmd, Stream::Stderr, MatchKind::Exact, text)? { - throw!(format!("stderr is not {:?}", text)); + throw!(format!("stderr is not {text:?}")); } } @@ -461,7 +461,7 @@ pub fn stderr_is(runcmd: &Runcmd, text: &str) { #[step] pub fn stderr_isnt(runcmd: &Runcmd, text: &str) { if check_matches(runcmd, Stream::Stderr, MatchKind::Exact, text)? { - throw!(format!("stderr is exactly {:?}", text)); + throw!(format!("stderr is exactly {text:?}")); } } @@ -474,7 +474,7 @@ pub fn stderr_isnt(runcmd: &Runcmd, text: &str) { #[step] pub fn stdout_contains(runcmd: &Runcmd, text: &str) { if !check_matches(runcmd, Stream::Stdout, MatchKind::Contains, text)? { - throw!(format!("stdout does not contain {:?}", text)); + throw!(format!("stdout does not contain {text:?}")); } } @@ -487,7 +487,7 @@ pub fn stdout_contains(runcmd: &Runcmd, text: &str) { #[step] pub fn stdout_doesnt_contain(runcmd: &Runcmd, text: &str) { if check_matches(runcmd, Stream::Stdout, MatchKind::Contains, text)? { - throw!(format!("stdout contains {:?}", text)); + throw!(format!("stdout contains {text:?}")); } } @@ -500,7 +500,7 @@ pub fn stdout_doesnt_contain(runcmd: &Runcmd, text: &str) { #[step] pub fn stderr_contains(runcmd: &Runcmd, text: &str) { if !check_matches(runcmd, Stream::Stderr, MatchKind::Contains, text)? { - throw!(format!("stderr does not contain {:?}", text)); + throw!(format!("stderr does not contain {text:?}")); } } @@ -513,7 +513,7 @@ pub fn stderr_contains(runcmd: &Runcmd, text: &str) { #[step] pub fn stderr_doesnt_contain(runcmd: &Runcmd, text: &str) { if check_matches(runcmd, Stream::Stderr, MatchKind::Contains, text)? { - throw!(format!("stderr contains {:?}", text)); + throw!(format!("stderr contains {text:?}")); } } @@ -527,7 +527,7 @@ pub fn stderr_doesnt_contain(runcmd: &Runcmd, text: &str) { #[step] pub fn stdout_matches_regex(runcmd: &Runcmd, regex: &str) { if !check_matches(runcmd, Stream::Stdout, MatchKind::Regex, regex)? { - throw!(format!("stdout does not match {:?}", regex)); + throw!(format!("stdout does not match {regex:?}")); } } @@ -541,7 +541,7 @@ pub fn stdout_matches_regex(runcmd: &Runcmd, regex: &str) { #[step] pub fn stdout_doesnt_match_regex(runcmd: &Runcmd, regex: &str) { if check_matches(runcmd, Stream::Stdout, MatchKind::Regex, regex)? { - throw!(format!("stdout matches {:?}", regex)); + throw!(format!("stdout matches {regex:?}")); } } @@ -555,7 +555,7 @@ pub fn stdout_doesnt_match_regex(runcmd: &Runcmd, regex: &str) { #[step] pub fn stderr_matches_regex(runcmd: &Runcmd, regex: &str) { if !check_matches(runcmd, Stream::Stderr, MatchKind::Regex, regex)? { - throw!(format!("stderr does not match {:?}", regex)); + throw!(format!("stderr does not match {regex:?}")); } } @@ -569,6 +569,6 @@ pub fn stderr_matches_regex(runcmd: &Runcmd, regex: &str) { #[step] pub fn stderr_doesnt_match_regex(runcmd: &Runcmd, regex: &str) { if check_matches(runcmd, Stream::Stderr, MatchKind::Regex, regex)? { - throw!(format!("stderr matches {:?}", regex)); + throw!(format!("stderr matches {regex:?}")); } } diff --git a/subplotlib/subplot-rust-support.rs b/subplotlib/subplot-rust-support.rs index 4763eff..95dc3f5 100644 --- a/subplotlib/subplot-rust-support.rs +++ b/subplotlib/subplot-rust-support.rs @@ -61,8 +61,6 @@ set -eu exec '{target_path}/{bin_name}' --resources '{src_dir}/share' "$@" "#, target_path = target_path.display(), - bin_name = bin_name, - src_dir = src_dir, ), )?; { @@ -92,21 +90,21 @@ fn uninstall_subplot(context: &mut SubplotContext) { #[step] #[context(Runcmd)] fn scenario_was_run(context: &ScenarioContext, name: &str) { - let text = format!("\nscenario: {}\n", name); + let text = format!("\nscenario: {name}\n"); runcmd::stdout_contains::call(context, &text)?; } #[step] #[context(Runcmd)] fn scenario_was_not_run(context: &ScenarioContext, name: &str) { - let text = format!("\nscenario: {}\n", name); + let text = format!("\nscenario: {name}\n"); runcmd::stdout_doesnt_contain::call(context, &text)?; } #[step] #[context(Runcmd)] fn step_was_run(context: &ScenarioContext, keyword: &str, name: &str) { - let text = format!("\n step: {} {}\n", keyword, name); + let text = format!("\n step: {keyword} {name}\n"); runcmd::stdout_contains::call(context, &text)?; } @@ -119,10 +117,7 @@ fn step_was_run_and_then( keyword2: &str, name2: &str, ) { - let text = format!( - "\n step: {} {}\n step: {} {}", - keyword1, name1, keyword2, name2 - ); + let text = format!("\n step: {keyword1} {name1}\n step: {keyword2} {name2}"); runcmd::stdout_contains::call(context, &text)?; } @@ -135,17 +130,14 @@ fn cleanup_was_run( keyword2: &str, name2: &str, ) { - let text = format!( - "\n cleanup: {} {}\n cleanup: {} {}\n", - keyword1, name1, keyword2, name2 - ); + let text = format!("\n cleanup: {keyword1} {name1}\n cleanup: {keyword2} {name2}\n"); runcmd::stdout_contains::call(context, &text)?; } #[step] #[context(Runcmd)] fn cleanup_was_not_run(context: &ScenarioContext, keyword: &str, name: &str) { - let text = format!("\n cleanup: {} {}\n", keyword, name); + let text = format!("\n cleanup: {keyword} {name}\n"); runcmd::stdout_doesnt_contain::call(context, &text)?; } @@ -162,7 +154,7 @@ fn end_of_file(context: &Datadir, filename: &str, nbytes: usize) -> Vec { fn file_ends_in_zero_newlines(context: &Datadir, filename: &str) { let b = end_of_file(context, filename, 1)?; if b[0] == b'\n' { - throw!(format!("File {} ends in unexpected newline", filename)); + throw!(format!("File {filename} ends in unexpected newline")); } } @@ -171,8 +163,7 @@ fn file_ends_in_one_newline(context: &Datadir, filename: &str) { let b = end_of_file(context, filename, 2)?; if !(b[0] != b'\n' && b[1] == b'\n') { throw!(format!( - "File {} does not end in exactly one newline", - filename + "File {filename} does not end in exactly one newline", )); } } @@ -182,8 +173,7 @@ fn file_ends_in_two_newlines(context: &Datadir, filename: &str) { let b = end_of_file(context, filename, 2)?; if b[0] != b'\n' || b[1] != b'\n' { throw!(format!( - "File {} does not end in exactly two newlines", - filename + "File {filename} does not end in exactly two newlines", )); } } @@ -209,12 +199,11 @@ fn json_output_matches_file(context: &ScenarioContext, filename: &str) { let output: serde_json::Value = serde_json::from_str(&output)?; let fcontent: serde_json::Value = serde_json::from_str(&fcontent)?; println!("########"); - println!("Output:\n{:#}", output); - println!("File:\n{:#}", fcontent); + println!("Output:\n{output:#}"); + println!("File:\n{fcontent:#}"); println!("########"); assert_eq!( output, fcontent, - "Command output does not match the content of {}", - filename + "Command output does not match the content of {filename}", ); } -- cgit v1.2.1