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): call boolean_ident one time less

Co-authored-by: x10an14 <x10an14@users.noreply.github.com>

+21 -12
+21 -12
lib/src/lints/bool_comparison.rs
··· 46 46 return None; 47 47 }; 48 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 - }; 49 + let (bool_side, non_bool_side): (NixBoolean, &SyntaxNode) = 50 + match (boolean_ident(&lhs), boolean_ident(&rhs)) { 51 + (None, None) => return None, 52 + (None, Some(bool)) => (bool, &lhs), 53 + (Some(bool), _) => (bool, &rhs), 54 + }; 56 55 57 - let replacement = match (boolean_ident(&bool_side).unwrap(), op == BinOpKind::Equal) { 56 + let replacement = match (&bool_side, op == BinOpKind::Equal) { 58 57 (NixBoolean::True, true) | (NixBoolean::False, false) => { 59 58 // `a == true`, `a != false` replace with just `a` 60 59 non_bool_side.clone() ··· 64 63 match non_bool_side.kind() { 65 64 SyntaxKind::NODE_APPLY | SyntaxKind::NODE_PAREN | SyntaxKind::NODE_IDENT => { 66 65 // do not parenthsize the replacement 67 - make::unary_not(&non_bool_side).node().clone() 66 + make::unary_not(non_bool_side).node().clone() 68 67 } 69 68 SyntaxKind::NODE_BIN_OP => { 70 69 let inner = BinOp::cast(non_bool_side.clone()).unwrap(); 71 70 // `!a ? b`, no paren required 72 71 if inner.operator()? == BinOpKind::IsSet { 73 - make::unary_not(&non_bool_side).node().clone() 72 + make::unary_not(non_bool_side).node().clone() 74 73 } else { 75 - let parens = make::parenthesize(&non_bool_side); 74 + let parens = make::parenthesize(non_bool_side); 76 75 make::unary_not(parens.node()).node().clone() 77 76 } 78 77 } 79 78 _ => { 80 - let parens = make::parenthesize(&non_bool_side); 79 + let parens = make::parenthesize(non_bool_side); 81 80 make::unary_not(parens.node()).node().clone() 82 81 } 83 82 } ··· 95 94 enum NixBoolean { 96 95 True, 97 96 False, 97 + } 98 + 99 + impl std::fmt::Display for NixBoolean { 100 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 101 + let s = match self { 102 + Self::True => "true", 103 + Self::False => "false", 104 + }; 105 + write!(f, "{s}") 106 + } 98 107 } 99 108 100 109 // not entirely accurate, underhanded nix programmers might write `true = false`