Skip to content

Commit 383c639

Browse files
fix: Address clippy collapsible_if warnings with let-chains (#1211)
1 parent 2dfadae commit 383c639

13 files changed

Lines changed: 113 additions & 113 deletions

File tree

rust-code-analysis-cli/src/main.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ fn act_on_file(path: PathBuf, cfg: &Config) -> std::io::Result<()> {
139139
};
140140
action::<Count>(&language, source, &path, pr, cfg)
141141
} else if cfg.preproc_lock.is_some() {
142-
if let Some(language) = guess_language(&source, &path).0 {
143-
if language == LANG::Cpp {
144-
let mut results = cfg.preproc_lock.as_ref().unwrap().lock().unwrap();
145-
preprocess(
146-
&PreprocParser::new(source, &path, None),
147-
&path,
148-
&mut results,
149-
);
150-
}
142+
if let Some(language) = guess_language(&source, &path).0
143+
&& language == LANG::Cpp
144+
{
145+
let mut results = cfg.preproc_lock.as_ref().unwrap().lock().unwrap();
146+
preprocess(
147+
&PreprocParser::new(source, &path, None),
148+
&path,
149+
&mut results,
150+
);
151151
}
152152
Ok(())
153153
} else {

src/alterator.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ impl Alterator for CppCode {
7070
AstNode::new(node.kind(), text, span, Vec::new())
7171
}
7272
Cpp::PreprocDef | Cpp::PreprocFunctionDef | Cpp::PreprocCall => {
73-
if let Some(last) = children.last() {
74-
if last.r#type == "\n" {
75-
children.pop();
76-
}
73+
if let Some(last) = children.last()
74+
&& last.r#type == "\n"
75+
{
76+
children.pop();
7777
}
7878
Self::get_default(node, code, span, children)
7979
}

src/checker.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,11 @@ impl Checker for RustCode {
601601
}
602602

603603
fn is_useful_comment(node: &Node, code: &[u8]) -> bool {
604-
if let Some(parent) = node.parent() {
605-
if parent.kind_id() == Rust::TokenTree {
606-
// A comment could be a macro token
607-
return true;
608-
}
604+
if let Some(parent) = node.parent()
605+
&& parent.kind_id() == Rust::TokenTree
606+
{
607+
// A comment could be a macro token
608+
return true;
609609
}
610610
let code = &code[node.start_byte()..node.end_byte()];
611611
code.starts_with(b"/// cbindgen:")

src/find.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ impl Callback for Find {
6565
type Cfg = FindCfg;
6666

6767
fn call<T: ParserTrait>(cfg: Self::Cfg, parser: &T) -> Self::Res {
68-
if let Some(good) = find(parser, &cfg.filters) {
69-
if !good.is_empty() {
70-
println!("In file {}", cfg.path.to_str().unwrap());
71-
for node in good {
72-
dump_node(parser.get_code(), &node, 1, cfg.line_start, cfg.line_end)?;
73-
}
74-
println!();
68+
if let Some(good) = find(parser, &cfg.filters)
69+
&& !good.is_empty()
70+
{
71+
println!("In file {}", cfg.path.to_str().unwrap());
72+
for node in good {
73+
dump_node(parser.get_code(), &node, 1, cfg.line_start, cfg.line_end)?;
7574
}
75+
println!();
7676
}
7777
Ok(())
7878
}

src/getter.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ impl Getter for PythonCode {
7575
String => {
7676
let mut operator = HalsteadType::Unknown;
7777
// check if we've a documentation string or a multiline comment
78-
if let Some(parent) = node.parent() {
79-
if parent.kind_id() != ExpressionStatement || parent.child_count() != 1 {
80-
operator = HalsteadType::Operand;
81-
};
82-
}
78+
if let Some(parent) = node.parent()
79+
&& (parent.kind_id() != ExpressionStatement || parent.child_count() != 1)
80+
{
81+
operator = HalsteadType::Operand;
82+
};
8383
operator
8484
}
8585
_ => HalsteadType::Unknown,
@@ -444,25 +444,24 @@ impl Getter for CppCode {
444444
Cpp::FunctionDeclarator == id
445445
|| Cpp::FunctionDeclarator2 == id
446446
|| Cpp::FunctionDeclarator3 == id
447-
}) {
448-
if let Some(first) = fd.child(0) {
449-
match first.kind_id().into() {
450-
Cpp::TypeIdentifier
451-
| Cpp::Identifier
452-
| Cpp::FieldIdentifier
453-
| Cpp::DestructorName
454-
| Cpp::OperatorName
455-
| Cpp::QualifiedIdentifier
456-
| Cpp::QualifiedIdentifier2
457-
| Cpp::QualifiedIdentifier3
458-
| Cpp::QualifiedIdentifier4
459-
| Cpp::TemplateFunction
460-
| Cpp::TemplateMethod => {
461-
let code = &code[first.start_byte()..first.end_byte()];
462-
return std::str::from_utf8(code).ok();
463-
}
464-
_ => {}
447+
}) && let Some(first) = fd.child(0)
448+
{
449+
match first.kind_id().into() {
450+
Cpp::TypeIdentifier
451+
| Cpp::Identifier
452+
| Cpp::FieldIdentifier
453+
| Cpp::DestructorName
454+
| Cpp::OperatorName
455+
| Cpp::QualifiedIdentifier
456+
| Cpp::QualifiedIdentifier2
457+
| Cpp::QualifiedIdentifier3
458+
| Cpp::QualifiedIdentifier4
459+
| Cpp::TemplateFunction
460+
| Cpp::TemplateMethod => {
461+
let code = &code[first.start_byte()..first.end_byte()];
462+
return std::str::from_utf8(code).ok();
465463
}
464+
_ => {}
466465
}
467466
}
468467
}

src/metrics/abc.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,10 @@ impl Abc for JavaCode {
404404
}
405405
GT | LT => {
406406
// Excludes `<` and `>` used for generic types
407-
if let Some(parent) = node.parent() {
408-
if !matches!(parent.kind_id().into(), TypeArguments) {
409-
stats.conditions += 1.;
410-
}
407+
if let Some(parent) = node.parent()
408+
&& !matches!(parent.kind_id().into(), TypeArguments)
409+
{
410+
stats.conditions += 1.;
411411
}
412412
}
413413
// Counts unary conditions in elements separated by `&&` or `||` boolean operators
@@ -423,31 +423,31 @@ impl Abc for JavaCode {
423423
// Counts unary conditions inside assignments
424424
VariableDeclarator | AssignmentExpression => {
425425
// The child node of index 2 contains the right operand of an assignment operation
426-
if let Some(right_operand) = node.child(2) {
427-
if matches!(
426+
if let Some(right_operand) = node.child(2)
427+
&& matches!(
428428
right_operand.kind_id().into(),
429429
ParenthesizedExpression | UnaryExpression
430-
) {
431-
java_inspect_container(&right_operand, &mut stats.conditions);
432-
}
430+
)
431+
{
432+
java_inspect_container(&right_operand, &mut stats.conditions);
433433
}
434434
}
435435
// Counts unary conditions inside if and while statements
436436
IfStatement | WhileStatement => {
437437
// The child node of index 1 contains the condition
438-
if let Some(condition) = node.child(1) {
439-
if matches!(condition.kind_id().into(), ParenthesizedExpression) {
440-
java_inspect_container(&condition, &mut stats.conditions);
441-
}
438+
if let Some(condition) = node.child(1)
439+
&& matches!(condition.kind_id().into(), ParenthesizedExpression)
440+
{
441+
java_inspect_container(&condition, &mut stats.conditions);
442442
}
443443
}
444444
// Counts unary conditions do-while statements
445445
DoStatement => {
446446
// The child node of index 3 contains the condition
447-
if let Some(condition) = node.child(3) {
448-
if matches!(condition.kind_id().into(), ParenthesizedExpression) {
449-
java_inspect_container(&condition, &mut stats.conditions);
450-
}
447+
if let Some(condition) = node.child(3)
448+
&& matches!(condition.kind_id().into(), ParenthesizedExpression)
449+
{
450+
java_inspect_container(&condition, &mut stats.conditions);
451451
}
452452
}
453453
// Counts unary conditions inside for statements
@@ -487,25 +487,25 @@ impl Abc for JavaCode {
487487
// Counts unary conditions inside return statements
488488
ReturnStatement => {
489489
// The child node of index 1 contains the return value
490-
if let Some(value) = node.child(1) {
491-
if matches!(
490+
if let Some(value) = node.child(1)
491+
&& matches!(
492492
value.kind_id().into(),
493493
ParenthesizedExpression | UnaryExpression
494-
) {
495-
java_inspect_container(&value, &mut stats.conditions)
496-
}
494+
)
495+
{
496+
java_inspect_container(&value, &mut stats.conditions)
497497
}
498498
}
499499
// Counts unary conditions inside implicit return statements in lambda expressions
500500
LambdaExpression => {
501501
// The child node of index 2 contains the return value
502-
if let Some(value) = node.child(2) {
503-
if matches!(
502+
if let Some(value) = node.child(2)
503+
&& matches!(
504504
value.kind_id().into(),
505505
ParenthesizedExpression | UnaryExpression
506-
) {
507-
java_inspect_container(&value, &mut stats.conditions)
508-
}
506+
)
507+
{
508+
java_inspect_container(&value, &mut stats.conditions)
509509
}
510510
}
511511
// Counts unary conditions inside ternary expressions
@@ -523,22 +523,22 @@ impl Abc for JavaCode {
523523
}
524524
}
525525
// The child node of index 2 contains the first expression
526-
if let Some(expression) = node.child(2) {
527-
if matches!(
526+
if let Some(expression) = node.child(2)
527+
&& matches!(
528528
expression.kind_id().into(),
529529
ParenthesizedExpression | UnaryExpression
530-
) {
531-
java_inspect_container(&expression, &mut stats.conditions);
532-
}
530+
)
531+
{
532+
java_inspect_container(&expression, &mut stats.conditions);
533533
}
534534
// The child node of index 4 contains the second expression
535-
if let Some(expression) = node.child(4) {
536-
if matches!(
535+
if let Some(expression) = node.child(4)
536+
&& matches!(
537537
expression.kind_id().into(),
538538
ParenthesizedExpression | UnaryExpression
539-
) {
540-
java_inspect_container(&expression, &mut stats.conditions);
541-
}
539+
)
540+
{
541+
java_inspect_container(&expression, &mut stats.conditions);
542542
}
543543
}
544544
_ => {}

src/metrics/cognitive.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,10 @@ impl Cognitive for RustCode {
328328
increment_by_one(stats);
329329
}
330330
BreakExpression | ContinueExpression => {
331-
if let Some(label_child) = node.child(1) {
332-
if let Label = label_child.kind_id().into() {
333-
increment_by_one(stats);
334-
}
331+
if let Some(label_child) = node.child(1)
332+
&& let Label = label_child.kind_id().into()
333+
{
334+
increment_by_one(stats);
335335
}
336336
}
337337
UnaryExpression => {

src/metrics/loc.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,13 @@ fn add_cloc_lines(stats: &mut Stats, start: usize, end: usize) {
553553
// This difference is necessary in order to avoid having
554554
// a wrong count for the blank metric.
555555
fn check_comment_ends_on_code_line(stats: &mut Stats, start_code_line: usize) {
556-
if let Some(end) = stats.cloc.comment_line_end {
557-
if end == start_code_line && !stats.ploc.lines.contains(&start_code_line) {
558-
// Comment entirely *before* a code line
559-
stats.cloc.only_comment_lines -= 1;
560-
stats.cloc.code_comment_lines += 1;
561-
}
556+
if let Some(end) = stats.cloc.comment_line_end
557+
&& end == start_code_line
558+
&& !stats.ploc.lines.contains(&start_code_line)
559+
{
560+
// Comment entirely *before* a code line
561+
stats.cloc.only_comment_lines -= 1;
562+
stats.cloc.code_comment_lines += 1;
562563
}
563564
}
564565

src/metrics/nargs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ impl NArgs for CppCode {
220220
return;
221221
}
222222

223-
if Self::is_closure(node) {
224-
if let Some(declarator) = node.child_by_field_name("declarator") {
225-
let new_node = declarator;
226-
compute_args::<Self>(&new_node, &mut stats.closure_nargs);
227-
}
223+
if Self::is_closure(node)
224+
&& let Some(declarator) = node.child_by_field_name("declarator")
225+
{
226+
let new_node = declarator;
227+
compute_args::<Self>(&new_node, &mut stats.closure_nargs);
228228
}
229229
}
230230
}

src/node.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl Tree {
1818
Self(parser.parse(code, None).unwrap())
1919
}
2020

21-
pub(crate) fn get_root(&self) -> Node {
21+
pub(crate) fn get_root(&self) -> Node<'_> {
2222
Node(self.0.root_node())
2323
}
2424
}
@@ -108,7 +108,7 @@ impl<'a> Node<'a> {
108108
self.0.child_count()
109109
}
110110

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

@@ -168,15 +168,15 @@ impl<'a> Node<'a> {
168168
pub(crate) fn has_ancestors(&self, typ: fn(&Node) -> bool, typs: fn(&Node) -> bool) -> bool {
169169
let mut res = false;
170170
let mut node = *self;
171-
if let Some(parent) = node.parent() {
172-
if typ(&parent) {
173-
node = parent;
174-
}
171+
if let Some(parent) = node.parent()
172+
&& typ(&parent)
173+
{
174+
node = parent;
175175
}
176-
if let Some(parent) = node.parent() {
177-
if typs(&parent) {
178-
res = true;
179-
}
176+
if let Some(parent) = node.parent()
177+
&& typs(&parent)
178+
{
179+
res = true;
180180
}
181181
res
182182
}

0 commit comments

Comments
 (0)