Inline `format!` args wherever possible

This commit is contained in:
Lena 2024-02-24 22:36:14 +01:00 committed by Martin Nordholts
parent 487bed2d95
commit 4c85483486
17 changed files with 35 additions and 40 deletions

View File

@ -13,6 +13,6 @@ fn main() {
println!("Themes:");
for theme in printer.themes() {
println!("- {}", theme);
println!("- {theme}");
}
}

View File

@ -380,7 +380,7 @@ fn asset_from_contents<T: serde::de::DeserializeOwned>(
} 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<T: serde::de::DeserializeOwned>(
@ -396,7 +396,7 @@ fn asset_from_cache<T: serde::de::DeserializeOwned>(
)
})?;
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}"
)
}

View File

@ -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<T: serde::Serialize>(
} else {
bincode::serialize_into(&mut contents, asset)
}
.map_err(|_| format!("Could not serialize {}", description))?;
.map_err(|_| format!("Could not serialize {description}"))?;
Ok(contents)
}

View File

@ -80,7 +80,7 @@ fn handle_license(path: &Path) -> Result<Option<String>> {
} 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

View File

@ -88,7 +88,7 @@ impl TryFrom<ThemeSet> 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(),

View File

@ -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)");

View File

@ -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())
}

View File

@ -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();
}

View File

@ -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(),

View File

@ -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());
}

View File

@ -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(())

View File

@ -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()),
}
}
}

View File

@ -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");

View File

@ -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:?}")
}
}
}

View File

@ -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")

View File

@ -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"
);
}
}

View File

@ -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");