Skip to content

Commit 3a0ff58

Browse files
committed
inline format arguments
In a few cases, removed the unneeded `&` - this causes a minor slowdown because compiler cannot eliminate those (yet).
1 parent 4998f58 commit 3a0ff58

14 files changed

+31
-39
lines changed

assets/theme_preview.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Output the square of a number.
22
fn print_square(num: f64) {
33
let result = f64::powf(num, 2.0);
4-
println!("The square of {:.2} is {:.2}.", num, result);
4+
println!("The square of {num:.2} is {result:.2}.");
55
}

src/assets.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@ impl HighlightingAssets {
191191
Some(theme) => theme,
192192
None => {
193193
if theme == "ansi-light" || theme == "ansi-dark" {
194-
bat_warning!("Theme '{}' is deprecated, using 'ansi' instead.", theme);
194+
bat_warning!("Theme '{theme}' is deprecated, using 'ansi' instead.");
195195
return self.get_theme("ansi");
196196
}
197197
if !theme.is_empty() {
198-
bat_warning!("Unknown theme '{}', using default.", theme)
198+
bat_warning!("Unknown theme '{theme}', using default.")
199199
}
200200
self.get_theme_set()
201201
.get(
@@ -343,8 +343,7 @@ fn asset_from_cache<T: serde::de::DeserializeOwned>(
343343
) -> Result<T> {
344344
let contents = fs::read(path).map_err(|_| {
345345
format!(
346-
"Could not load cached {} '{}'",
347-
description,
346+
"Could not load cached {description} '{}'",
348347
path.to_string_lossy()
349348
)
350349
})?;

src/assets/build_assets.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ fn build_theme_set(source_dir: &Path, include_integrated_assets: bool) -> Result
4747
let res = theme_set.add_from_folder(&theme_dir);
4848
if let Err(err) = res {
4949
println!(
50-
"Failed to load one or more themes from '{}' (reason: '{}')",
50+
"Failed to load one or more themes from '{}' (reason: '{err}')",
5151
theme_dir.to_string_lossy(),
52-
err,
5352
);
5453
}
5554
} else {
@@ -162,12 +161,11 @@ fn asset_to_cache<T: serde::Serialize>(
162161
description: &str,
163162
compressed: bool,
164163
) -> Result<()> {
165-
print!("Writing {} to {} ... ", description, path.to_string_lossy());
164+
print!("Writing {description} to {} ... ", path.to_string_lossy());
166165
let contents = asset_to_contents(asset, description, compressed)?;
167166
std::fs::write(path, &contents[..]).map_err(|_| {
168167
format!(
169-
"Could not save {} to {}",
170-
description,
168+
"Could not save {description} to {}",
171169
path.to_string_lossy()
172170
)
173171
})?;

src/bin/bat/assets.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn clear_asset(path: PathBuf, description: &str) {
5050
println!("skipped (not present)");
5151
}
5252
Err(err) => {
53-
println!("could not remove the cache file {:?}: {}", &path, err);
53+
println!("could not remove the cache file {path:?}: {err}");
5454
}
5555
Ok(_) => println!("okay"),
5656
}

src/bin/bat/clap_app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static VERSION: Lazy<String> = Lazy::new(|| {
1616
if git_version.is_empty() {
1717
crate_version!().to_string()
1818
} else {
19-
format!("{} ({})", crate_version!(), git_version)
19+
format!("{} ({git_version})", crate_version!())
2020
}
2121
});
2222

src/bin/bat/config.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ pub fn generate_config_file() -> bat::error::Result<()> {
8888

8989
fs::write(&config_file, default_config).map_err(|e| {
9090
format!(
91-
"Failed to create config file at '{}': {}",
91+
"Failed to create config file at '{}': {e}",
9292
config_file.to_string_lossy(),
93-
e
9493
)
9594
})?;
9695

src/bin/bat/main.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub fn get_languages(config: &Config, cache_dir: &Path) -> Result<String> {
161161
};
162162

163163
for lang in languages {
164-
write!(result, "{:width$}{}", lang.name, separator, width = longest).ok();
164+
write!(result, "{:width$}{separator}", lang.name, width = longest).ok();
165165

166166
// Number of characters on this line so far, wrap before `desired_width`
167167
let mut num_chars = 0;
@@ -172,7 +172,7 @@ pub fn get_languages(config: &Config, cache_dir: &Path) -> Result<String> {
172172
let new_chars = word.len() + comma_separator.len();
173173
if num_chars + new_chars >= desired_width {
174174
num_chars = 0;
175-
write!(result, "\n{:width$}{}", "", separator, width = longest).ok();
175+
write!(result, "\n{:width$}{separator}", "", width = longest).ok();
176176
}
177177

178178
num_chars += new_chars;
@@ -222,9 +222,8 @@ pub fn list_themes(
222222
if config.colored_output {
223223
writeln!(
224224
stdout,
225-
"Theme: {}{}\n",
225+
"Theme: {}{default_theme_info}\n",
226226
Style::new().bold().paint(theme.to_string()),
227-
default_theme_info
228227
)?;
229228
config.theme = theme.to_string();
230229
Controller::new(&config, &assets)
@@ -358,7 +357,7 @@ fn run() -> Result<bool> {
358357
"fish" => println!("{}", completions::FISH_COMPLETION),
359358
"ps1" => println!("{}", completions::PS1_COMPLETION),
360359
"zsh" => println!("{}", completions::ZSH_COMPLETION),
361-
_ => unreachable!("No completion for shell '{}' available.", shell),
360+
_ => unreachable!("No completion for shell '{shell}' available."),
362361
}
363362
return Ok(true);
364363
}

src/error.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,13 @@ pub fn default_error_handler(error: &Error, output: &mut dyn Write) {
6060
Error::SerdeYamlError(_) => {
6161
writeln!(
6262
output,
63-
"{}: Error while parsing metadata.yaml file: {}",
63+
"{}: Error while parsing metadata.yaml file: {error}",
6464
Red.paint("[bat error]"),
65-
error
6665
)
6766
.ok();
6867
}
6968
_ => {
70-
writeln!(output, "{}: {}", Red.paint("[bat error]"), error).ok();
69+
writeln!(output, "{}: {error}", Red.paint("[bat error]")).ok();
7170
}
7271
};
7372
}

src/input.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,14 @@ impl<'a> Input<'a> {
217217
metadata: self.metadata,
218218
reader: {
219219
let mut file = File::open(&path)
220-
.map_err(|e| format!("'{}': {}", path.to_string_lossy(), e))?;
220+
.map_err(|e| format!("'{}': {e}", path.to_string_lossy()))?;
221221
if file.metadata()?.is_dir() {
222222
return Err(format!("'{}' is a directory.", path.to_string_lossy()).into());
223223
}
224224

225225
if let Some(stdout) = stdout_identifier {
226226
let input_identifier = Identifier::try_from(file).map_err(|e| {
227-
format!("{}: Error identifying file: {}", path.to_string_lossy(), e)
227+
format!("{}: Error identifying file: {e}", path.to_string_lossy())
228228
})?;
229229
if stdout.surely_conflicts_with(&input_identifier) {
230230
return Err(format!(

src/lessopen.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl LessOpenPreprocessor {
3939
// Note that $LESSCLOSE has no such requirement
4040
if lessopen.match_indices("%s").count() != 1 {
4141
let error_msg = "LESSOPEN ignored: must contain exactly one %s";
42-
bat_warning!("{}", error_msg);
42+
bat_warning!("{error_msg}");
4343
return Err(error_msg.into());
4444
}
4545

@@ -110,7 +110,7 @@ impl LessOpenPreprocessor {
110110
if self.preprocess_stdin {
111111
if let Some(stdout) = stdout_identifier {
112112
let input_identifier = Identifier::try_from(clircle::Stdio::Stdin)
113-
.map_err(|e| format!("Stdin: Error identifying file: {}", e))?;
113+
.map_err(|e| format!("Stdin: Error identifying file: {e}"))?;
114114
if stdout.surely_conflicts_with(&input_identifier) {
115115
return Err("IO circle detected. The input from stdin is also an output. Aborting to avoid infinite loop.".into());
116116
}

src/printer.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl<'a> InteractivePrinter<'a> {
341341
self.print_horizontal_line_term(handle, self.colors.grid)?;
342342
} else {
343343
let hline = "─".repeat(self.config.term_width - (self.panel_width + 1));
344-
let hline = format!("{}{}{}", "─".repeat(self.panel_width), grid_char, hline);
344+
let hline = format!("{}{grid_char}{hline}", "─".repeat(self.panel_width));
345345
writeln!(handle, "{}", self.colors.grid.paint(hline))?;
346346
}
347347

@@ -355,8 +355,7 @@ impl<'a> InteractivePrinter<'a> {
355355

356356
let text_truncated: String = text.chars().take(self.panel_width - 1).collect();
357357
let text_filled: String = format!(
358-
"{}{}",
359-
text_truncated,
358+
"{text_truncated}{}",
360359
" ".repeat(self.panel_width - 1 - text_truncated.len())
361360
);
362361
if self.config.style_components.grid() {
@@ -521,13 +520,12 @@ impl Printer for InteractivePrinter<'_> {
521520
.try_for_each(|component| match component {
522521
StyleComponent::HeaderFilename => {
523522
let header_filename = format!(
524-
"{}{}{}",
523+
"{}{}{mode}",
525524
description
526525
.kind()
527526
.map(|kind| format!("{kind}: "))
528527
.unwrap_or_else(|| "".into()),
529528
self.colors.header_value.paint(description.title()),
530-
mode
531529
);
532530
self.print_header_multiline_component(handle, &header_filename)
533531
}
@@ -704,7 +702,7 @@ impl Printer for InteractivePrinter<'_> {
704702
"{}{}",
705703
as_terminal_escaped(
706704
style,
707-
&format!("{}{}", self.ansi_style, text_trimmed),
705+
&format!("{}{text_trimmed}", self.ansi_style),
708706
true_color,
709707
colored_output,
710708
italics,
@@ -794,7 +792,7 @@ impl Printer for InteractivePrinter<'_> {
794792
"{}{}\n{}",
795793
as_terminal_escaped(
796794
style,
797-
&format!("{}{}", self.ansi_style, line_buf),
795+
&format!("{}{line_buf}", self.ansi_style),
798796
self.config.true_color,
799797
self.config.colored_output,
800798
self.config.use_italic_text,
@@ -821,7 +819,7 @@ impl Printer for InteractivePrinter<'_> {
821819
"{}",
822820
as_terminal_escaped(
823821
style,
824-
&format!("{}{}", self.ansi_style, line_buf),
822+
&format!("{}{line_buf}", self.ansi_style),
825823
self.config.true_color,
826824
self.config.colored_output,
827825
self.config.use_italic_text,

src/vscreen.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,15 @@ impl Attributes {
212212
}
213213

214214
fn update_with_charset(&mut self, kind: char, set: impl Iterator<Item = char>) -> bool {
215-
self.charset = format!("\x1B{}{}", kind, set.take(1).collect::<String>());
215+
self.charset = format!("\x1B{kind}{}", set.take(1).collect::<String>());
216216
true
217217
}
218218

219219
fn parse_color(color: u16, parameters: &mut dyn Iterator<Item = u16>) -> String {
220220
match color % 10 {
221221
8 => match parameters.next() {
222-
Some(5) /* 256-color */ => format!("\x1B[{};5;{}m", color, join(";", 1, parameters)),
223-
Some(2) /* 24-bit color */ => format!("\x1B[{};2;{}m", color, join(";", 3, parameters)),
222+
Some(5) /* 256-color */ => format!("\x1B[{color};5;{}m", join(";", 1, parameters)),
223+
Some(2) /* 24-bit color */ => format!("\x1B[{color};2;{}m", join(";", 3, parameters)),
224224
Some(c) => format!("\x1B[{color};{c}m"),
225225
_ => "".to_owned(),
226226
},

tests/tester/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn create_sample_directory() -> Result<TempDir, git2::Error> {
7575

7676
// Copy over `sample.rs`
7777
let sample_path = temp_dir.path().join("sample.rs");
78-
println!("{:?}", &sample_path);
78+
println!("{sample_path:?}");
7979
fs::copy("tests/snapshots/sample.rs", &sample_path).expect("successful copy");
8080

8181
// Commit

tests/utils/mocked_pagers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn get_mocked_pagers_dir() -> PathBuf {
1919
pub fn from(base: &str) -> String {
2020
let mut cmd_and_args = shell_words::split(base).unwrap();
2121
let suffix = if cfg!(windows) { ".bat" } else { "" };
22-
let mut out_cmd = format!("{}{}", cmd_and_args.first().unwrap(), suffix);
22+
let mut out_cmd = format!("{}{suffix}", cmd_and_args.first().unwrap());
2323

2424
if (cmd_and_args.len() > 1) {
2525
out_cmd.push(' ');

0 commit comments

Comments
 (0)