diff options
author | Akshay <[email protected]> | 2022-02-20 04:07:32 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2022-02-20 04:07:32 +0000 |
commit | e8130a90dca048d195603281f72e1af0b4f7ccc6 (patch) | |
tree | 7b722cfc3dcf16417f73ba1577f824782903ad2e | |
parent | a80e252193096f22ae79fa03e66a0853ddae050e (diff) |
new lint: bool_simplification
TODO: add more patterns to this
-rw-r--r-- | bin/tests/data/bool_simplification.nix | 7 | ||||
-rw-r--r-- | bin/tests/main.rs | 3 | ||||
-rw-r--r-- | bin/tests/snapshots/main__bool_simplification.snap | 13 | ||||
-rw-r--r-- | lib/src/lints.rs | 1 | ||||
-rw-r--r-- | lib/src/lints/bool_simplification.rs | 61 |
5 files changed, 84 insertions, 1 deletions
diff --git a/bin/tests/data/bool_simplification.nix b/bin/tests/data/bool_simplification.nix new file mode 100644 index 0000000..64c2c7a --- /dev/null +++ b/bin/tests/data/bool_simplification.nix | |||
@@ -0,0 +1,7 @@ | |||
1 | let | ||
2 | _ = !(a == b); | ||
3 | # do not match here | ||
4 | _ = !(a != b); | ||
5 | _ = a != b; | ||
6 | in | ||
7 | null | ||
diff --git a/bin/tests/main.rs b/bin/tests/main.rs index bec64d2..3403f65 100644 --- a/bin/tests/main.rs +++ b/bin/tests/main.rs | |||
@@ -63,5 +63,6 @@ test_lint! { | |||
63 | empty_inherit, | 63 | empty_inherit, |
64 | faster_groupby => session_info!("2.5"), | 64 | faster_groupby => session_info!("2.5"), |
65 | faster_zipattrswith => session_info!("2.6"), | 65 | faster_zipattrswith => session_info!("2.6"), |
66 | deprecated_to_path => session_info!("2.4") | 66 | deprecated_to_path => session_info!("2.4"), |
67 | bool_simplification | ||
67 | } | 68 | } |
diff --git a/bin/tests/snapshots/main__bool_simplification.snap b/bin/tests/snapshots/main__bool_simplification.snap new file mode 100644 index 0000000..9e3c991 --- /dev/null +++ b/bin/tests/snapshots/main__bool_simplification.snap | |||
@@ -0,0 +1,13 @@ | |||
1 | --- | ||
2 | source: bin/tests/main.rs | ||
3 | expression: "&out" | ||
4 | |||
5 | --- | ||
6 | [W18] Warning: This boolean expression can be simplified | ||
7 | ╭─[data/bool_simplification.nix:2:7] | ||
8 | │ | ||
9 | 2 │ _ = !(a == b); | ||
10 | · ────┬──── | ||
11 | · ╰────── Try != instead of !(... == ...) | ||
12 | ───╯ | ||
13 | |||
diff --git a/lib/src/lints.rs b/lib/src/lints.rs index 439fd8f..582cabe 100644 --- a/lib/src/lints.rs +++ b/lib/src/lints.rs | |||
@@ -18,4 +18,5 @@ lints! { | |||
18 | faster_groupby, | 18 | faster_groupby, |
19 | faster_zipattrswith, | 19 | faster_zipattrswith, |
20 | deprecated_to_path, | 20 | deprecated_to_path, |
21 | bool_simplification, | ||
21 | } | 22 | } |
diff --git a/lib/src/lints/bool_simplification.rs b/lib/src/lints/bool_simplification.rs new file mode 100644 index 0000000..9ccb1f6 --- /dev/null +++ b/lib/src/lints/bool_simplification.rs | |||
@@ -0,0 +1,61 @@ | |||
1 | use crate::{make, session::SessionInfo, Metadata, Report, Rule, Suggestion}; | ||
2 | |||
3 | use if_chain::if_chain; | ||
4 | use macros::lint; | ||
5 | use rnix::{ | ||
6 | types::{BinOp, BinOpKind, Paren, TypedNode, UnaryOp, UnaryOpKind, Wrapper}, | ||
7 | NodeOrToken, SyntaxElement, SyntaxKind, | ||
8 | }; | ||
9 | |||
10 | /// ## What it does | ||
11 | /// Checks for boolean expressions that can be simplified. | ||
12 | /// | ||
13 | /// ## Why is this bad? | ||
14 | /// Complex booleans affect readibility. | ||
15 | /// | ||
16 | /// ## Example | ||
17 | /// ```nix | ||
18 | /// if !(x == y) then 0 else 1 | ||
19 | /// ``` | ||
20 | /// | ||
21 | /// Use `!=` instead: | ||
22 | /// | ||
23 | /// ```nix | ||
24 | /// if x != y then 0 else 1 | ||
25 | /// ``` | ||
26 | #[lint( | ||
27 | name = "bool_simplification", | ||
28 | note = "This boolean expression can be simplified", | ||
29 | code = 18, | ||
30 | match_with = SyntaxKind::NODE_UNARY_OP | ||
31 | )] | ||
32 | struct BoolSimplification; | ||
33 | |||
34 | impl Rule for BoolSimplification { | ||
35 | fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option<Report> { | ||
36 | if_chain! { | ||
37 | if let NodeOrToken::Node(node) = node; | ||
38 | if let Some(unary_expr) = UnaryOp::cast(node.clone()); | ||
39 | if unary_expr.operator() == UnaryOpKind::Invert; | ||
40 | if let Some(value_expr) = unary_expr.value(); | ||
41 | if let Some(paren_expr) = Paren::cast(value_expr.clone()); | ||
42 | if let Some(inner_expr) = paren_expr.inner(); | ||
43 | if let Some(bin_expr) = BinOp::cast(inner_expr); | ||
44 | if let Some(BinOpKind::Equal) = bin_expr.operator(); | ||
45 | then { | ||
46 | let at = node.text_range(); | ||
47 | let message = "Try `!=` instead of `!(... == ...)`"; | ||
48 | |||
49 | let lhs = bin_expr.lhs()?; | ||
50 | let rhs = bin_expr.rhs()?; | ||
51 | let replacement = make::binary(&lhs, "!=", &rhs).node().clone(); | ||
52 | Some( | ||
53 | self.report() | ||
54 | .suggest(at, message, Suggestion::new(at, replacement)), | ||
55 | ) | ||
56 | } else { | ||
57 | None | ||
58 | } | ||
59 | } | ||
60 | } | ||
61 | } | ||