diff --git a/rust-code-analysis-cli/src/main.rs b/rust-code-analysis-cli/src/main.rs index 4e9c0da43..728029297 100644 --- a/rust-code-analysis-cli/src/main.rs +++ b/rust-code-analysis-cli/src/main.rs @@ -139,15 +139,15 @@ fn act_on_file(path: PathBuf, cfg: &Config) -> std::io::Result<()> { }; action::(&language, source, &path, pr, cfg) } else if cfg.preproc_lock.is_some() { - if let Some(language) = guess_language(&source, &path).0 { - if language == LANG::Cpp { - let mut results = cfg.preproc_lock.as_ref().unwrap().lock().unwrap(); - preprocess( - &PreprocParser::new(source, &path, None), - &path, - &mut results, - ); - } + if let Some(language) = guess_language(&source, &path).0 + && language == LANG::Cpp + { + let mut results = cfg.preproc_lock.as_ref().unwrap().lock().unwrap(); + preprocess( + &PreprocParser::new(source, &path, None), + &path, + &mut results, + ); } Ok(()) } else { diff --git a/src/alterator.rs b/src/alterator.rs index 6e9f9734f..a79c4964a 100644 --- a/src/alterator.rs +++ b/src/alterator.rs @@ -70,10 +70,10 @@ impl Alterator for CppCode { AstNode::new(node.kind(), text, span, Vec::new()) } Cpp::PreprocDef | Cpp::PreprocFunctionDef | Cpp::PreprocCall => { - if let Some(last) = children.last() { - if last.r#type == "\n" { - children.pop(); - } + if let Some(last) = children.last() + && last.r#type == "\n" + { + children.pop(); } Self::get_default(node, code, span, children) } diff --git a/src/checker.rs b/src/checker.rs index 75c433b82..ec965b993 100644 --- a/src/checker.rs +++ b/src/checker.rs @@ -601,11 +601,11 @@ impl Checker for RustCode { } fn is_useful_comment(node: &Node, code: &[u8]) -> bool { - if let Some(parent) = node.parent() { - if parent.kind_id() == Rust::TokenTree { - // A comment could be a macro token - return true; - } + if let Some(parent) = node.parent() + && parent.kind_id() == Rust::TokenTree + { + // A comment could be a macro token + return true; } let code = &code[node.start_byte()..node.end_byte()]; code.starts_with(b"/// cbindgen:") diff --git a/src/find.rs b/src/find.rs index f441fe533..b6c37b91c 100644 --- a/src/find.rs +++ b/src/find.rs @@ -65,14 +65,14 @@ impl Callback for Find { type Cfg = FindCfg; fn call(cfg: Self::Cfg, parser: &T) -> Self::Res { - if let Some(good) = find(parser, &cfg.filters) { - if !good.is_empty() { - println!("In file {}", cfg.path.to_str().unwrap()); - for node in good { - dump_node(parser.get_code(), &node, 1, cfg.line_start, cfg.line_end)?; - } - println!(); + if let Some(good) = find(parser, &cfg.filters) + && !good.is_empty() + { + println!("In file {}", cfg.path.to_str().unwrap()); + for node in good { + dump_node(parser.get_code(), &node, 1, cfg.line_start, cfg.line_end)?; } + println!(); } Ok(()) } diff --git a/src/getter.rs b/src/getter.rs index 8a03adc50..c4cc4fc86 100644 --- a/src/getter.rs +++ b/src/getter.rs @@ -75,11 +75,11 @@ impl Getter for PythonCode { String => { let mut operator = HalsteadType::Unknown; // check if we've a documentation string or a multiline comment - if let Some(parent) = node.parent() { - if parent.kind_id() != ExpressionStatement || parent.child_count() != 1 { - operator = HalsteadType::Operand; - }; - } + if let Some(parent) = node.parent() + && (parent.kind_id() != ExpressionStatement || parent.child_count() != 1) + { + operator = HalsteadType::Operand; + }; operator } _ => HalsteadType::Unknown, @@ -444,25 +444,24 @@ impl Getter for CppCode { Cpp::FunctionDeclarator == id || Cpp::FunctionDeclarator2 == id || Cpp::FunctionDeclarator3 == id - }) { - if let Some(first) = fd.child(0) { - match first.kind_id().into() { - Cpp::TypeIdentifier - | Cpp::Identifier - | Cpp::FieldIdentifier - | Cpp::DestructorName - | Cpp::OperatorName - | Cpp::QualifiedIdentifier - | Cpp::QualifiedIdentifier2 - | Cpp::QualifiedIdentifier3 - | Cpp::QualifiedIdentifier4 - | Cpp::TemplateFunction - | Cpp::TemplateMethod => { - let code = &code[first.start_byte()..first.end_byte()]; - return std::str::from_utf8(code).ok(); - } - _ => {} + }) && let Some(first) = fd.child(0) + { + match first.kind_id().into() { + Cpp::TypeIdentifier + | Cpp::Identifier + | Cpp::FieldIdentifier + | Cpp::DestructorName + | Cpp::OperatorName + | Cpp::QualifiedIdentifier + | Cpp::QualifiedIdentifier2 + | Cpp::QualifiedIdentifier3 + | Cpp::QualifiedIdentifier4 + | Cpp::TemplateFunction + | Cpp::TemplateMethod => { + let code = &code[first.start_byte()..first.end_byte()]; + return std::str::from_utf8(code).ok(); } + _ => {} } } } diff --git a/src/metrics/abc.rs b/src/metrics/abc.rs index 2a1e62a89..a147a4792 100644 --- a/src/metrics/abc.rs +++ b/src/metrics/abc.rs @@ -404,10 +404,10 @@ impl Abc for JavaCode { } GT | LT => { // Excludes `<` and `>` used for generic types - if let Some(parent) = node.parent() { - if !matches!(parent.kind_id().into(), TypeArguments) { - stats.conditions += 1.; - } + if let Some(parent) = node.parent() + && !matches!(parent.kind_id().into(), TypeArguments) + { + stats.conditions += 1.; } } // Counts unary conditions in elements separated by `&&` or `||` boolean operators @@ -423,31 +423,31 @@ impl Abc for JavaCode { // Counts unary conditions inside assignments VariableDeclarator | AssignmentExpression => { // The child node of index 2 contains the right operand of an assignment operation - if let Some(right_operand) = node.child(2) { - if matches!( + if let Some(right_operand) = node.child(2) + && matches!( right_operand.kind_id().into(), ParenthesizedExpression | UnaryExpression - ) { - java_inspect_container(&right_operand, &mut stats.conditions); - } + ) + { + java_inspect_container(&right_operand, &mut stats.conditions); } } // Counts unary conditions inside if and while statements IfStatement | WhileStatement => { // The child node of index 1 contains the condition - if let Some(condition) = node.child(1) { - if matches!(condition.kind_id().into(), ParenthesizedExpression) { - java_inspect_container(&condition, &mut stats.conditions); - } + if let Some(condition) = node.child(1) + && matches!(condition.kind_id().into(), ParenthesizedExpression) + { + java_inspect_container(&condition, &mut stats.conditions); } } // Counts unary conditions do-while statements DoStatement => { // The child node of index 3 contains the condition - if let Some(condition) = node.child(3) { - if matches!(condition.kind_id().into(), ParenthesizedExpression) { - java_inspect_container(&condition, &mut stats.conditions); - } + if let Some(condition) = node.child(3) + && matches!(condition.kind_id().into(), ParenthesizedExpression) + { + java_inspect_container(&condition, &mut stats.conditions); } } // Counts unary conditions inside for statements @@ -487,25 +487,25 @@ impl Abc for JavaCode { // Counts unary conditions inside return statements ReturnStatement => { // The child node of index 1 contains the return value - if let Some(value) = node.child(1) { - if matches!( + if let Some(value) = node.child(1) + && matches!( value.kind_id().into(), ParenthesizedExpression | UnaryExpression - ) { - java_inspect_container(&value, &mut stats.conditions) - } + ) + { + java_inspect_container(&value, &mut stats.conditions) } } // Counts unary conditions inside implicit return statements in lambda expressions LambdaExpression => { // The child node of index 2 contains the return value - if let Some(value) = node.child(2) { - if matches!( + if let Some(value) = node.child(2) + && matches!( value.kind_id().into(), ParenthesizedExpression | UnaryExpression - ) { - java_inspect_container(&value, &mut stats.conditions) - } + ) + { + java_inspect_container(&value, &mut stats.conditions) } } // Counts unary conditions inside ternary expressions @@ -523,22 +523,22 @@ impl Abc for JavaCode { } } // The child node of index 2 contains the first expression - if let Some(expression) = node.child(2) { - if matches!( + if let Some(expression) = node.child(2) + && matches!( expression.kind_id().into(), ParenthesizedExpression | UnaryExpression - ) { - java_inspect_container(&expression, &mut stats.conditions); - } + ) + { + java_inspect_container(&expression, &mut stats.conditions); } // The child node of index 4 contains the second expression - if let Some(expression) = node.child(4) { - if matches!( + if let Some(expression) = node.child(4) + && matches!( expression.kind_id().into(), ParenthesizedExpression | UnaryExpression - ) { - java_inspect_container(&expression, &mut stats.conditions); - } + ) + { + java_inspect_container(&expression, &mut stats.conditions); } } _ => {} diff --git a/src/metrics/cognitive.rs b/src/metrics/cognitive.rs index a7b09fea3..4a619cade 100644 --- a/src/metrics/cognitive.rs +++ b/src/metrics/cognitive.rs @@ -328,10 +328,10 @@ impl Cognitive for RustCode { increment_by_one(stats); } BreakExpression | ContinueExpression => { - if let Some(label_child) = node.child(1) { - if let Label = label_child.kind_id().into() { - increment_by_one(stats); - } + if let Some(label_child) = node.child(1) + && let Label = label_child.kind_id().into() + { + increment_by_one(stats); } } UnaryExpression => { diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index d081ab489..5a422598d 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -553,12 +553,13 @@ fn add_cloc_lines(stats: &mut Stats, start: usize, end: usize) { // This difference is necessary in order to avoid having // a wrong count for the blank metric. fn check_comment_ends_on_code_line(stats: &mut Stats, start_code_line: usize) { - if let Some(end) = stats.cloc.comment_line_end { - if end == start_code_line && !stats.ploc.lines.contains(&start_code_line) { - // Comment entirely *before* a code line - stats.cloc.only_comment_lines -= 1; - stats.cloc.code_comment_lines += 1; - } + if let Some(end) = stats.cloc.comment_line_end + && end == start_code_line + && !stats.ploc.lines.contains(&start_code_line) + { + // Comment entirely *before* a code line + stats.cloc.only_comment_lines -= 1; + stats.cloc.code_comment_lines += 1; } } diff --git a/src/metrics/nargs.rs b/src/metrics/nargs.rs index 9df9a66af..ce531f6e6 100644 --- a/src/metrics/nargs.rs +++ b/src/metrics/nargs.rs @@ -220,11 +220,11 @@ impl NArgs for CppCode { return; } - if Self::is_closure(node) { - if let Some(declarator) = node.child_by_field_name("declarator") { - let new_node = declarator; - compute_args::(&new_node, &mut stats.closure_nargs); - } + if Self::is_closure(node) + && let Some(declarator) = node.child_by_field_name("declarator") + { + let new_node = declarator; + compute_args::(&new_node, &mut stats.closure_nargs); } } } diff --git a/src/node.rs b/src/node.rs index f6b98bb15..0cfc8171a 100644 --- a/src/node.rs +++ b/src/node.rs @@ -18,7 +18,7 @@ impl Tree { Self(parser.parse(code, None).unwrap()) } - pub(crate) fn get_root(&self) -> Node { + pub(crate) fn get_root(&self) -> Node<'_> { Node(self.0.root_node()) } } @@ -108,7 +108,7 @@ impl<'a> Node<'a> { self.0.child_count() } - pub(crate) fn child_by_field_name(&self, name: &str) -> Option { + pub(crate) fn child_by_field_name(&self, name: &str) -> Option> { self.0.child_by_field_name(name).map(Node) } @@ -168,15 +168,15 @@ impl<'a> Node<'a> { pub(crate) fn has_ancestors(&self, typ: fn(&Node) -> bool, typs: fn(&Node) -> bool) -> bool { let mut res = false; let mut node = *self; - if let Some(parent) = node.parent() { - if typ(&parent) { - node = parent; - } + if let Some(parent) = node.parent() + && typ(&parent) + { + node = parent; } - if let Some(parent) = node.parent() { - if typs(&parent) { - res = true; - } + if let Some(parent) = node.parent() + && typs(&parent) + { + res = true; } res } diff --git a/src/parser.rs b/src/parser.rs index 6973247e4..b13901fb8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -152,7 +152,7 @@ impl< } #[inline(always)] - fn get_root(&self) -> Node { + fn get_root(&self) -> Node<'_> { self.tree.get_root() } diff --git a/src/tools.rs b/src/tools.rs index 2dd1cf7d9..17d6f9869 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -250,7 +250,7 @@ pub(crate) fn remove_blank_lines(data: &mut Vec) { let count_trailing = data .iter() .rev() - .take_while(|&c| (*c == b'\n' || *c == b'\r')) + .take_while(|&c| *c == b'\n' || *c == b'\r') .count(); if count_trailing > 0 { data.truncate(data.len() - count_trailing); diff --git a/src/traits.rs b/src/traits.rs index 0edb032b8..16d4ed9cb 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -61,7 +61,7 @@ pub trait ParserTrait { fn new(code: Vec, path: &Path, pr: Option>) -> Self; fn get_language(&self) -> LANG; - fn get_root(&self) -> Node; + fn get_root(&self) -> Node<'_>; fn get_code(&self) -> &[u8]; fn get_filters(&self, filters: &[String]) -> Filter; }