Skip to content
Open
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
2 changes: 2 additions & 0 deletions bin/tests/bool_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ generate_tests! {
// has attr
"false == m ? n",
"true == o ? p",
// don't lint if we're not 100% sure it's legit
"let foo = {}; in (foo.bar or false) == true",
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: bin/tests/bool_comparison.rs
expression: "\"let foo = {}; in (foo.bar or false) == true\""
---

Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ expression: "\"f != false\""
1 │ f != false
· ─────┬────
· ╰────── Comparing f with boolean literal false
· ╰────── Comparing 'f' with boolean literal 'false'. We can't check the type of 'f'. Please verify that holds a boolean expression.
───╯
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ expression: "\"true == d\""
1 │ true == d
· ────┬────
· ╰────── Comparing d with boolean literal true
· ╰────── Comparing 'd' with boolean literal 'true'. We can't check the type of 'd'. Please verify that holds a boolean expression.
───╯
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ expression: "\"false == false\""
1 │ false == false
· ───────┬──────
· ╰──────── Comparing false with boolean literal false
· ╰──────── Comparing 'false' with boolean literal 'false'. We can't check the type of 'false'. Please verify that holds a boolean expression.
───╯
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ expression: "\"false != h\""
1 │ false != h
· ─────┬────
· ╰────── Comparing h with boolean literal false
· ╰────── Comparing 'h' with boolean literal 'false'. We can't check the type of 'h'. Please verify that holds a boolean expression.
───╯
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ expression: "\"true == true\""
1 │ true == true
· ──────┬─────
· ╰─────── Comparing true with boolean literal true
· ╰─────── Comparing 'true' with boolean literal 'true'. We can't check the type of 'true'. Please verify that holds a boolean expression.
───╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: bin/tests/bool_comparison.rs
expression: "\"let foo = {}; in (foo.bar or false) == true\""
---

Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ expression: "\"true == c\""
1 │ true == c
· ────┬────
· ╰────── Comparing c with boolean literal true
· ╰────── Comparing 'c' with boolean literal 'true'. We can't check the type of 'c'. Please verify that holds a boolean expression.
───╯
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ expression: "\"true != g\""
1 │ true != g
· ────┬────
· ╰────── Comparing g with boolean literal true
· ╰────── Comparing 'g' with boolean literal 'true'. We can't check the type of 'g'. Please verify that holds a boolean expression.
───╯
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ expression: "\"b == true\""
1 │ b == true
· ────┬────
· ╰────── Comparing b with boolean literal true
· ╰────── Comparing 'b' with boolean literal 'true'. We can't check the type of 'b'. Please verify that holds a boolean expression.
───╯
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ expression: "\"false == m ? n\""
1 │ false == m ? n
· ───────┬──────
· ╰──────── Comparing m ? n with boolean literal false
· ╰──────── Comparing 'm ? n' with boolean literal 'false'. We can't check the type of 'm ? n'. Please verify that holds a boolean expression.
───╯
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ expression: "\"a == true\""
1 │ a == true
· ────┬────
· ╰────── Comparing a with boolean literal true
· ╰────── Comparing 'a' with boolean literal 'true'. We can't check the type of 'a'. Please verify that holds a boolean expression.
───╯
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ expression: "\"true == o ? p\""
1 │ true == o ? p
· ──────┬──────
· ╰──────── Comparing o ? p with boolean literal true
· ╰──────── Comparing 'o ? p' with boolean literal 'true'. We can't check the type of 'o ? p'. Please verify that holds a boolean expression.
───╯
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ expression: "\"false == true\""
1 │ false == true
· ──────┬──────
· ╰──────── Comparing true with boolean literal false
· ╰──────── Comparing 'true' with boolean literal 'false'. We can't check the type of 'true'. Please verify that holds a boolean expression.
───╯
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ expression: "\"e != true\""
1 │ e != true
· ────┬────
· ╰────── Comparing e with boolean literal true
· ╰────── Comparing 'e' with boolean literal 'true'. We can't check the type of 'e'. Please verify that holds a boolean expression.
───╯
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ expression: "\"true == false\""
1 │ true == false
· ──────┬──────
· ╰──────── Comparing false with boolean literal true
· ╰──────── Comparing 'false' with boolean literal 'true'. We can't check the type of 'false'. Please verify that holds a boolean expression.
───╯
10 changes: 8 additions & 2 deletions lib/src/lints/bool_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{Metadata, Report, Rule, Suggestion, make};
use macros::lint;
use rnix::{
NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode,
ast::{BinOp, BinOpKind, Ident},
ast::{BinOp, BinOpKind, Expr, Ident},
};
use rowan::ast::AstNode as _;

Expand Down Expand Up @@ -41,6 +41,12 @@ impl Rule for BoolComparison {
};
let bin_expr = BinOp::cast(node.clone())?;
let (lhs, rhs) = (bin_expr.lhs()?, bin_expr.rhs()?);
if [&lhs, &rhs]
.iter()
.any(|expr| !matches!(expr, Expr::Literal(_) | Expr::Ident(_) | Expr::HasAttr(_)))
{
return None;
}
let (lhs, rhs) = (lhs.syntax(), rhs.syntax());
let op = EqualityBinOpKind::try_from(bin_expr.operator()?)?;

Expand Down Expand Up @@ -78,7 +84,7 @@ impl Rule for BoolComparison {
let at = node.text_range();
Some(self.report().suggest(
at,
format!("Comparing `{non_bool_side}` with boolean literal `{bool_side}`"),
format!("Comparing '{non_bool_side}' with boolean literal '{bool_side}'. We can't check the type of '{non_bool_side}'. Please verify that holds a boolean expression."),
Suggestion::with_replacement(at, replacement),
))
}
Expand Down