Lints and suggestions for the Nix programming language
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(bool_comparison): undo some cyclomatic complexity

Co-authored-by: Shahar "Dawn" Or <mightyiampresence@gmail.com>

authored by

x10an14
Shahar "Dawn" Or
and committed by
Shahar "Dawn" Or
607b65f0 f6195e63

+51 -49
+51 -49
lib/src/lints/bool_comparison.rs
··· 35 35 36 36 impl Rule for BoolComparison { 37 37 fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option<Report> { 38 - if let NodeOrToken::Node(node) = node 39 - && let Some(bin_expr) = BinOp::cast(node.clone()) 40 - && let Some(lhs) = bin_expr.lhs() 41 - && let Some(rhs) = bin_expr.rhs() 42 - && let Some(op) = bin_expr.operator() 43 - && let BinOpKind::Equal | BinOpKind::NotEqual = op 44 - { 45 - let (non_bool_side, bool_side) = if boolean_ident(&lhs).is_some() { 46 - (rhs, lhs) 47 - } else if boolean_ident(&rhs).is_some() { 48 - (lhs, rhs) 49 - } else { 50 - return None; 51 - }; 52 - let at = node.text_range(); 53 - let replacement = { 54 - match (boolean_ident(&bool_side).unwrap(), op == BinOpKind::Equal) { 55 - (NixBoolean::True, true) | (NixBoolean::False, false) => { 56 - // `a == true`, `a != false` replace with just `a` 57 - non_bool_side.clone() 58 - } 59 - (NixBoolean::True, false) | (NixBoolean::False, true) => { 60 - // `a != true`, `a == false` replace with `!a` 61 - match non_bool_side.kind() { 62 - SyntaxKind::NODE_APPLY 63 - | SyntaxKind::NODE_PAREN 64 - | SyntaxKind::NODE_IDENT => { 65 - // do not parenthsize the replacement 38 + let NodeOrToken::Node(node) = node else { 39 + return None; 40 + }; 41 + let bin_expr = BinOp::cast(node.clone())?; 42 + let (lhs, rhs) = (bin_expr.lhs()?, bin_expr.rhs()?); 43 + let op = bin_expr.operator()?; 44 + 45 + let (BinOpKind::Equal | BinOpKind::NotEqual) = op else { 46 + return None; 47 + }; 48 + 49 + let (non_bool_side, bool_side) = if boolean_ident(&lhs).is_some() { 50 + (rhs, lhs) 51 + } else if boolean_ident(&rhs).is_some() { 52 + (lhs, rhs) 53 + } else { 54 + return None; 55 + }; 56 + 57 + let at = node.text_range(); 58 + let replacement = { 59 + match (boolean_ident(&bool_side).unwrap(), op == BinOpKind::Equal) { 60 + (NixBoolean::True, true) | (NixBoolean::False, false) => { 61 + // `a == true`, `a != false` replace with just `a` 62 + non_bool_side.clone() 63 + } 64 + (NixBoolean::True, false) | (NixBoolean::False, true) => { 65 + // `a != true`, `a == false` replace with `!a` 66 + match non_bool_side.kind() { 67 + SyntaxKind::NODE_APPLY 68 + | SyntaxKind::NODE_PAREN 69 + | SyntaxKind::NODE_IDENT => { 70 + // do not parenthsize the replacement 71 + make::unary_not(&non_bool_side).node().clone() 72 + } 73 + SyntaxKind::NODE_BIN_OP => { 74 + let inner = BinOp::cast(non_bool_side.clone()).unwrap(); 75 + // `!a ? b`, no paren required 76 + if inner.operator()? == BinOpKind::IsSet { 66 77 make::unary_not(&non_bool_side).node().clone() 67 - } 68 - SyntaxKind::NODE_BIN_OP => { 69 - let inner = BinOp::cast(non_bool_side.clone()).unwrap(); 70 - // `!a ? b`, no paren required 71 - if inner.operator()? == BinOpKind::IsSet { 72 - make::unary_not(&non_bool_side).node().clone() 73 - } else { 74 - let parens = make::parenthesize(&non_bool_side); 75 - make::unary_not(parens.node()).node().clone() 76 - } 77 - } 78 - _ => { 78 + } else { 79 79 let parens = make::parenthesize(&non_bool_side); 80 80 make::unary_not(parens.node()).node().clone() 81 81 } 82 82 } 83 + _ => { 84 + let parens = make::parenthesize(&non_bool_side); 85 + make::unary_not(parens.node()).node().clone() 86 + } 83 87 } 84 88 } 85 - }; 86 - let message = format!("Comparing `{non_bool_side}` with boolean literal `{bool_side}`"); 87 - Some( 88 - self.report() 89 - .suggest(at, message, Suggestion::new(at, replacement)), 90 - ) 91 - } else { 92 - None 93 - } 89 + } 90 + }; 91 + let message = format!("Comparing `{non_bool_side}` with boolean literal `{bool_side}`"); 92 + Some( 93 + self.report() 94 + .suggest(at, message, Suggestion::new(at, replacement)), 95 + ) 94 96 } 95 97 } 96 98