diff options
Diffstat (limited to 'lib/src/lints/empty_pattern.rs')
-rw-r--r-- | lib/src/lints/empty_pattern.rs | 37 |
1 files changed, 37 insertions, 0 deletions
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 | } | ||