diff options
Diffstat (limited to 'lib/src/lints/redundant_pattern_bind.rs')
-rw-r--r-- | lib/src/lints/redundant_pattern_bind.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/src/lints/redundant_pattern_bind.rs b/lib/src/lints/redundant_pattern_bind.rs new file mode 100644 index 0000000..aebc549 --- /dev/null +++ b/lib/src/lints/redundant_pattern_bind.rs | |||
@@ -0,0 +1,41 @@ | |||
1 | use crate::{Lint, Metadata, Report, Rule, Suggestion}; | ||
2 | |||
3 | use if_chain::if_chain; | ||
4 | use macros::lint; | ||
5 | use rnix::{ | ||
6 | types::{Pattern, TokenWrapper, TypedNode}, | ||
7 | NodeOrToken, SyntaxElement, SyntaxKind, | ||
8 | }; | ||
9 | |||
10 | #[lint( | ||
11 | name = "redundant pattern bind", | ||
12 | note = "Found redundant pattern bind in function argument", | ||
13 | code = 10, | ||
14 | match_with = SyntaxKind::NODE_PATTERN | ||
15 | )] | ||
16 | struct RedundantPatternBind; | ||
17 | |||
18 | impl Rule for RedundantPatternBind { | ||
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 | |||
26 | // pattern is just ellipsis | ||
27 | if pattern.ellipsis(); | ||
28 | |||
29 | // pattern is bound | ||
30 | if let Some(ident) = pattern.at(); | ||
31 | then { | ||
32 | let at = node.text_range(); | ||
33 | let message = format!("This pattern bind is redundant, use `{}` instead", ident.as_str()); | ||
34 | let replacement = ident.node().clone(); | ||
35 | Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) | ||
36 | } else { | ||
37 | None | ||
38 | } | ||
39 | } | ||
40 | } | ||
41 | } | ||