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 --- bin/tests/data/empty_inherit.nix | 3 ++ bin/tests/snapshots/main__empty_inherit.snap | 13 +++++++ lib/src/lib.rs | 1 + lib/src/lints.rs | 1 + lib/src/lints/empty_inherit.rs | 53 ++++++++++++++++++++++++++++ lib/src/utils.rs | 16 +++++++++ 6 files changed, 87 insertions(+) create mode 100644 bin/tests/data/empty_inherit.nix create mode 100644 bin/tests/snapshots/main__empty_inherit.snap create mode 100644 lib/src/lints/empty_inherit.rs create mode 100644 lib/src/utils.rs diff --git a/bin/tests/data/empty_inherit.nix b/bin/tests/data/empty_inherit.nix new file mode 100644 index 0000000..dc31afc --- /dev/null +++ b/bin/tests/data/empty_inherit.nix @@ -0,0 +1,3 @@ +{ + inherit; +} diff --git a/bin/tests/snapshots/main__empty_inherit.snap b/bin/tests/snapshots/main__empty_inherit.snap new file mode 100644 index 0000000..1d91e0a --- /dev/null +++ b/bin/tests/snapshots/main__empty_inherit.snap @@ -0,0 +1,13 @@ +--- +source: bin/tests/main.rs +expression: "&out" + +--- +[W14] Warning: Found empty inherit statement + ╭─[data/empty_inherit.nix:2:3] + │ + 2 │ inherit; + · ────┬─── + · ╰───── Remove this empty inherit statement +───╯ + diff --git a/lib/src/lib.rs b/lib/src/lib.rs index d96d9eb..a25b814 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,6 +1,7 @@ #![recursion_limit = "1024"] mod lints; mod make; +mod utils; pub use lints::LINTS; diff --git a/lib/src/lints.rs b/lib/src/lints.rs index 891e3c8..5de6b65 100644 --- a/lib/src/lints.rs +++ b/lib/src/lints.rs @@ -14,4 +14,5 @@ lints! { redundant_pattern_bind, unquoted_uri, deprecated_is_null, + empty_inherit, } 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 + } + } + } +} diff --git a/lib/src/utils.rs b/lib/src/utils.rs new file mode 100644 index 0000000..d7cf4c8 --- /dev/null +++ b/lib/src/utils.rs @@ -0,0 +1,16 @@ +use rnix::{SyntaxKind, SyntaxNode, TextRange}; + +pub fn with_preceeding_whitespace(node: &SyntaxNode) -> TextRange { + let start = node + .prev_sibling_or_token() + .map(|t| { + if t.kind() == SyntaxKind::TOKEN_WHITESPACE { + t.text_range().start() + } else { + t.text_range().end() + } + }) + .unwrap_or(node.text_range().start()); + let end = node.text_range().end(); + TextRange::new(start, end) +} -- cgit v1.2.3