From 4c85483486172a706ab7fe7a5a7c7c4e6b8ebf3b Mon Sep 17 00:00:00 2001 From: Lena <126529524+acuteenvy@users.noreply.github.com> Date: Sat, 24 Feb 2024 22:36:14 +0100 Subject: [PATCH] Inline `format!` args wherever possible --- examples/list_syntaxes_and_themes.rs | 2 +- src/assets.rs | 9 ++++----- src/assets/build_assets.rs | 4 ++-- src/assets/build_assets/acknowledgements.rs | 4 ++-- src/assets/lazy_theme_set.rs | 2 +- src/bin/bat/assets.rs | 2 +- src/bin/bat/clap_app.rs | 2 +- src/bin/bat/main.rs | 7 ++----- src/decorations.rs | 2 +- src/input.rs | 2 +- src/printer.rs | 10 +++++----- src/style.rs | 2 +- src/syntax_mapping/ignored_suffixes.rs | 4 ++-- src/vscreen.rs | 6 +++--- tests/integration_tests.rs | 10 +++++----- tests/no_duplicate_extensions.rs | 3 +-- tests/tester/mod.rs | 4 ++-- 17 files changed, 35 insertions(+), 40 deletions(-) diff --git a/examples/list_syntaxes_and_themes.rs b/examples/list_syntaxes_and_themes.rs index 43ec1414..0381f000 100644 --- a/examples/list_syntaxes_and_themes.rs +++ b/examples/list_syntaxes_and_themes.rs @@ -13,6 +13,6 @@ fn main() { println!("Themes:"); for theme in printer.themes() { - println!("- {}", theme); + println!("- {theme}"); } } diff --git a/src/assets.rs b/src/assets.rs index 0129f76b..9655553d 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -380,7 +380,7 @@ fn asset_from_contents( } else { bincode::deserialize_from(contents) } - .map_err(|_| format!("Could not parse {}", description).into()) + .map_err(|_| format!("Could not parse {description}").into()) } fn asset_from_cache( @@ -396,7 +396,7 @@ fn asset_from_cache( ) })?; asset_from_contents(&contents[..], description, compressed) - .map_err(|_| format!("Could not parse cached {}", description).into()) + .map_err(|_| format!("Could not parse cached {description}").into()) } #[cfg(target_os = "macos")] @@ -466,7 +466,7 @@ mod tests { let file_path = self.temp_dir.path().join(file_name); { let mut temp_file = File::create(&file_path).unwrap(); - writeln!(temp_file, "{}", first_line).unwrap(); + writeln!(temp_file, "{first_line}").unwrap(); } let input = Input::ordinary_file(&file_path); @@ -514,8 +514,7 @@ mod tests { if !consistent { eprintln!( - "Inconsistent syntax detection:\nFor File: {}\nFor Reader: {}", - as_file, as_reader + "Inconsistent syntax detection:\nFor File: {as_file}\nFor Reader: {as_reader}" ) } diff --git a/src/assets/build_assets.rs b/src/assets/build_assets.rs index f3448679..5406c2f7 100644 --- a/src/assets/build_assets.rs +++ b/src/assets/build_assets.rs @@ -93,7 +93,7 @@ fn print_unlinked_contexts(syntax_set: &SyntaxSet) { if !missing_contexts.is_empty() { println!("Some referenced contexts could not be found!"); for context in missing_contexts { - println!("- {}", context); + println!("- {context}"); } } } @@ -152,7 +152,7 @@ pub(crate) fn asset_to_contents( } else { bincode::serialize_into(&mut contents, asset) } - .map_err(|_| format!("Could not serialize {}", description))?; + .map_err(|_| format!("Could not serialize {description}"))?; Ok(contents) } diff --git a/src/assets/build_assets/acknowledgements.rs b/src/assets/build_assets/acknowledgements.rs index fc915548..c4fde919 100644 --- a/src/assets/build_assets/acknowledgements.rs +++ b/src/assets/build_assets/acknowledgements.rs @@ -80,7 +80,7 @@ fn handle_license(path: &Path) -> Result> { } else if license_not_needed_in_acknowledgements(&license_text) { Ok(None) } else { - Err(format!("ERROR: License is of unknown type: {:?}", path).into()) + Err(format!("ERROR: License is of unknown type: {path:?}").into()) } } @@ -125,7 +125,7 @@ fn append_to_acknowledgements( relative_path: &str, license_text: &str, ) { - write!(acknowledgements, "## {}\n\n{}", relative_path, license_text).ok(); + write!(acknowledgements, "## {relative_path}\n\n{license_text}").ok(); // Make sure the last char is a newline to not mess up formatting later if acknowledgements diff --git a/src/assets/lazy_theme_set.rs b/src/assets/lazy_theme_set.rs index fcc3eb46..f3f3f690 100644 --- a/src/assets/lazy_theme_set.rs +++ b/src/assets/lazy_theme_set.rs @@ -88,7 +88,7 @@ impl TryFrom for LazyThemeSet { let lazy_theme = LazyTheme { serialized: crate::assets::build_assets::asset_to_contents( &theme, - &format!("theme {}", name), + &format!("theme {name}"), COMPRESS_LAZY_THEMES, )?, deserialized: OnceCell::new(), diff --git a/src/bin/bat/assets.rs b/src/bin/bat/assets.rs index a54927b8..0eeb5bf1 100644 --- a/src/bin/bat/assets.rs +++ b/src/bin/bat/assets.rs @@ -44,7 +44,7 @@ pub fn assets_from_cache_or_binary( } fn clear_asset(path: PathBuf, description: &str) { - print!("Clearing {} ... ", description); + print!("Clearing {description} ... "); match fs::remove_file(&path) { Err(err) if err.kind() == io::ErrorKind::NotFound => { println!("skipped (not present)"); diff --git a/src/bin/bat/clap_app.rs b/src/bin/bat/clap_app.rs index ac7f5c18..b82762b6 100644 --- a/src/bin/bat/clap_app.rs +++ b/src/bin/bat/clap_app.rs @@ -430,7 +430,7 @@ pub fn build_app(interactive_output: bool) -> Command { }); if let Some(invalid) = invalid_vals.next() { - Err(format!("Unknown style, '{}'", invalid)) + Err(format!("Unknown style, '{invalid}'")) } else { Ok(val.to_owned()) } diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs index d877bb9b..38595721 100644 --- a/src/bin/bat/main.rs +++ b/src/bin/bat/main.rs @@ -222,7 +222,7 @@ pub fn list_themes(cfg: &Config, config_dir: &Path, cache_dir: &Path) -> Result< )?; } else { for theme in assets.themes() { - writeln!(stdout, "{}", theme)?; + writeln!(stdout, "{theme}")?; } } @@ -232,10 +232,7 @@ pub fn list_themes(cfg: &Config, config_dir: &Path, cache_dir: &Path) -> Result< fn set_terminal_title_to(new_terminal_title: String) { let osc_command_for_setting_terminal_title = "\x1b]0;"; let osc_end_command = "\x07"; - print!( - "{}{}{}", - osc_command_for_setting_terminal_title, new_terminal_title, osc_end_command - ); + print!("{osc_command_for_setting_terminal_title}{new_terminal_title}{osc_end_command}"); io::stdout().flush().unwrap(); } diff --git a/src/decorations.rs b/src/decorations.rs index 85d8103a..5b7846c3 100644 --- a/src/decorations.rs +++ b/src/decorations.rs @@ -56,7 +56,7 @@ impl Decoration for LineNumberDecoration { self.cached_wrap.clone() } else { - let plain: String = format!("{:4}", line_number); + let plain: String = format!("{line_number:4}"); DecorationText { width: plain.len(), text: self.color.paint(plain).to_string(), diff --git a/src/input.rs b/src/input.rs index ccab98bf..0ebaa4ce 100644 --- a/src/input.rs +++ b/src/input.rs @@ -197,7 +197,7 @@ impl<'a> Input<'a> { InputKind::StdIn => { if let Some(stdout) = stdout_identifier { let input_identifier = Identifier::try_from(clircle::Stdio::Stdin) - .map_err(|e| format!("Stdin: Error identifying file: {}", e))?; + .map_err(|e| format!("Stdin: Error identifying file: {e}"))?; if stdout.surely_conflicts_with(&input_identifier) { return Err("IO circle detected. The input from stdin is also an output. Aborting to avoid infinite loop.".into()); } diff --git a/src/printer.rs b/src/printer.rs index fc6c16f0..8dbe9d0b 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -160,7 +160,7 @@ impl<'a> Printer for SimplePrinter<'a> { self.config.tab_width, self.config.nonprintable_notation, ); - write!(handle, "{}", line)?; + write!(handle, "{line}")?; } else { match handle { OutputHandle::IoWrite(handle) => handle.write_all(line_buffer)?, @@ -333,7 +333,7 @@ impl<'a> InteractivePrinter<'a> { " ".repeat(self.panel_width - 1 - text_truncated.len()) ); if self.config.style_components.grid() { - format!("{} │ ", text_filled) + format!("{text_filled} │ ") } else { text_filled } @@ -368,7 +368,7 @@ impl<'a> InteractivePrinter<'a> { content: &str, ) -> Result<()> { self.print_header_component_indent(handle)?; - writeln!(handle, "{}", content) + writeln!(handle, "{content}") } fn print_header_multiline_component( @@ -494,7 +494,7 @@ impl<'a> Printer for InteractivePrinter<'a> { "{}{}{}", description .kind() - .map(|kind| format!("{}: ", kind)) + .map(|kind| format!("{kind}: ")) .unwrap_or_else(|| "".into()), self.colors.header_value.paint(description.title()), mode @@ -552,7 +552,7 @@ impl<'a> Printer for InteractivePrinter<'a> { "{}", self.colors .grid - .paint(format!("{}{}{}{}", panel, snip_left, title, snip_right)) + .paint(format!("{panel}{snip_left}{title}{snip_right}")) )?; Ok(()) diff --git a/src/style.rs b/src/style.rs index 0f83d1bc..652b3743 100644 --- a/src/style.rs +++ b/src/style.rs @@ -80,7 +80,7 @@ impl FromStr for StyleComponent { "full" => Ok(StyleComponent::Full), "default" => Ok(StyleComponent::Default), "plain" => Ok(StyleComponent::Plain), - _ => Err(format!("Unknown style '{}'", s).into()), + _ => Err(format!("Unknown style '{s}'").into()), } } } diff --git a/src/syntax_mapping/ignored_suffixes.rs b/src/syntax_mapping/ignored_suffixes.rs index 6ee7a598..a6d38e8f 100644 --- a/src/syntax_mapping/ignored_suffixes.rs +++ b/src/syntax_mapping/ignored_suffixes.rs @@ -73,7 +73,7 @@ fn internal_suffixes() { let file_names = ignored_suffixes .values .iter() - .map(|suffix| format!("test.json{}", suffix)); + .map(|suffix| format!("test.json{suffix}")); for file_name_str in file_names { let file_name = OsStr::new(&file_name_str); let expected_stripped_file_name = OsStr::new("test.json"); @@ -95,7 +95,7 @@ fn external_suffixes() { let file_names = ignored_suffixes .values .iter() - .map(|suffix| format!("test.json{}", suffix)); + .map(|suffix| format!("test.json{suffix}")); for file_name_str in file_names { let file_name = OsStr::new(&file_name_str); let expected_stripped_file_name = OsStr::new("test.json"); diff --git a/src/vscreen.rs b/src/vscreen.rs index f7ba3f91..e211c2aa 100644 --- a/src/vscreen.rs +++ b/src/vscreen.rs @@ -221,11 +221,11 @@ impl Attributes { 8 => match parameters.next() { Some(5) /* 256-color */ => format!("\x1B[{};5;{}m", color, join(";", 1, parameters)), Some(2) /* 24-bit color */ => format!("\x1B[{};2;{}m", color, join(";", 3, parameters)), - Some(c) => format!("\x1B[{};{}m", color, c), + Some(c) => format!("\x1B[{color};{c}m"), _ => "".to_owned(), }, 9 => "".to_owned(), - _ => format!("\x1B[{}m", color), + _ => format!("\x1B[{color}m"), } } @@ -435,7 +435,7 @@ impl<'a> EscapeSequenceOffsetsIterator<'a> { } Some((_, tc)) => { - panic!("this should not be reached: char {:?}", tc) + panic!("this should not be reached: char {tc:?}") } } } diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 61537bee..d6523366 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1758,7 +1758,7 @@ fn do_not_panic_regression_tests() { ] { bat() .arg("--color=always") - .arg(&format!("regression_tests/{}", filename)) + .arg(&format!("regression_tests/{filename}")) .assert() .success(); } @@ -1771,7 +1771,7 @@ fn do_not_detect_different_syntax_for_stdin_and_files() { let cmd_for_file = bat() .arg("--color=always") .arg("--map-syntax=*.js:Markdown") - .arg(&format!("--file-name={}", file)) + .arg(&format!("--file-name={file}")) .arg("--style=plain") .arg(file) .assert() @@ -1781,7 +1781,7 @@ fn do_not_detect_different_syntax_for_stdin_and_files() { .arg("--color=always") .arg("--map-syntax=*.js:Markdown") .arg("--style=plain") - .arg(&format!("--file-name={}", file)) + .arg(&format!("--file-name={file}")) .pipe_stdin(Path::new(EXAMPLES_DIR).join(file)) .unwrap() .assert() @@ -1800,7 +1800,7 @@ fn no_first_line_fallback_when_mapping_to_invalid_syntax() { bat() .arg("--color=always") .arg("--map-syntax=*.invalid-syntax:InvalidSyntax") - .arg(&format!("--file-name={}", file)) + .arg(&format!("--file-name={file}")) .arg("--style=plain") .arg(file) .assert() @@ -1998,7 +1998,7 @@ fn ansi_passthrough_emit() { .arg("--paging=never") .arg("--color=never") .arg("--terminal-width=80") - .arg(format!("--wrap={}", wrapping)) + .arg(format!("--wrap={wrapping}")) .arg("--decorations=always") .arg("--style=plain") .write_stdin("\x1B[33mColor\nColor \x1B[m\nPlain\n") diff --git a/tests/no_duplicate_extensions.rs b/tests/no_duplicate_extensions.rs index c28a9c6a..1872f0df 100644 --- a/tests/no_duplicate_extensions.rs +++ b/tests/no_duplicate_extensions.rs @@ -30,8 +30,7 @@ fn no_duplicate_extensions() { for extension in &syntax.file_extensions { assert!( KNOWN_EXCEPTIONS.contains(&extension.as_str()) || extensions.insert(extension), - "File extension / pattern \"{}\" appears twice in the syntax set", - extension + "File extension / pattern \"{extension}\" appears twice in the syntax set" ); } } diff --git a/tests/tester/mod.rs b/tests/tester/mod.rs index c4e916a6..91fa40df 100644 --- a/tests/tester/mod.rs +++ b/tests/tester/mod.rs @@ -29,7 +29,7 @@ impl BatTester { "--color=never", "--decorations=always", "--terminal-width=80", - &format!("--style={}", style), + &format!("--style={style}"), ]) .output() .expect("bat failed"); @@ -40,7 +40,7 @@ impl BatTester { .replace("tests/snapshots/", ""); let mut expected = String::new(); - let mut file = File::open(format!("tests/snapshots/output/{}.snapshot.txt", name)) + let mut file = File::open(format!("tests/snapshots/output/{name}.snapshot.txt")) .expect("snapshot file missing"); file.read_to_string(&mut expected) .expect("could not read snapshot file");