aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lints
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 /lib/src/lints
parent2dacf12a3df91f27e5f290dcf495378076da3eed (diff)
new lint: empty-let-in
Diffstat (limited to 'lib/src/lints')
-rw-r--r--lib/src/lints/empty_let_in.rs42
1 files changed, 42 insertions, 0 deletions
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