Skip to content

Commit

Permalink
Apply clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
TimTheBig committed May 25, 2024
1 parent fa10bbb commit a630d34
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 36 deletions.
5 changes: 4 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(clippy::all, clippy::pedantic)]

use clap::ArgMatches;

use crate::error::{CliError, CliResult};
Expand All @@ -15,6 +17,7 @@ arg_enum! {
}

#[derive(Debug)]
#[allow(clippy::struct_excessive_bools)]
pub struct Config<'a> {
pub verbose: bool,
pub all: bool,
Expand All @@ -31,7 +34,7 @@ impl<'a> Config<'a> {
pub fn from_matches<'b: 'a>(m: &'b ArgMatches) -> CliResult<Self> {
if let Some(ext_vec) = m.values_of("language") {
for e in ext_vec {
if let None = Language::from_ext(e) {
if Language::from_ext(e).is_none() {
return Err(CliError::UnknownExt(format!(
"unsupported source code extension \
'{}'",
Expand Down
11 changes: 7 additions & 4 deletions src/count/counts.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(clippy::all, clippy::pedantic)]

use crate::comment::Comment;
use crate::config::{Config, Utf8Rule};
use crate::count::Count;
Expand Down Expand Up @@ -26,7 +28,7 @@ pub struct Counts<'c> {
}

impl<'c> Counts<'c> {
pub fn new(cfg: &'c Config) -> Self {
pub fn new(cfg: &'c Config<'_>) -> Self {
Counts {
cfg,
counts: vec![],
Expand Down Expand Up @@ -78,7 +80,7 @@ impl<'c> Counts<'c> {
debugln!("Extension is valid");
let mut found = false;
debugln!("Searching for previous entries of that type");
for l in self.counts.iter_mut() {
for l in &mut self.counts {
if l.lang == pos_lang {
debugln!("Found");
found = true;
Expand All @@ -100,6 +102,7 @@ impl<'c> Counts<'c> {
}

#[cfg_attr(feature = "lints", allow(cyclomatic_complexity, trivial_regex))]
#[allow(clippy::too_many_lines)]
pub fn count(&mut self) -> CliResult<()> {
for count in self.counts.iter_mut() {
debugln!("iter; count={:?};", count);
Expand Down Expand Up @@ -302,7 +305,7 @@ impl<'c> Counts<'c> {
count.comments(),
count.code(),
if (usafe_per - 00f64).abs() < f64::EPSILON {
"".to_owned()
String::new()
} else {
format!("{} ({:.2}%)", count.usafe(), usafe_per)
}
Expand Down Expand Up @@ -332,7 +335,7 @@ impl<'c> Counts<'c> {
(self.tot_usafe as f64 / self.tot_code as f64) * 100.00f64
)
} else {
"".to_owned()
String::new()
}
));

Expand Down
2 changes: 1 addition & 1 deletion src/count/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Deref for Count {
}

impl StdFmt::Display for Count {
fn fmt(&self, f: &mut StdFmt::Formatter) -> StdFmt::Result {
fn fmt(&self, f: &mut StdFmt::Formatter<'_>) -> StdFmt::Result {
write!(
f,
"{}\t{}\t{}\t{}\t{}\t{}",
Expand Down
9 changes: 6 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ impl CliError {
wlnerr!("{}", self);
::std::process::exit(1)
} else {
println!("{}", self);
println!("{self}");
::std::process::exit(0)
}
}
}

impl Display for CliError {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(f, "{} {}", Format::Error("error:"), self.to_string())
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
CliError::Generic(ref d) | CliError::UnknownExt(ref d) => write!(f, "{} {}", Format::Error("error:"), d),
CliError::Unknown => write!(f, "An unknown fatal error has occurred, please consider filing a bug-report!"),
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(clippy::all, clippy::pedantic)]

#[cfg(all(feature = "color", not(target_os = "windows")))]
use ansiterm::ANSIString;

Expand Down Expand Up @@ -43,7 +45,7 @@ impl<T: fmt::Display> Format<T> {

#[cfg(any(not(feature = "color"), target_os = "windows"))]
impl<T: fmt::Display> fmt::Display for Format<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", &self.format())
}
}
Expand Down
21 changes: 5 additions & 16 deletions src/fsutil.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![warn(clippy::all, clippy::pedantic)]
use gitignore::File;

use std::fs;
Expand All @@ -9,7 +10,7 @@ pub fn get_all_files(
path: &PathBuf,
exclude: &[PathBuf],
follow_links: bool,
gitignore: &Option<File>,
gitignore: &Option<File<'_>>,
) {
debugln!(
"executing; get_all_files; path={:?}; exclude={:?}; all={:?}",
Expand All @@ -36,13 +37,7 @@ pub fn get_all_files(
for entry in dir {
let entry = entry.unwrap();
let file_path = entry.path();
get_all_files(
v,
&file_path.to_path_buf(),
exclude,
follow_links,
gitignore,
);
get_all_files(v, &file_path.clone(), exclude, follow_links, gitignore);
}
} else {
debugln!("It's a file");
Expand All @@ -56,17 +51,11 @@ pub fn get_all_files(
if let Ok(result) = get_metadata(&file_path, follow_links) {
if result.is_dir() {
debugln!("It's a dir");
let dir = fs::read_dir(&path).unwrap();
let dir = fs::read_dir(path).unwrap();
for entry in dir {
let entry = entry.unwrap();
let file_path = entry.path();
get_all_files(
v,
&file_path.to_path_buf(),
exclude,
follow_links,
gitignore,
);
get_all_files(v, &file_path.clone(), exclude, follow_links, gitignore);
}
} else {
debugln!("It's a file");
Expand Down
4 changes: 3 additions & 1 deletion src/language.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(clippy::all, clippy::pedantic)]

use crate::comment::Comment;
use std::fmt as StdFmt;

Expand Down Expand Up @@ -102,7 +104,7 @@ impl Language {
}

impl StdFmt::Display for Language {
fn fmt(&self, f: &mut StdFmt::Formatter) -> StdFmt::Result {
fn fmt(&self, f: &mut StdFmt::Formatter<'_>) -> StdFmt::Result {
write!(f, "{}", self.name())
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(clippy::all, clippy::pedantic)]

macro_rules! cli_try {
($t:expr) => {{
match $t {
Expand Down
15 changes: 6 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,12 @@
unused_import_braces,
unused_qualifications
)]
#![warn(clippy::all, clippy::pedantic, rust_2018_idioms)]

#[macro_use]
extern crate clap;
#[cfg(feature = "color")]
extern crate ansiterm;
extern crate gitignore;
extern crate glob;
extern crate regex;
extern crate tabwriter;

#[cfg(feature = "debug")]
use std::env;
Expand Down Expand Up @@ -240,7 +237,7 @@ fn main() {
.validator(|s| single_char(s.to_string())))
.arg(Arg::from_usage("--utf8-rule [RULE] 'Sets the UTF-8 parsing rule'")
.default_value("strict")
.possible_values(&UTF8_RULES))
.possible_values(UTF8_RULES))
.after_help("\
When using '--exclude <PATH>' the path given can either be relative to the current directory, or \
absolute. When '--exclude <PATH>' is a file or path, it must be relative to the current directory \
Expand All @@ -254,13 +251,13 @@ the current directory you could do '--exclude */test.rs'."))
if let Some(m) = m.subcommand_matches("count") {
let cfg = Config::from_matches(m).unwrap_or_else(|e| e.exit());
println!("Gathering information...");
if let Err(e) = execute(cfg) {
if let Err(e) = execute(&cfg) {
e.exit();
}
}
}

fn execute(cfg: Config) -> CliResult<()> {
fn execute(cfg: &Config<'_>) -> CliResult<()> {
debugln!("executing; cmd=execute;");
verboseln!(cfg, "{}: {:?}", Format::Warning("Excluding"), cfg.exclude);
verbose!(
Expand All @@ -273,13 +270,13 @@ fn execute(cfg: Config) -> CliResult<()> {
cfg.exts.as_ref().unwrap().join(", ")
)
} else {
"".to_owned()
String::new()
}
);

debugln!("Checking for files or dirs to count from cli");

let mut counts = Counts::new(&cfg);
let mut counts = Counts::new(cfg);
counts.fill_from();
cli_try!(counts.count());
cli_try!(counts.write_results());
Expand Down

0 comments on commit a630d34

Please sign in to comment.