Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions rust-code-analysis-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ fn act_on_file(path: PathBuf, cfg: &Config) -> std::io::Result<()> {
};
action::<Count>(&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 {
Expand Down
8 changes: 4 additions & 4 deletions src/alterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:")
Expand Down
14 changes: 7 additions & 7 deletions src/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ impl Callback for Find {
type Cfg = FindCfg;

fn call<T: ParserTrait>(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(())
}
Expand Down
45 changes: 22 additions & 23 deletions src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}
_ => {}
}
}
}
Expand Down
74 changes: 37 additions & 37 deletions src/metrics/abc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
_ => {}
Expand Down
8 changes: 4 additions & 4 deletions src/metrics/cognitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
13 changes: 7 additions & 6 deletions src/metrics/loc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/metrics/nargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Self>(&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::<Self>(&new_node, &mut stats.closure_nargs);
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<'a> Node<'a> {
self.0.child_count()
}

pub(crate) fn child_by_field_name(&self, name: &str) -> Option<Node> {
pub(crate) fn child_by_field_name(&self, name: &str) -> Option<Node<'_>> {
self.0.child_by_field_name(name).map(Node)
}

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl<
}

#[inline(always)]
fn get_root(&self) -> Node {
fn get_root(&self) -> Node<'_> {
self.tree.get_root()
}

Expand Down
2 changes: 1 addition & 1 deletion src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ pub(crate) fn remove_blank_lines(data: &mut Vec<u8>) {
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);
Expand Down
2 changes: 1 addition & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub trait ParserTrait {

fn new(code: Vec<u8>, path: &Path, pr: Option<Arc<PreprocResults>>) -> 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;
}
Expand Down