aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-10-02 06:20:47 +0100
committerAkshay <[email protected]>2021-10-02 06:21:27 +0100
commita60429891fe9eb5290f95a52dd5e56f62d25d344 (patch)
tree3623ae9c2204e666adcacf9403599378af49e5e7
parent2dacf12a3df91f27e5f290dcf495378076da3eed (diff)
new lint: empty-let-in
-rw-r--r--Cargo.lock20
-rw-r--r--lib/src/lib.rs6
-rw-r--r--lib/src/lints.rs1
-rw-r--r--lib/src/lints/empty_let_in.rs42
4 files changed, 56 insertions, 13 deletions
diff --git a/Cargo.lock b/Cargo.lock
index f222d10..bae8674 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -24,16 +24,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
24checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 24checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
25 25
26[[package]] 26[[package]]
27name = "bin"
28version = "0.1.0"
29dependencies = [
30 "anyhow",
31 "ariadne",
32 "lib",
33 "rnix",
34]
35
36[[package]]
37name = "cbitset" 27name = "cbitset"
38version = "0.2.0" 28version = "0.2.0"
39source = "registry+https://github.com/rust-lang/crates.io-index" 29source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -158,6 +148,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
158checksum = "b203e79e90905594272c1c97c7af701533d42adaab0beb3859018e477d54a3b0" 148checksum = "b203e79e90905594272c1c97c7af701533d42adaab0beb3859018e477d54a3b0"
159 149
160[[package]] 150[[package]]
151name = "statix"
152version = "0.1.0"
153dependencies = [
154 "anyhow",
155 "ariadne",
156 "lib",
157 "rnix",
158]
159
160[[package]]
161name = "syn" 161name = "syn"
162version = "1.0.76" 162version = "1.0.76"
163source = "registry+https://github.com/rust-lang/crates.io-index" 163source = "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
3lint_map! { 3lint_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 @@
1use crate::{Lint, Metadata, Report, Rule, Suggestion};
2
3use if_chain::if_chain;
4use macros::lint;
5use 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)]
17struct EmptyLetIn;
18
19impl 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