From 21775f28cd1bdde8eddd84a508813b01d620db89 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 4 Dec 2021 13:02:57 +0530 Subject: new lint: empty_inherit --- lib/src/lints/empty_inherit.rs | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 lib/src/lints/empty_inherit.rs (limited to 'lib/src/lints') diff --git a/lib/src/lints/empty_inherit.rs b/lib/src/lints/empty_inherit.rs new file mode 100644 index 0000000..9ae4cf2 --- /dev/null +++ b/lib/src/lints/empty_inherit.rs @@ -0,0 +1,53 @@ +use crate::{make, utils, Metadata, Report, Rule, Suggestion}; + +use if_chain::if_chain; +use macros::lint; +use rnix::{ + types::{Inherit, TypedNode}, + NodeOrToken, SyntaxElement, SyntaxKind, +}; + +/// ## What it does +/// Checks for empty inherit statements. +/// +/// ## Why is this bad? +/// Useless code, probably the result of a refactor. +/// +/// ## Example +/// +/// ```nix +/// inherit; +/// ``` +/// +/// Remove it altogether. +#[lint( + name = "empty_inherit", + note = "Found empty inherit statement", + code = 14, + match_with = SyntaxKind::NODE_INHERIT +)] +struct EmptyInherit; + +impl Rule for EmptyInherit { + fn validate(&self, node: &SyntaxElement) -> Option { + if_chain! { + if let NodeOrToken::Node(node) = node; + if let Some(inherit_stmt) = Inherit::cast(node.clone()); + if inherit_stmt.from().is_none(); + if inherit_stmt.idents().count() == 0; + then { + let at = node.text_range(); + let replacement = make::empty().node().clone(); + let replacement_at = utils::with_preceeding_whitespace(node); + let message = "Remove this empty `inherit` statement"; + Some( + self + .report() + .suggest(at, message, Suggestion::new(replacement_at, replacement)) + ) + } else { + None + } + } + } +} -- cgit v1.2.3