diff options
Diffstat (limited to 'lib/src/lints/empty_inherit.rs')
-rw-r--r-- | lib/src/lints/empty_inherit.rs | 53 |
1 files changed, 53 insertions, 0 deletions
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 @@ | |||
1 | use crate::{make, utils, Metadata, Report, Rule, Suggestion}; | ||
2 | |||
3 | use if_chain::if_chain; | ||
4 | use macros::lint; | ||
5 | use 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 | )] | ||
29 | struct EmptyInherit; | ||
30 | |||
31 | impl 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 | } | ||