diff options
author | Akshay <[email protected]> | 2021-10-02 09:09:11 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-10-02 09:09:39 +0100 |
commit | c00900120d88c3d42666ed93716b821bd694e8d6 (patch) | |
tree | c98bc6fb91ce886e2ea1e0506209477d0f1100ea /lib/src | |
parent | 088559eedd40b16fff2ce97e48006580be39dd03 (diff) |
new lint: manual_inherit
Diffstat (limited to 'lib/src')
-rw-r--r-- | lib/src/lints.rs | 1 | ||||
-rw-r--r-- | lib/src/lints/empty_let_in.rs | 3 | ||||
-rw-r--r-- | lib/src/lints/manual_inherit.rs | 43 | ||||
-rw-r--r-- | lib/src/make.rs | 15 |
4 files changed, 58 insertions, 4 deletions
diff --git a/lib/src/lints.rs b/lib/src/lints.rs index a5fe0da..7d5cd30 100644 --- a/lib/src/lints.rs +++ b/lib/src/lints.rs | |||
@@ -3,4 +3,5 @@ use crate::lint_map; | |||
3 | lint_map! { | 3 | lint_map! { |
4 | bool_comparison, | 4 | bool_comparison, |
5 | empty_let_in, | 5 | empty_let_in, |
6 | manual_inherit, | ||
6 | } | 7 | } |
diff --git a/lib/src/lints/empty_let_in.rs b/lib/src/lints/empty_let_in.rs index 4b074e7..207d253 100644 --- a/lib/src/lints/empty_let_in.rs +++ b/lib/src/lints/empty_let_in.rs | |||
@@ -3,8 +3,7 @@ use crate::{Lint, Metadata, Report, Rule, Suggestion}; | |||
3 | use if_chain::if_chain; | 3 | use if_chain::if_chain; |
4 | use macros::lint; | 4 | use macros::lint; |
5 | use rnix::{ | 5 | use rnix::{ |
6 | types::{LetIn, TypedNode, | 6 | types::{LetIn, TypedNode, EntryHolder}, |
7 | EntryHolder}, | ||
8 | NodeOrToken, SyntaxElement, SyntaxKind, | 7 | NodeOrToken, SyntaxElement, SyntaxKind, |
9 | }; | 8 | }; |
10 | 9 | ||
diff --git a/lib/src/lints/manual_inherit.rs b/lib/src/lints/manual_inherit.rs new file mode 100644 index 0000000..2f8367f --- /dev/null +++ b/lib/src/lints/manual_inherit.rs | |||
@@ -0,0 +1,43 @@ | |||
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::{KeyValue, Ident, TypedNode, TokenWrapper}, | ||
7 | NodeOrToken, SyntaxElement, SyntaxKind, | ||
8 | }; | ||
9 | |||
10 | #[lint( | ||
11 | name = "manual inherit", | ||
12 | note = "Assignment instead of `inherit` keyword", | ||
13 | code = 3, | ||
14 | match_with = SyntaxKind::NODE_KEY_VALUE | ||
15 | )] | ||
16 | struct ManualInherit; | ||
17 | |||
18 | impl Rule for ManualInherit { | ||
19 | fn validate(&self, node: &SyntaxElement) -> Option<Report> { | ||
20 | if_chain! { | ||
21 | if let NodeOrToken::Node(key_value_node) = node; | ||
22 | if let Some(key_value_stmt) = KeyValue::cast(key_value_node.clone()); | ||
23 | if let Some(key_path) = key_value_stmt.key(); | ||
24 | if let Some(key_node) = key_path.path().next(); | ||
25 | if let Some(key) = Ident::cast(key_node); | ||
26 | |||
27 | if let Some(value_node) = key_value_stmt.value(); | ||
28 | if let Some(value) = Ident::cast(value_node); | ||
29 | |||
30 | if key.as_str() == value.as_str(); | ||
31 | |||
32 | then { | ||
33 | let at = node.text_range(); | ||
34 | let replacement = make::inherit_stmt(&[key]).node().clone(); | ||
35 | let message = format!("The assignment `{}` is better written with `inherit`", node); | ||
36 | Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) | ||
37 | } else { | ||
38 | None | ||
39 | } | ||
40 | } | ||
41 | } | ||
42 | } | ||
43 | |||
diff --git a/lib/src/make.rs b/lib/src/make.rs index 0b21105..facd247 100644 --- a/lib/src/make.rs +++ b/lib/src/make.rs | |||
@@ -1,11 +1,13 @@ | |||
1 | use rnix::{SyntaxNode, types::{TypedNode, self}}; | 1 | use std::iter::IntoIterator; |
2 | |||
3 | use rnix::{SyntaxNode, types::{TypedNode, self, TokenWrapper}}; | ||
2 | 4 | ||
3 | fn ast_from_text<N: TypedNode>(text: &str) -> N { | 5 | fn ast_from_text<N: TypedNode>(text: &str) -> N { |
4 | let parse = rnix::parse(text); | 6 | let parse = rnix::parse(text); |
5 | let node = match parse.node().descendants().find_map(N::cast) { | 7 | let node = match parse.node().descendants().find_map(N::cast) { |
6 | Some(it) => it, | 8 | Some(it) => it, |
7 | None => { | 9 | None => { |
8 | panic!("Failed to make ast node `{}` from text {}", std::any::type_name::<N>(), text) | 10 | panic!("Failed to make ast node `{}` from text `{}`", std::any::type_name::<N>(), text) |
9 | } | 11 | } |
10 | }; | 12 | }; |
11 | node | 13 | node |
@@ -18,3 +20,12 @@ pub fn parenthesize(node: &SyntaxNode) -> types::Paren { | |||
18 | pub fn unary_not(node: &SyntaxNode) -> types::UnaryOp { | 20 | pub fn unary_not(node: &SyntaxNode) -> types::UnaryOp { |
19 | ast_from_text(&format!("!{}", node)) | 21 | ast_from_text(&format!("!{}", node)) |
20 | } | 22 | } |
23 | |||
24 | pub fn inherit_stmt<'a>(nodes: impl IntoIterator<Item = &'a types::Ident>) -> types::Inherit { | ||
25 | let inherited_idents = nodes | ||
26 | .into_iter() | ||
27 | .map(|i| i.as_str().to_owned()) | ||
28 | .collect::<Vec<_>>() | ||
29 | .join(" "); | ||
30 | ast_from_text(&format!("{{ inherit {}; }}", inherited_idents)) | ||
31 | } | ||