aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lints
diff options
context:
space:
mode:
Diffstat (limited to 'lib/src/lints')
-rw-r--r--lib/src/lints/empty_let_in.rs3
-rw-r--r--lib/src/lints/manual_inherit.rs43
2 files changed, 44 insertions, 2 deletions
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};
3use if_chain::if_chain; 3use if_chain::if_chain;
4use macros::lint; 4use macros::lint;
5use rnix::{ 5use 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 @@
1use crate::{make, Lint, Metadata, Report, Rule, Suggestion};
2
3use if_chain::if_chain;
4use macros::lint;
5use 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)]
16struct ManualInherit;
17
18impl 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