From 6bf1b98c885ab4a4fbbef628f9edebe5e985cbf1 Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Wed, 8 Apr 2020 11:16:56 +0200 Subject: [PATCH] Fix clippy warnings --- src/exit.rs | 59 +++++++++++++---------------------------------- src/main.rs | 13 +++++------ src/metrics.rs | 4 ++-- src/preproc.rs | 2 +- src/tools.rs | 46 +++++++++++++++++++----------------- src/web/server.rs | 6 ++--- 6 files changed, 53 insertions(+), 77 deletions(-) diff --git a/src/exit.rs b/src/exit.rs index 639309b43..d6f4eeb45 100644 --- a/src/exit.rs +++ b/src/exit.rs @@ -51,85 +51,58 @@ where impl Exit for PythonCode { fn compute(node: &Node, stats: &mut Stats) { - match node.kind_id().into() { - Python::ReturnStatement => { - stats.exit += 1; - } - _ => {} + if let Python::ReturnStatement = node.kind_id().into() { + stats.exit += 1; } } } impl Exit for MozjsCode { fn compute(node: &Node, stats: &mut Stats) { - match node.kind_id().into() { - Mozjs::ReturnStatement => { - stats.exit += 1; - } - _ => {} + if let Mozjs::ReturnStatement = node.kind_id().into() { + stats.exit += 1; } } } impl Exit for JavascriptCode { fn compute(node: &Node, stats: &mut Stats) { - match node.kind_id().into() { - Javascript::ReturnStatement => { - stats.exit += 1; - } - _ => {} + if let Javascript::ReturnStatement = node.kind_id().into() { + stats.exit += 1; } } } impl Exit for TypescriptCode { fn compute(node: &Node, stats: &mut Stats) { - match node.kind_id().into() { - Typescript::ReturnStatement => { - stats.exit += 1; - } - _ => {} + if let Typescript::ReturnStatement = node.kind_id().into() { + stats.exit += 1; } } } impl Exit for TsxCode { fn compute(node: &Node, stats: &mut Stats) { - match node.kind_id().into() { - Tsx::ReturnStatement => { - stats.exit += 1; - } - _ => {} + if let Tsx::ReturnStatement = node.kind_id().into() { + stats.exit += 1; } } } impl Exit for RustCode { fn compute(node: &Node, stats: &mut Stats) { - use Rust::*; - - match node.kind_id().into() { - ReturnExpression => { - stats.exit += 1; - } - _ => { - if Self::is_func(node) { - if let Some(_) = node.child_by_field_name("return_type") { - stats.exit += 1; - } - } - } + if let Rust::ReturnExpression = node.kind_id().into() { + stats.exit += 1; + } else if Self::is_func(node) && node.child_by_field_name("return_type").is_some() { + stats.exit += 1; } } } impl Exit for CppCode { fn compute(node: &Node, stats: &mut Stats) { - match node.kind_id().into() { - Cpp::ReturnStatement => { - stats.exit += 1; - } - _ => {} + if let Cpp::ReturnStatement = node.kind_id().into() { + stats.exit += 1; } } } diff --git a/src/main.rs b/src/main.rs index 76e659c18..3efa94a8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -145,10 +145,10 @@ fn consumer(receiver: JobReceiver) { } } -fn send_file(path: PathBuf, cfg: &Arc, language: &Option, sender: &JobSender) { +fn send_file(path: PathBuf, cfg: &Arc, language: Option, sender: &JobSender) { sender .send(Some(JobItem { - language: language.clone(), + language, path, cfg: Arc::clone(cfg), })) @@ -203,14 +203,14 @@ fn explore( }; } - send_file(path, &cfg, &language, &sender); + send_file(path, &cfg, language, &sender); } } } else if (include.is_empty() || include.is_match(&path)) && (exclude.is_empty() || !exclude.is_match(&path)) && path.is_file() { - send_file(path, &cfg, &language, &sender); + send_file(path, &cfg, language, &sender); } } @@ -393,7 +393,7 @@ fn main() { } let paths: Vec<_> = matches.values_of("paths").unwrap().collect(); - let paths: Vec = paths.iter().map(|x| x.to_string()).collect(); + let paths: Vec = paths.iter().map(|x| (*x).to_string()).collect(); let dump = matches.is_present("dump"); let function = matches.is_present("function"); let in_place = matches.is_present("in_place"); @@ -488,7 +488,6 @@ fn main() { let producer = { let sender = sender.clone(); - let cfg = cfg.clone(); let include = mk_globset(matches.values_of("include").unwrap()); let exclude = mk_globset(matches.values_of("exclude").unwrap()); @@ -543,7 +542,7 @@ fn main() { println!("{}", data); } else { let output = PathBuf::from(output); - write_file(&output, data.to_string().as_bytes()).unwrap(); + write_file(&output, data.as_bytes()).unwrap(); } } } diff --git a/src/metrics.rs b/src/metrics.rs index bc8fa9ea9..40edadc9b 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -396,12 +396,12 @@ impl<'a> FuncSpace<'a> { file.push(".json"); let mut json_path = output_path.clone(); - json_path.push(file.clone()); + json_path.push(file); if json_path.as_path().exists() { let mut new_filename = path.to_str().unwrap().to_string(); let re = Regex::new(r"[\\:/]").unwrap(); - new_filename = re.replace_all(&mut new_filename, "_").to_string(); + new_filename = re.replace_all(&new_filename, "_").to_string(); new_filename.push_str(".json"); json_path.pop(); json_path.push(new_filename); diff --git a/src/preproc.rs b/src/preproc.rs index 23c8dc748..c9112b96b 100644 --- a/src/preproc.rs +++ b/src/preproc.rs @@ -28,7 +28,7 @@ impl PreprocFile { pub fn new_macros(macros: &[&str]) -> Self { let mut pf = Self::default(); for m in macros { - pf.macros.insert(m.to_string()); + pf.macros.insert((*m).to_string()); } pf } diff --git a/src/tools.rs b/src/tools.rs index 329ca4cd8..90e737b24 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -1,6 +1,7 @@ use crate::languages::fake; use crate::languages::*; use regex::bytes::Regex; +use std::cmp::Ordering; use std::collections::HashMap; use std::fs::File; use std::io::{Read, Write}; @@ -65,13 +66,10 @@ pub fn get_language_for_file(path: &PathBuf) -> Option { } fn mode_to_str(mode: &[u8]) -> Option { - std::str::from_utf8(mode) - .ok() - .map(|m| m.to_lowercase()) - .map(|m| m.to_string()) + std::str::from_utf8(mode).ok().map(|m| m.to_lowercase()) } -fn get_emacs_mode<'a>(buf: &'a [u8]) -> Option { +fn get_emacs_mode(buf: &[u8]) -> Option { // we just try to use the emacs info (if there) lazy_static! { // comment containing coding info are useful @@ -111,10 +109,11 @@ pub fn guess_language>(buf: &[u8], path: P) -> (Option, Str .extension() .map(|e| e.to_str().unwrap()) .map(|e| e.to_lowercase()) - .unwrap_or("".to_string()); + .unwrap_or_else(|| "".to_string()); let from_ext = get_from_ext(&ext); - let mode = get_emacs_mode(buf).unwrap_or("".to_string()); + let mode = get_emacs_mode(buf).unwrap_or_else(|| "".to_string()); + let from_mode = get_from_emacs_mode(&mode); if let Some(lang_ext) = from_ext { @@ -134,15 +133,16 @@ pub fn guess_language>(buf: &[u8], path: P) -> (Option, Str fake::get_true(&ext, &mode).unwrap_or_else(|| lang_ext.get_name().to_string()), ) } + } else if let Some(lang_mode) = from_mode { + ( + Some(lang_mode), + fake::get_true(&ext, &mode).unwrap_or_else(|| lang_mode.get_name().to_string()), + ) } else { - if let Some(lang_mode) = from_mode { - ( - Some(lang_mode), - fake::get_true(&ext, &mode).unwrap_or_else(|| lang_mode.get_name().to_string()), - ) - } else { - (None, fake::get_true(&ext, &mode).unwrap_or("".to_string())) - } + ( + None, + fake::get_true(&ext, &mode).unwrap_or_else(|| "".to_string()), + ) } } @@ -236,12 +236,16 @@ pub fn guess_file( continue; } if let Some(dist) = get_paths_dist(current_path, &p) { - if dist < dist_min { - dist_min = dist; - path_min.clear(); - path_min.push(p); - } else if dist == dist_min { - path_min.push(p) + match dist.cmp(&dist_min) { + Ordering::Less => { + dist_min = dist; + path_min.clear(); + path_min.push(p); + } + Ordering::Equal => { + path_min.push(p); + } + Ordering::Greater => {} } } } diff --git a/src/web/server.rs b/src/web/server.rs index 09ea6845b..e4632a1bf 100644 --- a/src/web/server.rs +++ b/src/web/server.rs @@ -587,9 +587,9 @@ mod tests { // Inspired from https://hg.mozilla.org/mozilla-central/file/9b2a99adc05e53cd4010de512f50118594756650/extensions/java/xpcom/tests/testparams/TestParams.java#l64. #[actix_rt::test] async fn test_web_comment_plain_bad_chars() { - let bad_bytes = &[142, 137, 138, 136, 140, 141, 10]; - let input_vec = ["/*char*/s: ".as_bytes(), bad_bytes].concat(); - let output_vec = ["s: ".as_bytes(), bad_bytes].concat(); + let bad_bytes: &[u8] = &[142, 137, 138, 136, 140, 141, 10]; + let input_vec = [b"/*char*/s: ", bad_bytes].concat(); + let output_vec = [b"s: ", bad_bytes].concat(); let mut app = test::init_service( App::new()