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 /lib/src/lints/empty_let_in.rs | |
parent | 2dacf12a3df91f27e5f290dcf495378076da3eed (diff) |
new lint: empty-let-in
Diffstat (limited to 'lib/src/lints/empty_let_in.rs')
-rw-r--r-- | lib/src/lints/empty_let_in.rs | 42 |
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 @@ | |||
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 | |||