From 7a430237eea3e1d296e0d69e40f154a3727ba2fc Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 2 Oct 2021 18:40:08 +0530 Subject: new lint: manual_inherit_from --- lib/src/lints.rs | 1 + lib/src/lints/manual_inherit.rs | 2 +- lib/src/lints/manual_inherit_from.rs | 49 ++++++++++++++++++++++++++++++++++++ lib/src/make.rs | 9 +++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 lib/src/lints/manual_inherit_from.rs (limited to 'lib/src') 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! { bool_comparison, empty_let_in, manual_inherit, + manual_inherit_from, } 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::{ #[lint( name = "manual inherit", - note = "Assignment instead of `inherit` keyword", + note = "Assignment instead of inherit", code = 3, match_with = SyntaxKind::NODE_KEY_VALUE )] 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 @@ +use crate::{make, Lint, Metadata, Report, Rule, Suggestion}; + +use if_chain::if_chain; +use macros::lint; +use rnix::{ + types::{KeyValue, Ident, Select, TypedNode, TokenWrapper}, + NodeOrToken, SyntaxElement, SyntaxKind, +}; + +#[lint( + name = "manual inherit from", + note = "Assignment instead of inherit from", + code = 4, + 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) = Select::cast(value_node); + if let Some(index_node) = value.index(); + if let Some(index) = Ident::cast(index_node); + + if key.as_str() == index.as_str(); + + then { + let at = node.text_range(); + let replacement = { + let set = value.set()?; + make::inherit_from_stmt(set, &[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 + } + } + } +} + + 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) -> ty .join(" "); ast_from_text(&format!("{{ inherit {}; }}", inherited_idents)) } + +pub fn inherit_from_stmt<'a>(from: SyntaxNode, nodes: impl IntoIterator) -> types::Inherit { + let inherited_idents = nodes + .into_iter() + .map(|i| i.as_str().to_owned()) + .collect::>() + .join(" "); + ast_from_text(&format!("{{ inherit ({}) {}; }}", from, inherited_idents)) +} -- cgit v1.2.3