diff options
author | Akshay <[email protected]> | 2021-10-02 06:20:47 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-10-02 06:21:27 +0100 |
commit | a60429891fe9eb5290f95a52dd5e56f62d25d344 (patch) | |
tree | 3623ae9c2204e666adcacf9403599378af49e5e7 | |
parent | 2dacf12a3df91f27e5f290dcf495378076da3eed (diff) |
new lint: empty-let-in
-rw-r--r-- | Cargo.lock | 20 | ||||
-rw-r--r-- | lib/src/lib.rs | 6 | ||||
-rw-r--r-- | lib/src/lints.rs | 1 | ||||
-rw-r--r-- | lib/src/lints/empty_let_in.rs | 42 |
4 files changed, 56 insertions, 13 deletions
@@ -24,16 +24,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
24 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" | 24 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" |
25 | 25 | ||
26 | [[package]] | 26 | [[package]] |
27 | name = "bin" | ||
28 | version = "0.1.0" | ||
29 | dependencies = [ | ||
30 | "anyhow", | ||
31 | "ariadne", | ||
32 | "lib", | ||
33 | "rnix", | ||
34 | ] | ||
35 | |||
36 | [[package]] | ||
37 | name = "cbitset" | 27 | name = "cbitset" |
38 | version = "0.2.0" | 28 | version = "0.2.0" |
39 | source = "registry+https://github.com/rust-lang/crates.io-index" | 29 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -158,6 +148,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
158 | checksum = "b203e79e90905594272c1c97c7af701533d42adaab0beb3859018e477d54a3b0" | 148 | checksum = "b203e79e90905594272c1c97c7af701533d42adaab0beb3859018e477d54a3b0" |
159 | 149 | ||
160 | [[package]] | 150 | [[package]] |
151 | name = "statix" | ||
152 | version = "0.1.0" | ||
153 | dependencies = [ | ||
154 | "anyhow", | ||
155 | "ariadne", | ||
156 | "lib", | ||
157 | "rnix", | ||
158 | ] | ||
159 | |||
160 | [[package]] | ||
161 | name = "syn" | 161 | name = "syn" |
162 | version = "1.0.76" | 162 | version = "1.0.76" |
163 | source = "registry+https://github.com/rust-lang/crates.io-index" | 163 | source = "registry+https://github.com/rust-lang/crates.io-index" |
diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 93792d4..ab4a14a 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs | |||
@@ -32,7 +32,7 @@ impl Report { | |||
32 | self | 32 | self |
33 | } | 33 | } |
34 | /// Add a diagnostic with a fix to this report | 34 | /// Add a diagnostic with a fix to this report |
35 | pub fn suggest(mut self, at: TextRange, message: String, suggestion: Suggestion) -> Self { | 35 | pub fn suggest<S: AsRef<str>>(mut self, at: TextRange, message: S, suggestion: Suggestion) -> Self { |
36 | self.diagnostics.push(Diagnostic::suggest(at, message, suggestion)); | 36 | self.diagnostics.push(Diagnostic::suggest(at, message, suggestion)); |
37 | self | 37 | self |
38 | } | 38 | } |
@@ -54,8 +54,8 @@ impl Diagnostic { | |||
54 | Self { at, message, suggestion: None } | 54 | Self { at, message, suggestion: None } |
55 | } | 55 | } |
56 | /// Construct a diagnostic with a fix. | 56 | /// Construct a diagnostic with a fix. |
57 | pub fn suggest(at: TextRange, message: String, suggestion: Suggestion) -> Self { | 57 | pub fn suggest<S: AsRef<str>>(at: TextRange, message: S, suggestion: Suggestion) -> Self { |
58 | Self { at, message, suggestion: Some(suggestion) } | 58 | Self { at, message: message.as_ref().into(), suggestion: Some(suggestion) } |
59 | } | 59 | } |
60 | } | 60 | } |
61 | 61 | ||
diff --git a/lib/src/lints.rs b/lib/src/lints.rs index 15d9063..a5fe0da 100644 --- a/lib/src/lints.rs +++ b/lib/src/lints.rs | |||
@@ -2,4 +2,5 @@ use crate::lint_map; | |||
2 | 2 | ||
3 | lint_map! { | 3 | lint_map! { |
4 | bool_comparison, | 4 | bool_comparison, |
5 | empty_let_in, | ||
5 | } | 6 | } |
diff --git a/lib/src/lints/empty_let_in.rs b/lib/src/lints/empty_let_in.rs new file mode 100644 index 0000000..4b074e7 --- /dev/null +++ b/lib/src/lints/empty_let_in.rs | |||
@@ -0,0 +1,42 @@ | |||
1 | use crate::{Lint, Metadata, Report, Rule, Suggestion}; | ||
2 | |||
3 | use if_chain::if_chain; | ||
4 | use macros::lint; | ||
5 | use rnix::{ | ||
6 | types::{LetIn, TypedNode, | ||
7 | EntryHolder}, | ||
8 | NodeOrToken, SyntaxElement, SyntaxKind, | ||
9 | }; | ||
10 | |||
11 | #[lint( | ||
12 | name = "empty let-in", | ||
13 | note = "Useless let-in expression", | ||
14 | code = 2, | ||
15 | match_with = SyntaxKind::NODE_LET_IN | ||
16 | )] | ||
17 | struct EmptyLetIn; | ||
18 | |||
19 | impl Rule for EmptyLetIn { | ||
20 | fn validate(&self, node: &SyntaxElement) -> Option<Report> { | ||
21 | if_chain! { | ||
22 | if let NodeOrToken::Node(let_in_node) = node; | ||
23 | if let Some(let_in_expr) = LetIn::cast(let_in_node.clone()); | ||
24 | let entries = let_in_expr.entries(); | ||
25 | let inherits = let_in_expr.inherits(); | ||
26 | |||
27 | if entries.count() == 0; | ||
28 | if inherits.count() == 0; | ||
29 | |||
30 | if let Some(body) = let_in_expr.body(); | ||
31 | then { | ||
32 | let at = node.text_range(); | ||
33 | let replacement = body; | ||
34 | let message = "This let-in expression has no entries"; | ||
35 | Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) | ||
36 | } else { | ||
37 | None | ||
38 | } | ||
39 | } | ||
40 | } | ||
41 | } | ||
42 | |||