From c00900120d88c3d42666ed93716b821bd694e8d6 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 2 Oct 2021 13:39:11 +0530 Subject: new lint: manual_inherit --- lib/src/lints/empty_let_in.rs | 3 +-- lib/src/lints/manual_inherit.rs | 43 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 lib/src/lints/manual_inherit.rs (limited to 'lib/src/lints') 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}; use if_chain::if_chain; use macros::lint; use rnix::{ - types::{LetIn, TypedNode, - EntryHolder}, + types::{LetIn, TypedNode, EntryHolder}, NodeOrToken, SyntaxElement, SyntaxKind, }; 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 @@ +use crate::{make, Lint, Metadata, Report, Rule, Suggestion}; + +use if_chain::if_chain; +use macros::lint; +use rnix::{ + types::{KeyValue, Ident, TypedNode, TokenWrapper}, + NodeOrToken, SyntaxElement, SyntaxKind, +}; + +#[lint( + name = "manual inherit", + note = "Assignment instead of `inherit` keyword", + code = 3, + match_with = SyntaxKind::NODE_KEY_VALUE +)] +struct ManualInherit; + +impl Rule for ManualInherit { + fn validate(&self, node: &SyntaxElement) -> Option { + if_chain! { + if let NodeOrToken::Node(key_value_node) = node; + if let Some(key_value_stmt) = KeyValue::cast(key_value_node.clone()); + if let Some(key_path) = key_value_stmt.key(); + if let Some(key_node) = key_path.path().next(); + if let Some(key) = Ident::cast(key_node); + + if let Some(value_node) = key_value_stmt.value(); + if let Some(value) = Ident::cast(value_node); + + if key.as_str() == value.as_str(); + + then { + let at = node.text_range(); + let replacement = make::inherit_stmt(&[key]).node().clone(); + let message = format!("The assignment `{}` is better written with `inherit`", node); + Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) + } else { + None + } + } + } +} + -- cgit v1.2.3