diff options
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | bin/Cargo.toml | 2 | ||||
-rw-r--r-- | flake.nix | 2 | ||||
-rw-r--r-- | lib/src/lints.rs | 3 | ||||
-rw-r--r-- | lib/src/lints/empty_pattern.rs | 37 | ||||
-rw-r--r-- | lib/src/lints/redundant_pattern_bind.rs | 41 | ||||
-rw-r--r-- | lib/src/lints/unquoted_splice.rs | 33 | ||||
-rw-r--r-- | lib/src/make.rs | 4 | ||||
-rw-r--r-- | notes.txt | 7 |
9 files changed, 128 insertions, 3 deletions
@@ -388,7 +388,7 @@ checksum = "b203e79e90905594272c1c97c7af701533d42adaab0beb3859018e477d54a3b0" | |||
388 | 388 | ||
389 | [[package]] | 389 | [[package]] |
390 | name = "statix" | 390 | name = "statix" |
391 | version = "0.2.0" | 391 | version = "0.2.3" |
392 | dependencies = [ | 392 | dependencies = [ |
393 | "ariadne", | 393 | "ariadne", |
394 | "clap", | 394 | "clap", |
diff --git a/bin/Cargo.toml b/bin/Cargo.toml index 971fb47..78f4b9c 100644 --- a/bin/Cargo.toml +++ b/bin/Cargo.toml | |||
@@ -1,6 +1,6 @@ | |||
1 | [package] | 1 | [package] |
2 | name = "statix" | 2 | name = "statix" |
3 | version = "0.2.2" | 3 | version = "0.2.3" |
4 | edition = "2018" | 4 | edition = "2018" |
5 | license = "MIT" | 5 | license = "MIT" |
6 | 6 | ||
@@ -49,7 +49,7 @@ | |||
49 | 49 | ||
50 | statix = with final; pkgs.stdenv.mkDerivation { | 50 | statix = with final; pkgs.stdenv.mkDerivation { |
51 | pname = "statix"; | 51 | pname = "statix"; |
52 | version = "v0.2.2"; | 52 | version = "v0.2.3"; |
53 | src = builtins.path { | 53 | src = builtins.path { |
54 | path = ./.; | 54 | path = ./.; |
55 | name = "statix"; | 55 | name = "statix"; |
diff --git a/lib/src/lints.rs b/lib/src/lints.rs index f7e98ac..09cd77c 100644 --- a/lib/src/lints.rs +++ b/lib/src/lints.rs | |||
@@ -9,4 +9,7 @@ lint_map! { | |||
9 | collapsible_let_in, | 9 | collapsible_let_in, |
10 | eta_reduction, | 10 | eta_reduction, |
11 | useless_parens, | 11 | useless_parens, |
12 | unquoted_splice, | ||
13 | empty_pattern, | ||
14 | redundant_pattern_bind, | ||
12 | } | 15 | } |
diff --git a/lib/src/lints/empty_pattern.rs b/lib/src/lints/empty_pattern.rs new file mode 100644 index 0000000..6fb7558 --- /dev/null +++ b/lib/src/lints/empty_pattern.rs | |||
@@ -0,0 +1,37 @@ | |||
1 | use crate::{make, Lint, Metadata, Report, Rule, Suggestion}; | ||
2 | |||
3 | use if_chain::if_chain; | ||
4 | use macros::lint; | ||
5 | use rnix::{ | ||
6 | types::{Pattern, TypedNode}, | ||
7 | NodeOrToken, SyntaxElement, SyntaxKind, | ||
8 | }; | ||
9 | |||
10 | #[lint( | ||
11 | name = "empty pattern", | ||
12 | note = "Found empty pattern in function argument", | ||
13 | code = 10, | ||
14 | match_with = SyntaxKind::NODE_PATTERN | ||
15 | )] | ||
16 | struct EmptyPattern; | ||
17 | |||
18 | impl Rule for EmptyPattern { | ||
19 | fn validate(&self, node: &SyntaxElement) -> Option<Report> { | ||
20 | if_chain! { | ||
21 | if let NodeOrToken::Node(node) = node; | ||
22 | if let Some(pattern) = Pattern::cast(node.clone()); | ||
23 | // no patterns within `{ }` | ||
24 | if pattern.entries().count() == 0; | ||
25 | // pattern is not bound | ||
26 | if pattern.at().is_none(); | ||
27 | then { | ||
28 | let at = node.text_range(); | ||
29 | let message = "This pattern is empty, use `_` instead"; | ||
30 | let replacement = make::ident("_").node().clone(); | ||
31 | Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) | ||
32 | } else { | ||
33 | None | ||
34 | } | ||
35 | } | ||
36 | } | ||
37 | } | ||
diff --git a/lib/src/lints/redundant_pattern_bind.rs b/lib/src/lints/redundant_pattern_bind.rs new file mode 100644 index 0000000..aebc549 --- /dev/null +++ b/lib/src/lints/redundant_pattern_bind.rs | |||
@@ -0,0 +1,41 @@ | |||
1 | use crate::{Lint, Metadata, Report, Rule, Suggestion}; | ||
2 | |||
3 | use if_chain::if_chain; | ||
4 | use macros::lint; | ||
5 | use rnix::{ | ||
6 | types::{Pattern, TokenWrapper, TypedNode}, | ||
7 | NodeOrToken, SyntaxElement, SyntaxKind, | ||
8 | }; | ||
9 | |||
10 | #[lint( | ||
11 | name = "redundant pattern bind", | ||
12 | note = "Found redundant pattern bind in function argument", | ||
13 | code = 10, | ||
14 | match_with = SyntaxKind::NODE_PATTERN | ||
15 | )] | ||
16 | struct RedundantPatternBind; | ||
17 | |||
18 | impl Rule for RedundantPatternBind { | ||
19 | fn validate(&self, node: &SyntaxElement) -> Option<Report> { | ||
20 | if_chain! { | ||
21 | if let NodeOrToken::Node(node) = node; | ||
22 | if let Some(pattern) = Pattern::cast(node.clone()); | ||
23 | // no patterns within `{ }` | ||
24 | if pattern.entries().count() == 0; | ||
25 | |||
26 | // pattern is just ellipsis | ||
27 | if pattern.ellipsis(); | ||
28 | |||
29 | // pattern is bound | ||
30 | if let Some(ident) = pattern.at(); | ||
31 | then { | ||
32 | let at = node.text_range(); | ||
33 | let message = format!("This pattern bind is redundant, use `{}` instead", ident.as_str()); | ||
34 | let replacement = ident.node().clone(); | ||
35 | Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) | ||
36 | } else { | ||
37 | None | ||
38 | } | ||
39 | } | ||
40 | } | ||
41 | } | ||
diff --git a/lib/src/lints/unquoted_splice.rs b/lib/src/lints/unquoted_splice.rs new file mode 100644 index 0000000..4d1ed69 --- /dev/null +++ b/lib/src/lints/unquoted_splice.rs | |||
@@ -0,0 +1,33 @@ | |||
1 | use crate::{make, Lint, Metadata, Report, Rule, Suggestion}; | ||
2 | |||
3 | use if_chain::if_chain; | ||
4 | use macros::lint; | ||
5 | use rnix::{ | ||
6 | types::{Dynamic, TypedNode}, | ||
7 | NodeOrToken, SyntaxElement, SyntaxKind, | ||
8 | }; | ||
9 | |||
10 | #[lint( | ||
11 | name = "unquoted splice", | ||
12 | note = "Found unquoted splice expression", | ||
13 | code = 9, | ||
14 | match_with = SyntaxKind::NODE_DYNAMIC | ||
15 | )] | ||
16 | struct UnquotedSplice; | ||
17 | |||
18 | impl Rule for UnquotedSplice { | ||
19 | fn validate(&self, node: &SyntaxElement) -> Option<Report> { | ||
20 | if_chain! { | ||
21 | if let NodeOrToken::Node(node) = node; | ||
22 | if Dynamic::cast(node.clone()).is_some(); | ||
23 | then { | ||
24 | let at = node.text_range(); | ||
25 | let replacement = make::quote(&node).node().clone(); | ||
26 | let message = "Consider quoting this splice expression"; | ||
27 | Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) | ||
28 | } else { | ||
29 | None | ||
30 | } | ||
31 | } | ||
32 | } | ||
33 | } | ||
diff --git a/lib/src/make.rs b/lib/src/make.rs index 217ba12..44adae6 100644 --- a/lib/src/make.rs +++ b/lib/src/make.rs | |||
@@ -24,6 +24,10 @@ pub fn parenthesize(node: &SyntaxNode) -> types::Paren { | |||
24 | ast_from_text(&format!("({})", node)) | 24 | ast_from_text(&format!("({})", node)) |
25 | } | 25 | } |
26 | 26 | ||
27 | pub fn quote(node: &SyntaxNode) -> types::Str { | ||
28 | ast_from_text(&format!("\"{}\"", node)) | ||
29 | } | ||
30 | |||
27 | pub fn unary_not(node: &SyntaxNode) -> types::UnaryOp { | 31 | pub fn unary_not(node: &SyntaxNode) -> types::UnaryOp { |
28 | ast_from_text(&format!("!{}", node)) | 32 | ast_from_text(&format!("!{}", node)) |
29 | } | 33 | } |
@@ -40,6 +40,13 @@ Lint ideas | |||
40 | - manual map over list | 40 | - manual map over list |
41 | - merge inherit | 41 | - merge inherit |
42 | - merge inherit-from | 42 | - merge inherit-from |
43 | - empty inherit | ||
44 | - useless antiquote/splice (where is antiquote truly | ||
45 | required?) | ||
46 | - useless variadic (things like `{...} : expr`, replace with | ||
47 | `_: expr`) | ||
48 | - redundant pattern `{...} @ inputs : expr`, replace with | ||
49 | `inputs: expr` | ||
43 | 50 | ||
44 | Extensions | 51 | Extensions |
45 | ---------- | 52 | ---------- |