Skip to content
Merged
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
33 changes: 16 additions & 17 deletions crates/oxc_linter/src/rules/unicorn/no_useless_length_check.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fmt::Debug;

use itertools::concat;
use oxc_ast::{
ast::{Expression, LogicalExpression},
AstKind,
Expand Down Expand Up @@ -158,42 +157,42 @@ impl Rule for NoUselessLengthCheck {
if ![LogicalOperator::And, LogicalOperator::Or].contains(&log_expr.operator) {
return;
}
let flat_expr = flat_logical_expression(log_expr);
for i in 0..flat_expr.len() - 1 {
if let Some(diag) =
is_useless_check(flat_expr[i], flat_expr[i + 1], log_expr.operator)
{
let mut flat_exprs = Vec::new();
make_flat_logical_expression(log_expr, &mut flat_exprs);
for window in flat_exprs.windows(2) {
if let Some(diag) = is_useless_check(window[0], window[1], log_expr.operator) {
ctx.diagnostic(diag);
}
}
};
}
}

fn flat_logical_expression<'a>(node: &'a LogicalExpression<'a>) -> Vec<&'a Expression<'a>> {
let left = match &node.left.without_parentheses() {
fn make_flat_logical_expression<'a>(
node: &'a LogicalExpression<'a>,
result: &mut Vec<&'a Expression<'a>>,
) {
match &node.left.without_parentheses() {
Expression::LogicalExpression(le) => {
if le.operator == node.operator {
flat_logical_expression(le)
make_flat_logical_expression(le, result);
} else {
vec![&node.left]
result.push(&node.left);
}
}
_ => vec![&node.left],
_ => result.push(&node.left),
};

let right = match &node.right.without_parentheses() {
match &node.right.without_parentheses() {
Expression::LogicalExpression(le) => {
if le.operator == node.operator {
flat_logical_expression(le)
make_flat_logical_expression(le, result);
} else {
vec![&node.right]
result.push(&node.right);
}
}
_ => vec![&node.right],
_ => result.push(&node.right),
};

concat(vec![left, right])
}

#[test]
Expand Down