From 3c9a675b9d776bf2210803d582e9af88f9df2631 Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 28 Oct 2021 07:50:36 +0530 Subject: fix fp with manual_inherit and manual_inherit_from the lint also needed to validate if the length of the path component was exactly one, and not just check if the first component matches the value. --- bin/src/fix/single.rs | 16 +++++++--------- bin/src/main.rs | 7 ++++++- lib/src/lib.rs | 1 + lib/src/lints/manual_inherit.rs | 9 +++++---- lib/src/lints/manual_inherit_from.rs | 11 +++++------ 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/bin/src/fix/single.rs b/bin/src/fix/single.rs index 4d492f8..15a2ef4 100644 --- a/bin/src/fix/single.rs +++ b/bin/src/fix/single.rs @@ -36,15 +36,13 @@ fn find(offset: TextSize, src: &str) -> Result { .node() .preorder_with_tokens() .filter_map(|event| match event { - WalkEvent::Enter(child) => { - LINTS.get(&child.kind()).map(|rules| { - rules - .iter() - .filter_map(|rule| rule.validate(&child)) - .filter(|report| report.total_suggestion_range().is_some()) - .next() - }) - } + WalkEvent::Enter(child) => LINTS.get(&child.kind()).map(|rules| { + rules + .iter() + .filter_map(|rule| rule.validate(&child)) + .filter(|report| report.total_suggestion_range().is_some()) + .next() + }), _ => None, }) .flatten() diff --git a/bin/src/main.rs b/bin/src/main.rs index 9e9c8ac..c5d0626 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -60,7 +60,12 @@ fn _main() -> Result<(), StatixErr> { let src = if let Some(path) = &single_config.target { std::fs::read_to_string(&path).map_err(SingleFixErr::InvalidPath)? } else { - io::stdin().lock().lines().map(|l| l.unwrap()).collect::>().join("\n") + io::stdin() + .lock() + .lines() + .map(|l| l.unwrap()) + .collect::>() + .join("\n") }; let path_id = if let Some(path) = &single_config.target { diff --git a/lib/src/lib.rs b/lib/src/lib.rs index c2f24c6..753e5c1 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,3 +1,4 @@ +#![recursion_limit = "1024"] mod lints; mod make; diff --git a/lib/src/lints/manual_inherit.rs b/lib/src/lints/manual_inherit.rs index 69f032b..0a6933c 100644 --- a/lib/src/lints/manual_inherit.rs +++ b/lib/src/lints/manual_inherit.rs @@ -3,7 +3,7 @@ use crate::{make, Lint, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; use rnix::{ - types::{KeyValue, Ident, TypedNode, TokenWrapper}, + types::{Ident, KeyValue, TokenWrapper, TypedNode}, NodeOrToken, SyntaxElement, SyntaxKind, }; @@ -20,8 +20,10 @@ impl Rule for ManualInherit { if_chain! { if let NodeOrToken::Node(node) = node; if let Some(key_value_stmt) = KeyValue::cast(node.clone()); - if let Some(key_path) = key_value_stmt.key(); - if let Some(key_node) = key_path.path().next(); + if let mut key_path = key_value_stmt.key()?.path(); + if let Some(key_node) = key_path.next(); + // ensure that path has exactly one component + if key_path.next().is_none(); if let Some(key) = Ident::cast(key_node); if let Some(value_node) = key_value_stmt.value(); @@ -40,4 +42,3 @@ impl Rule for ManualInherit { } } } - diff --git a/lib/src/lints/manual_inherit_from.rs b/lib/src/lints/manual_inherit_from.rs index 2bff5f8..355ed8a 100644 --- a/lib/src/lints/manual_inherit_from.rs +++ b/lib/src/lints/manual_inherit_from.rs @@ -3,7 +3,7 @@ use crate::{make, Lint, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; use rnix::{ - types::{KeyValue, Ident, Select, TypedNode, TokenWrapper}, + types::{Ident, KeyValue, Select, TokenWrapper, TypedNode}, NodeOrToken, SyntaxElement, SyntaxKind, }; @@ -20,8 +20,9 @@ impl Rule for ManualInherit { if_chain! { if let NodeOrToken::Node(node) = node; if let Some(key_value_stmt) = KeyValue::cast(node.clone()); - if let Some(key_path) = key_value_stmt.key(); - if let Some(key_node) = key_path.path().next(); + if let mut key_path = key_value_stmt.key()?.path(); + if let Some(key_node) = key_path.next(); + if key_path.next().is_none(); if let Some(key) = Ident::cast(key_node); if let Some(value_node) = key_value_stmt.value(); @@ -35,7 +36,7 @@ impl Rule for ManualInherit { let at = node.text_range(); let replacement = { let set = value.set()?; - make::inherit_from_stmt(set, &[key]).node().clone() + make::inherit_from_stmt(set, &[key]).node().clone() }; let message = "This assignment is better written with `inherit`"; Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) @@ -45,5 +46,3 @@ impl Rule for ManualInherit { } } } - - -- cgit v1.2.3