Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Luni-4 authored and marco-c committed Nov 11, 2020
1 parent 46a2f49 commit 7d3830c
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 36 deletions.
10 changes: 6 additions & 4 deletions rust-code-analysis-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,19 @@ fn act_on_file(language: Option<LANG>, path: PathBuf, cfg: &Config) -> std::io::
}
} else {
let cfg = MetricsCfg { path };
action::<Metrics>(&language, source, &cfg.path.clone(), pr, cfg)
let path = cfg.path.clone();
action::<Metrics>(&language, source, &path, pr, cfg)
}
} else if cfg.comments {
let cfg = CommentRmCfg {
in_place: cfg.in_place,
path,
};
let path = cfg.path.clone();
if language == LANG::Cpp {
action::<CommentRm>(&LANG::Ccomment, source, &cfg.path.clone(), pr, cfg)
action::<CommentRm>(&LANG::Ccomment, source, &path, pr, cfg)
} else {
action::<CommentRm>(&language, source, &cfg.path.clone(), pr, cfg)
action::<CommentRm>(&language, source, &path, pr, cfg)
}
} else if cfg.function {
let cfg = FunctionCfg { path: path.clone() };
Expand Down Expand Up @@ -477,7 +479,7 @@ fn main() {
.value_of("output_format")
.map(parse_or_exit::<Format>);
let pretty = matches.is_present("pretty");
let output = matches.value_of("output").map(|s| PathBuf::from(s));
let output = matches.value_of("output").map(PathBuf::from);
let output_is_dir = output.as_ref().map(|p| p.is_dir()).unwrap_or(false);
if metrics && output.is_some() && !output_is_dir {
eprintln!("Error: The output parameter must be a directory");
Expand Down
2 changes: 1 addition & 1 deletion src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl Getter for RustCode {
if let Some(name) = node
.object()
.child_by_field_name("name")
.or(node.object().child_by_field_name("type"))
.or_else(|| node.object().child_by_field_name("type"))
{
let code = &code[name.start_byte()..name.end_byte()];
std::str::from_utf8(code).ok()
Expand Down
10 changes: 0 additions & 10 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ macro_rules! mk_impl_lang {
/// ```
/// use rust_code_analysis::LANG;
///
/// # fn main() {
/// println!("{}", LANG::Rust.get_name());
/// # }
/// ```
pub fn get_name(&self) -> &'static str {
match self {
Expand Down Expand Up @@ -113,7 +111,6 @@ macro_rules! mk_action {
///
/// use rust_code_analysis::{action, Callback, LANG, Metrics, MetricsCfg};
///
/// # fn main() {
/// let source_code = "int a = 42;";
/// let language = LANG::Cpp;
///
Expand All @@ -127,7 +124,6 @@ macro_rules! mk_action {
/// };
///
/// action::<Metrics>(&language, source_as_vec, &cfg.path.clone(), None, cfg);
/// # }
/// ```
///
/// [`Callback`]: trait.Callback.html
Expand All @@ -152,7 +148,6 @@ macro_rules! mk_action {
///
/// use rust_code_analysis::{get_function_spaces, LANG};
///
/// # fn main() {
/// let source_code = "int a = 42;";
/// let language = LANG::Cpp;
///
Expand All @@ -161,7 +156,6 @@ macro_rules! mk_action {
/// let source_as_vec = source_code.as_bytes().to_vec();
///
/// get_function_spaces(&language, source_as_vec, &path, None).unwrap();
/// # }
/// ```
#[inline(always)]
pub fn get_function_spaces(lang: &LANG, source: Vec<u8>, path: &PathBuf, pr: Option<Arc<PreprocResults>>) -> Option<FuncSpace> {
Expand All @@ -188,11 +182,9 @@ macro_rules! mk_extensions {
/// ```
/// use rust_code_analysis::get_from_ext;
///
/// # fn main() {
/// let ext = "rs";
///
/// get_from_ext(ext).unwrap();
/// # }
/// ```
pub fn get_from_ext(ext: &str) -> Option<LANG>{
match ext {
Expand Down Expand Up @@ -221,11 +213,9 @@ macro_rules! mk_emacs_mode {
/// ```
/// use rust_code_analysis::get_from_emacs_mode;
///
/// # fn main() {
/// let emacs_mode = "rust";
///
/// get_from_emacs_mode(emacs_mode).unwrap();
/// # }
/// ```
pub fn get_from_emacs_mode(mode: &str) -> Option<LANG>{
match mode {
Expand Down
5 changes: 2 additions & 3 deletions src/metrics/cognitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl BoolSequence {
}

fn eval_based_on_prev(&mut self, bool_id: u16, structural: usize) -> usize {
let new_structural = if let Some(prev) = self.boolean_op {
if let Some(prev) = self.boolean_op {
if prev != bool_id {
// The boolean operator is different from the previous one, so
// the counter is incremented.
Expand All @@ -156,8 +156,7 @@ impl BoolSequence {
// logical operators and increment the counter.
self.boolean_op = Some(bool_id);
structural + 1
};
new_structural
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/metrics/mi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub trait Mi
where
Self: Checker,
{
fn compute<'a>(
fn compute(
loc: &loc::Stats,
cyclomatic: &cyclomatic::Stats,
halstead: &halstead::Stats,
Expand Down
2 changes: 0 additions & 2 deletions src/output/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::traits::*;
///
/// use rust_code_analysis::{dump_node, CppParser, ParserTrait};
///
/// # fn main() {
/// let source_code = "int a = 42;";
///
/// // The path to a dummy file used to contain the source code
Expand All @@ -31,7 +30,6 @@ use crate::traits::*;
///
/// // Dump the AST from the first line of code in a file to the last one
/// dump_node(&source_as_vec, &root, -1, None, None).unwrap();
/// # }
/// ```
///
/// [`Result`]: #variant.Result
Expand Down
2 changes: 0 additions & 2 deletions src/output/dump_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use crate::spaces::{CodeMetrics, FuncSpace};
///
/// use rust_code_analysis::{dump_root, metrics, CppParser, ParserTrait};
///
/// # fn main() {
/// let source_code = "int a = 42;";
///
/// // The path to a dummy file used to contain the source code
Expand All @@ -38,7 +37,6 @@ use crate::spaces::{CodeMetrics, FuncSpace};
///
/// // Dump all metrics
/// dump_root(&space).unwrap();
/// # }
/// ```
///
/// [`Result`]: #variant.Result
Expand Down
2 changes: 0 additions & 2 deletions src/spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ struct State<'a> {
///
/// use rust_code_analysis::{CppParser, metrics, ParserTrait};
///
/// # fn main() {
/// let source_code = "int a = 42;";
///
/// // The path to a dummy file used to contain the source code
Expand All @@ -231,7 +230,6 @@ struct State<'a> {
///
/// // Gets all function spaces data of the code contained in foo.c
/// metrics(&parser, &path).unwrap();
/// # }
/// ```
pub fn metrics<'a, T: ParserTrait>(parser: &'a T, path: &'a PathBuf) -> Option<FuncSpace> {
let code = parser.get_code();
Expand Down
12 changes: 1 addition & 11 deletions src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ use std::path::{Component, Path, PathBuf};
///
/// use rust_code_analysis::read_file;
///
/// # fn main() {
/// let path = PathBuf::from("Cargo.toml");
/// read_file(&path).unwrap();
/// # }
/// ```
pub fn read_file(path: &PathBuf) -> std::io::Result<Vec<u8>> {
let mut file = File::open(path)?;
Expand All @@ -40,10 +38,8 @@ pub fn read_file(path: &PathBuf) -> std::io::Result<Vec<u8>> {
///
/// use rust_code_analysis::read_file_with_eol;
///
/// # fn main() {
/// let path = PathBuf::from("Cargo.toml");
/// read_file_with_eol(&path).unwrap();
/// # }
/// ```
pub fn read_file_with_eol(path: &PathBuf) -> std::io::Result<Option<Vec<u8>>> {
let file_size = fs::metadata(&path).map_or(1024 * 1024, |m| m.len() as usize);
Expand All @@ -55,7 +51,7 @@ pub fn read_file_with_eol(path: &PathBuf) -> std::io::Result<Option<Vec<u8>>> {
let mut file = File::open(path)?;

let mut start = vec![0; 64.min(file_size)];
let start = if let Ok(_) = file.read_exact(&mut start) {
let start = if file.read_exact(&mut start).is_ok() {
// Skip the bom if one
if start[..2] == [b'\xFE', b'\xFF'] || start[..2] == [b'\xFF', b'\xFE'] {
&start[2..]
Expand Down Expand Up @@ -96,11 +92,9 @@ pub fn read_file_with_eol(path: &PathBuf) -> std::io::Result<Option<Vec<u8>>> {
///
/// use rust_code_analysis::write_file;
///
/// # fn main() {
/// let path = PathBuf::from("foo.txt");
/// let data: [u8; 4] = [0; 4];
/// write_file(&path, &data).unwrap();
/// # }
/// ```
pub fn write_file(path: &PathBuf, data: &[u8]) -> std::io::Result<()> {
let mut file = File::create(path)?;
Expand All @@ -119,10 +113,8 @@ pub fn write_file(path: &PathBuf, data: &[u8]) -> std::io::Result<()> {
///
/// use rust_code_analysis::get_language_for_file;
///
/// # fn main() {
/// let path = PathBuf::from("build.rs");
/// get_language_for_file(&path).unwrap();
/// # }
/// ```
pub fn get_language_for_file(path: &PathBuf) -> Option<LANG> {
if let Some(ext) = path.extension() {
Expand Down Expand Up @@ -183,7 +175,6 @@ fn get_emacs_mode(buf: &[u8]) -> Option<String> {
///
/// use rust_code_analysis::guess_language;
///
/// # fn main() {
/// let source_code = "int a = 42;";
///
/// // The path to a dummy file used to contain the source code
Expand All @@ -192,7 +183,6 @@ fn get_emacs_mode(buf: &[u8]) -> Option<String> {
///
/// // Guess the language of a code
/// guess_language(&source_slice, &path);
/// # }
/// ```
///
/// [`LANG`]: enum.LANG.html
Expand Down

0 comments on commit 7d3830c

Please sign in to comment.