diff options
author | Akshay <[email protected]> | 2021-10-02 14:10:08 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-10-02 14:10:08 +0100 |
commit | 7a430237eea3e1d296e0d69e40f154a3727ba2fc (patch) | |
tree | 5a39f01ad5d7ea1931b0a884c613e8e4462c847f /lib | |
parent | c00900120d88c3d42666ed93716b821bd694e8d6 (diff) |
new lint: manual_inherit_from
Diffstat (limited to 'lib')
-rw-r--r-- | lib/src/lints.rs | 1 | ||||
-rw-r--r-- | lib/src/lints/manual_inherit.rs | 2 | ||||
-rw-r--r-- | lib/src/lints/manual_inherit_from.rs | 49 | ||||
-rw-r--r-- | lib/src/make.rs | 9 |
4 files changed, 60 insertions, 1 deletions
diff --git a/lib/src/lints.rs b/lib/src/lints.rs index 7d5cd30..30c1678 100644 --- a/lib/src/lints.rs +++ b/lib/src/lints.rs | |||
@@ -4,4 +4,5 @@ lint_map! { | |||
4 | bool_comparison, | 4 | bool_comparison, |
5 | empty_let_in, | 5 | empty_let_in, |
6 | manual_inherit, | 6 | manual_inherit, |
7 | manual_inherit_from, | ||
7 | } | 8 | } |
diff --git a/lib/src/lints/manual_inherit.rs b/lib/src/lints/manual_inherit.rs index 2f8367f..c1b10ec 100644 --- a/lib/src/lints/manual_inherit.rs +++ b/lib/src/lints/manual_inherit.rs | |||
@@ -9,7 +9,7 @@ use rnix::{ | |||
9 | 9 | ||
10 | #[lint( | 10 | #[lint( |
11 | name = "manual inherit", | 11 | name = "manual inherit", |
12 | note = "Assignment instead of `inherit` keyword", | 12 | note = "Assignment instead of inherit", |
13 | code = 3, | 13 | code = 3, |
14 | match_with = SyntaxKind::NODE_KEY_VALUE | 14 | match_with = SyntaxKind::NODE_KEY_VALUE |
15 | )] | 15 | )] |
diff --git a/lib/src/lints/manual_inherit_from.rs b/lib/src/lints/manual_inherit_from.rs new file mode 100644 index 0000000..055cba2 --- /dev/null +++ b/lib/src/lints/manual_inherit_from.rs | |||
@@ -0,0 +1,49 @@ | |||
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, Select, TypedNode, TokenWrapper}, | ||
7 | NodeOrToken, SyntaxElement, SyntaxKind, | ||
8 | }; | ||
9 | |||
10 | #[lint( | ||
11 | name = "manual inherit from", | ||
12 | note = "Assignment instead of inherit from", | ||
13 | code = 4, | ||
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) = Select::cast(value_node); | ||
29 | if let Some(index_node) = value.index(); | ||
30 | if let Some(index) = Ident::cast(index_node); | ||
31 | |||
32 | if key.as_str() == index.as_str(); | ||
33 | |||
34 | then { | ||
35 | let at = node.text_range(); | ||
36 | let replacement = { | ||
37 | let set = value.set()?; | ||
38 | make::inherit_from_stmt(set, &[key]).node().clone() | ||
39 | }; | ||
40 | let message = format!("The assignment `{}` is better written with `inherit`", node); | ||
41 | Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) | ||
42 | } else { | ||
43 | None | ||
44 | } | ||
45 | } | ||
46 | } | ||
47 | } | ||
48 | |||
49 | |||
diff --git a/lib/src/make.rs b/lib/src/make.rs index facd247..cf371a2 100644 --- a/lib/src/make.rs +++ b/lib/src/make.rs | |||
@@ -29,3 +29,12 @@ pub fn inherit_stmt<'a>(nodes: impl IntoIterator<Item = &'a types::Ident>) -> ty | |||
29 | .join(" "); | 29 | .join(" "); |
30 | ast_from_text(&format!("{{ inherit {}; }}", inherited_idents)) | 30 | ast_from_text(&format!("{{ inherit {}; }}", inherited_idents)) |
31 | } | 31 | } |
32 | |||
33 | pub fn inherit_from_stmt<'a>(from: SyntaxNode, nodes: impl IntoIterator<Item = &'a types::Ident>) -> types::Inherit { | ||
34 | let inherited_idents = nodes | ||
35 | .into_iter() | ||
36 | .map(|i| i.as_str().to_owned()) | ||
37 | .collect::<Vec<_>>() | ||
38 | .join(" "); | ||
39 | ast_from_text(&format!("{{ inherit ({}) {}; }}", from, inherited_idents)) | ||
40 | } | ||