diff --git a/bin/tests/bool_comparison.rs b/bin/tests/bool_comparison.rs index a2c889fc..a516fd61 100644 --- a/bin/tests/bool_comparison.rs +++ b/bin/tests/bool_comparison.rs @@ -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", ], } diff --git a/bin/tests/snapshots/bool_comparison__fix_90387764e7c02df897cd8374ca670cd2faff59d2cebb3f416ba1e649f46efcac.snap b/bin/tests/snapshots/bool_comparison__fix_90387764e7c02df897cd8374ca670cd2faff59d2cebb3f416ba1e649f46efcac.snap new file mode 100644 index 00000000..b076e1ac --- /dev/null +++ b/bin/tests/snapshots/bool_comparison__fix_90387764e7c02df897cd8374ca670cd2faff59d2cebb3f416ba1e649f46efcac.snap @@ -0,0 +1,5 @@ +--- +source: bin/tests/bool_comparison.rs +expression: "\"let foo = {}; in (foo.bar or false) == true\"" +--- + diff --git a/bin/tests/snapshots/bool_comparison__lint_101b3f071d9ef6e1c6cddd4b3d4d2a4f7d4cf2764ff5811ee2d532b3ccb45690.snap b/bin/tests/snapshots/bool_comparison__lint_101b3f071d9ef6e1c6cddd4b3d4d2a4f7d4cf2764ff5811ee2d532b3ccb45690.snap index e3f5206f..70cfcaa5 100644 --- a/bin/tests/snapshots/bool_comparison__lint_101b3f071d9ef6e1c6cddd4b3d4d2a4f7d4cf2764ff5811ee2d532b3ccb45690.snap +++ b/bin/tests/snapshots/bool_comparison__lint_101b3f071d9ef6e1c6cddd4b3d4d2a4f7d4cf2764ff5811ee2d532b3ccb45690.snap @@ -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. ───╯ diff --git a/bin/tests/snapshots/bool_comparison__lint_43a857afe823ef70b47d4e0fbf00af8951e09a9a9891d29b3ba9e762a2978adb.snap b/bin/tests/snapshots/bool_comparison__lint_43a857afe823ef70b47d4e0fbf00af8951e09a9a9891d29b3ba9e762a2978adb.snap index f2923f30..8d3d5122 100644 --- a/bin/tests/snapshots/bool_comparison__lint_43a857afe823ef70b47d4e0fbf00af8951e09a9a9891d29b3ba9e762a2978adb.snap +++ b/bin/tests/snapshots/bool_comparison__lint_43a857afe823ef70b47d4e0fbf00af8951e09a9a9891d29b3ba9e762a2978adb.snap @@ -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. ───╯ diff --git a/bin/tests/snapshots/bool_comparison__lint_5d2c71eef69cc07e519dc3646f786ba7a32f5546c3a2194d2636a2684391e3f1.snap b/bin/tests/snapshots/bool_comparison__lint_5d2c71eef69cc07e519dc3646f786ba7a32f5546c3a2194d2636a2684391e3f1.snap index bd0f41a5..1ef7a311 100644 --- a/bin/tests/snapshots/bool_comparison__lint_5d2c71eef69cc07e519dc3646f786ba7a32f5546c3a2194d2636a2684391e3f1.snap +++ b/bin/tests/snapshots/bool_comparison__lint_5d2c71eef69cc07e519dc3646f786ba7a32f5546c3a2194d2636a2684391e3f1.snap @@ -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. ───╯ diff --git a/bin/tests/snapshots/bool_comparison__lint_630b66ca8810d52f4e68ed1c8d68db36bce21a1cb65b60ec15b901e87b62c13a.snap b/bin/tests/snapshots/bool_comparison__lint_630b66ca8810d52f4e68ed1c8d68db36bce21a1cb65b60ec15b901e87b62c13a.snap index 76e69512..53a7b3b5 100644 --- a/bin/tests/snapshots/bool_comparison__lint_630b66ca8810d52f4e68ed1c8d68db36bce21a1cb65b60ec15b901e87b62c13a.snap +++ b/bin/tests/snapshots/bool_comparison__lint_630b66ca8810d52f4e68ed1c8d68db36bce21a1cb65b60ec15b901e87b62c13a.snap @@ -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. ───╯ diff --git a/bin/tests/snapshots/bool_comparison__lint_82b623ef0ecd1e0e70c1dbe39f02832657a82dd5a58392f23345b22467a89b1a.snap b/bin/tests/snapshots/bool_comparison__lint_82b623ef0ecd1e0e70c1dbe39f02832657a82dd5a58392f23345b22467a89b1a.snap index cd7a4249..53122abd 100644 --- a/bin/tests/snapshots/bool_comparison__lint_82b623ef0ecd1e0e70c1dbe39f02832657a82dd5a58392f23345b22467a89b1a.snap +++ b/bin/tests/snapshots/bool_comparison__lint_82b623ef0ecd1e0e70c1dbe39f02832657a82dd5a58392f23345b22467a89b1a.snap @@ -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. ───╯ diff --git a/bin/tests/snapshots/bool_comparison__lint_90387764e7c02df897cd8374ca670cd2faff59d2cebb3f416ba1e649f46efcac.snap b/bin/tests/snapshots/bool_comparison__lint_90387764e7c02df897cd8374ca670cd2faff59d2cebb3f416ba1e649f46efcac.snap new file mode 100644 index 00000000..b076e1ac --- /dev/null +++ b/bin/tests/snapshots/bool_comparison__lint_90387764e7c02df897cd8374ca670cd2faff59d2cebb3f416ba1e649f46efcac.snap @@ -0,0 +1,5 @@ +--- +source: bin/tests/bool_comparison.rs +expression: "\"let foo = {}; in (foo.bar or false) == true\"" +--- + diff --git a/bin/tests/snapshots/bool_comparison__lint_9f48ebd94e1733d7ccda130d62830499b7344b794644163af528b0c834c5cca4.snap b/bin/tests/snapshots/bool_comparison__lint_9f48ebd94e1733d7ccda130d62830499b7344b794644163af528b0c834c5cca4.snap index cf663e7f..9a70d866 100644 --- a/bin/tests/snapshots/bool_comparison__lint_9f48ebd94e1733d7ccda130d62830499b7344b794644163af528b0c834c5cca4.snap +++ b/bin/tests/snapshots/bool_comparison__lint_9f48ebd94e1733d7ccda130d62830499b7344b794644163af528b0c834c5cca4.snap @@ -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. ───╯ diff --git a/bin/tests/snapshots/bool_comparison__lint_a09de9082d480f3e54de569de2789ebfee65a1ecbd31dcbdfc543b8b13e4bda4.snap b/bin/tests/snapshots/bool_comparison__lint_a09de9082d480f3e54de569de2789ebfee65a1ecbd31dcbdfc543b8b13e4bda4.snap index 9251f714..14f20992 100644 --- a/bin/tests/snapshots/bool_comparison__lint_a09de9082d480f3e54de569de2789ebfee65a1ecbd31dcbdfc543b8b13e4bda4.snap +++ b/bin/tests/snapshots/bool_comparison__lint_a09de9082d480f3e54de569de2789ebfee65a1ecbd31dcbdfc543b8b13e4bda4.snap @@ -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. ───╯ diff --git a/bin/tests/snapshots/bool_comparison__lint_a11a365119d2c64750d4253426c99db3e016d21e6183a706845dfed17d803393.snap b/bin/tests/snapshots/bool_comparison__lint_a11a365119d2c64750d4253426c99db3e016d21e6183a706845dfed17d803393.snap index 9ae1c7be..112ac09f 100644 --- a/bin/tests/snapshots/bool_comparison__lint_a11a365119d2c64750d4253426c99db3e016d21e6183a706845dfed17d803393.snap +++ b/bin/tests/snapshots/bool_comparison__lint_a11a365119d2c64750d4253426c99db3e016d21e6183a706845dfed17d803393.snap @@ -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. ───╯ diff --git a/bin/tests/snapshots/bool_comparison__lint_a14c6695accf2da707eaeb76ded232840736b9034ef1b55d1c8c92b9f5098a14.snap b/bin/tests/snapshots/bool_comparison__lint_a14c6695accf2da707eaeb76ded232840736b9034ef1b55d1c8c92b9f5098a14.snap index b3408305..479467c1 100644 --- a/bin/tests/snapshots/bool_comparison__lint_a14c6695accf2da707eaeb76ded232840736b9034ef1b55d1c8c92b9f5098a14.snap +++ b/bin/tests/snapshots/bool_comparison__lint_a14c6695accf2da707eaeb76ded232840736b9034ef1b55d1c8c92b9f5098a14.snap @@ -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. ───╯ diff --git a/bin/tests/snapshots/bool_comparison__lint_a30c834e61da781cbcbc0c95b79ab0a51a81f1fdc5a5d885d778f43d268ed2b3.snap b/bin/tests/snapshots/bool_comparison__lint_a30c834e61da781cbcbc0c95b79ab0a51a81f1fdc5a5d885d778f43d268ed2b3.snap index 8dba507a..bba99178 100644 --- a/bin/tests/snapshots/bool_comparison__lint_a30c834e61da781cbcbc0c95b79ab0a51a81f1fdc5a5d885d778f43d268ed2b3.snap +++ b/bin/tests/snapshots/bool_comparison__lint_a30c834e61da781cbcbc0c95b79ab0a51a81f1fdc5a5d885d778f43d268ed2b3.snap @@ -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. ───╯ diff --git a/bin/tests/snapshots/bool_comparison__lint_a83e7884e4c039d6517795734c6cab32d5e69473f7a6b6fe660eec020d70993d.snap b/bin/tests/snapshots/bool_comparison__lint_a83e7884e4c039d6517795734c6cab32d5e69473f7a6b6fe660eec020d70993d.snap index 1100232c..08457ad2 100644 --- a/bin/tests/snapshots/bool_comparison__lint_a83e7884e4c039d6517795734c6cab32d5e69473f7a6b6fe660eec020d70993d.snap +++ b/bin/tests/snapshots/bool_comparison__lint_a83e7884e4c039d6517795734c6cab32d5e69473f7a6b6fe660eec020d70993d.snap @@ -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. ───╯ diff --git a/bin/tests/snapshots/bool_comparison__lint_ba56854c55bd3954b56d4e0c3d32b65cd144d55b060637c5ff8085d81bef6c49.snap b/bin/tests/snapshots/bool_comparison__lint_ba56854c55bd3954b56d4e0c3d32b65cd144d55b060637c5ff8085d81bef6c49.snap index a75666af..af51892f 100644 --- a/bin/tests/snapshots/bool_comparison__lint_ba56854c55bd3954b56d4e0c3d32b65cd144d55b060637c5ff8085d81bef6c49.snap +++ b/bin/tests/snapshots/bool_comparison__lint_ba56854c55bd3954b56d4e0c3d32b65cd144d55b060637c5ff8085d81bef6c49.snap @@ -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. ───╯ diff --git a/bin/tests/snapshots/bool_comparison__lint_c031ce4a7d754ef42071e72da7d6bf4b645ebdaf948399be54f775686a58347b.snap b/bin/tests/snapshots/bool_comparison__lint_c031ce4a7d754ef42071e72da7d6bf4b645ebdaf948399be54f775686a58347b.snap index 223e6657..57efe61d 100644 --- a/bin/tests/snapshots/bool_comparison__lint_c031ce4a7d754ef42071e72da7d6bf4b645ebdaf948399be54f775686a58347b.snap +++ b/bin/tests/snapshots/bool_comparison__lint_c031ce4a7d754ef42071e72da7d6bf4b645ebdaf948399be54f775686a58347b.snap @@ -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. ───╯ diff --git a/bin/tests/snapshots/bool_comparison__lint_eb450df0fb6d76e9df94f175a6b87a7c1a667495e18432ed8f38540fd051cec1.snap b/bin/tests/snapshots/bool_comparison__lint_eb450df0fb6d76e9df94f175a6b87a7c1a667495e18432ed8f38540fd051cec1.snap index 9d3b3d90..61ae523b 100644 --- a/bin/tests/snapshots/bool_comparison__lint_eb450df0fb6d76e9df94f175a6b87a7c1a667495e18432ed8f38540fd051cec1.snap +++ b/bin/tests/snapshots/bool_comparison__lint_eb450df0fb6d76e9df94f175a6b87a7c1a667495e18432ed8f38540fd051cec1.snap @@ -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. ───╯ diff --git a/lib/src/lints/bool_comparison.rs b/lib/src/lints/bool_comparison.rs index ef95c60a..b7d572ab 100644 --- a/lib/src/lints/bool_comparison.rs +++ b/lib/src/lints/bool_comparison.rs @@ -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 _; @@ -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()?)?; @@ -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), )) }