aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lints/empty_inherit.rs
blob: 871c218fa2a5133d96d4324cdd6cf294c5b53156 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::{make, session::SessionInfo, 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, _sess: &SessionInfo) -> Option<Report> {
        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
            }
        }
    }
}