aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lints/unquoted_splice.rs
blob: c2fd6e44bfb4b4b56e67188a97bbcca0922492b9 (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
54
55
56
57
use crate::{make, Metadata, Report, Rule, Suggestion};

use if_chain::if_chain;
use macros::lint;
use rnix::{
    types::{Dynamic, TypedNode},
    NodeOrToken, SyntaxElement, SyntaxKind,
};

/// ## What it does
/// Checks for antiquote/splice expressions that are not quoted.
///
/// ## Why is this bad?
/// An *anti*quoted expression should always occur within a *quoted*
/// expression.
///
/// ## Example
///
/// ```
/// let
///   pkgs = nixpkgs.legacyPackages.${system};
/// in
///   pkgs
/// ```
///
/// Quote the splice expression:
///
/// ```
/// let
///   pkgs = nixpkgs.legacyPackages."${system}";
/// in
///   pkgs
/// ```
#[lint(
    name = "unquoted splice",
    note = "Found unquoted splice expression",
    code = 9,
    match_with = SyntaxKind::NODE_DYNAMIC
)]
struct UnquotedSplice;

impl Rule for UnquotedSplice {
    fn validate(&self, node: &SyntaxElement) -> Option<Report> {
        if_chain! {
            if let NodeOrToken::Node(node) = node;
            if Dynamic::cast(node.clone()).is_some();
            then {
                let at = node.text_range();
                let replacement = make::quote(&node).node().clone();
                let message = "Consider quoting this splice expression";
                Some(self.report().suggest(at, message, Suggestion::new(at, replacement)))
            } else {
                None
            }
        }
    }
}