aboutsummaryrefslogtreecommitdiff
path: root/lib/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-12-04 07:32:57 +0000
committerAkshay <[email protected]>2021-12-04 07:34:20 +0000
commit21775f28cd1bdde8eddd84a508813b01d620db89 (patch)
treeca78b5732040253bc49baafa5f1889e848172daa /lib/src
parent1079486539d44b2e70c623fb4948d6e0b9b11812 (diff)
new lint: empty_inherit
Diffstat (limited to 'lib/src')
-rw-r--r--lib/src/lib.rs1
-rw-r--r--lib/src/lints.rs1
-rw-r--r--lib/src/lints/empty_inherit.rs53
-rw-r--r--lib/src/utils.rs16
4 files changed, 71 insertions, 0 deletions
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 @@
1#![recursion_limit = "1024"] 1#![recursion_limit = "1024"]
2mod lints; 2mod lints;
3mod make; 3mod make;
4mod utils;
4 5
5pub use lints::LINTS; 6pub use lints::LINTS;
6 7
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! {
14 redundant_pattern_bind, 14 redundant_pattern_bind,
15 unquoted_uri, 15 unquoted_uri,
16 deprecated_is_null, 16 deprecated_is_null,
17 empty_inherit,
17} 18}
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 @@
1use crate::{make, utils, Metadata, Report, Rule, Suggestion};
2
3use if_chain::if_chain;
4use macros::lint;
5use rnix::{
6 types::{Inherit, TypedNode},
7 NodeOrToken, SyntaxElement, SyntaxKind,
8};
9
10/// ## What it does
11/// Checks for empty inherit statements.
12///
13/// ## Why is this bad?
14/// Useless code, probably the result of a refactor.
15///
16/// ## Example
17///
18/// ```nix
19/// inherit;
20/// ```
21///
22/// Remove it altogether.
23#[lint(
24 name = "empty_inherit",
25 note = "Found empty inherit statement",
26 code = 14,
27 match_with = SyntaxKind::NODE_INHERIT
28)]
29struct EmptyInherit;
30
31impl Rule for EmptyInherit {
32 fn validate(&self, node: &SyntaxElement) -> Option<Report> {
33 if_chain! {
34 if let NodeOrToken::Node(node) = node;
35 if let Some(inherit_stmt) = Inherit::cast(node.clone());
36 if inherit_stmt.from().is_none();
37 if inherit_stmt.idents().count() == 0;
38 then {
39 let at = node.text_range();
40 let replacement = make::empty().node().clone();
41 let replacement_at = utils::with_preceeding_whitespace(node);
42 let message = "Remove this empty `inherit` statement";
43 Some(
44 self
45 .report()
46 .suggest(at, message, Suggestion::new(replacement_at, replacement))
47 )
48 } else {
49 None
50 }
51 }
52 }
53}
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 @@
1use rnix::{SyntaxKind, SyntaxNode, TextRange};
2
3pub fn with_preceeding_whitespace(node: &SyntaxNode) -> TextRange {
4 let start = node
5 .prev_sibling_or_token()
6 .map(|t| {
7 if t.kind() == SyntaxKind::TOKEN_WHITESPACE {
8 t.text_range().start()
9 } else {
10 t.text_range().end()
11 }
12 })
13 .unwrap_or(node.text_range().start());
14 let end = node.text_range().end();
15 TextRange::new(start, end)
16}