Fix clippy suggestions

Leads to a performance improvement for `bat -A`:

    | Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
    |:---|---:|---:|---:|---:|
    | `./bat-master --no-config -A ./bat-master` | 259.8 ± 1.1 | 258.4 | 261.7 | 1.15 ± 0.01 |
    | `./bat-2301 --no-config -A ./bat-master` | 225.6 ± 1.8 | 224.0 | 229.5 | 1.00 |
This commit is contained in:
David Peter 2022-09-04 00:02:08 +02:00 committed by David Peter
parent eab1c9eb46
commit 48541b8507
8 changed files with 15 additions and 11 deletions

View File

@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
use crate::error::*; use crate::error::*;
#[derive(Debug, PartialEq, Default, Serialize, Deserialize)] #[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct AssetsMetadata { pub struct AssetsMetadata {
bat_version: Option<String>, bat_version: Option<String>,
creation_time: Option<SystemTime>, creation_time: Option<SystemTime>,

View File

@ -1,3 +1,4 @@
use std::fmt::Write;
use std::fs::read_to_string; use std::fs::read_to_string;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -124,7 +125,7 @@ fn append_to_acknowledgements(
relative_path: &str, relative_path: &str,
license_text: &str, license_text: &str,
) { ) {
acknowledgements.push_str(&format!("## {}\n\n{}", relative_path, license_text)); write!(acknowledgements, "## {}\n\n{}", relative_path, license_text).ok();
// Make sure the last char is a newline to not mess up formatting later // Make sure the last char is a newline to not mess up formatting later
if acknowledgements if acknowledgements

View File

@ -8,6 +8,7 @@ mod directories;
mod input; mod input;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::fmt::Write as _;
use std::io; use std::io;
use std::io::{BufReader, Write}; use std::io::{BufReader, Write};
use std::path::Path; use std::path::Path;
@ -125,7 +126,7 @@ pub fn get_languages(config: &Config) -> Result<String> {
if config.loop_through { if config.loop_through {
for lang in languages { for lang in languages {
result += &format!("{}:{}\n", lang.name, lang.file_extensions.join(",")); writeln!(result, "{}:{}", lang.name, lang.file_extensions.join(",")).ok();
} }
} else { } else {
let longest = languages let longest = languages
@ -146,7 +147,7 @@ pub fn get_languages(config: &Config) -> Result<String> {
}; };
for lang in languages { for lang in languages {
result += &format!("{:width$}{}", lang.name, separator, width = longest); write!(result, "{:width$}{}", lang.name, separator, width = longest).ok();
// Number of characters on this line so far, wrap before `desired_width` // Number of characters on this line so far, wrap before `desired_width`
let mut num_chars = 0; let mut num_chars = 0;
@ -157,11 +158,11 @@ pub fn get_languages(config: &Config) -> Result<String> {
let new_chars = word.len() + comma_separator.len(); let new_chars = word.len() + comma_separator.len();
if num_chars + new_chars >= desired_width { if num_chars + new_chars >= desired_width {
num_chars = 0; num_chars = 0;
result += &format!("\n{:width$}{}", "", separator, width = longest); write!(result, "\n{:width$}{}", "", separator, width = longest).ok();
} }
num_chars += new_chars; num_chars += new_chars;
result += &format!("{}", style.paint(&word[..])); write!(result, "{}", style.paint(&word[..])).ok();
if extension.peek().is_some() { if extension.peek().is_some() {
result += comma_separator; result += comma_separator;
} }

View File

@ -3,7 +3,7 @@
use std::ffi::OsStr; use std::ffi::OsStr;
use std::process::Command; use std::process::Command;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Eq)]
pub enum LessVersion { pub enum LessVersion {
Less(usize), Less(usize),
BusyBox, BusyBox,

View File

@ -168,7 +168,7 @@ fn test_parse_minus_fail() {
assert!(range.is_err()); assert!(range.is_err());
} }
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum RangeCheckResult { pub enum RangeCheckResult {
// Within one of the given ranges // Within one of the given ranges
InRange, InRange,

View File

@ -1,4 +1,4 @@
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PagingMode { pub enum PagingMode {
Always, Always,
QuitIfOneScreen, QuitIfOneScreen,

View File

@ -1,3 +1,5 @@
use std::fmt::Write;
/// Expand tabs like an ANSI-enabled expand(1). /// Expand tabs like an ANSI-enabled expand(1).
pub fn expand_tabs(mut text: &str, width: usize, cursor: &mut usize) -> String { pub fn expand_tabs(mut text: &str, width: usize, cursor: &mut usize) -> String {
let mut buffer = String::with_capacity(text.len() * 2); let mut buffer = String::with_capacity(text.len() * 2);
@ -92,7 +94,7 @@ pub fn replace_nonprintable(input: &[u8], tab_width: usize) -> String {
c => output.push_str(&c.escape_unicode().collect::<String>()), c => output.push_str(&c.escape_unicode().collect::<String>()),
} }
} else { } else {
output.push_str(&format!("\\x{:02X}", input[idx])); write!(output, "\\x{:02X}", input[idx]).ok();
idx += 1; idx += 1;
} }
} }

View File

@ -7,7 +7,7 @@ use globset::{Candidate, GlobBuilder, GlobMatcher};
pub mod ignored_suffixes; pub mod ignored_suffixes;
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive] #[non_exhaustive]
pub enum MappingTarget<'a> { pub enum MappingTarget<'a> {
/// For mapping a path to a specific syntax. /// For mapping a path to a specific syntax.