aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lints/deprecated_to_path.rs
blob: 36cbe359e69d342df55e8a07ad0d399fc323b4fd (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
58
59
use crate::{session::SessionInfo, Metadata, Report, Rule};

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

/// ## What it does
/// Checks for usage of the `toPath` function.
///
/// ## Why is this bad?
/// `toPath` is deprecated.
///
/// ## Example
///
/// ```nix
/// builtins.toPath "/path"
/// ```
///
/// Try these instead:
///
/// ```nix
/// # to convert the string to an absolute path:
/// /. + "/path"
/// # => /abc
///
/// # to convert the string to a path relative to the current directory:
/// ./. + "/bin"
/// # => /home/np/statix/bin
/// ```
#[lint(
    name = "deprecated_to_path",
    note = "Found usage of deprecated builtin toPath",
    code = 17,
    match_with = SyntaxKind::NODE_APPLY
)]
struct DeprecatedIsNull;

static ALLOWED_PATHS: &[&str; 2] = &["builtins.toPath", "toPath"];

impl Rule for DeprecatedIsNull {
    fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option<Report> {
        if_chain! {
            if let NodeOrToken::Node(node) = node;
            if let Some(apply) = Apply::cast(node.clone());
            let lambda_path = apply.lambda()?.to_string();
            if ALLOWED_PATHS.iter().any(|&p| p == lambda_path.as_str());
            then {
                let at = node.text_range();
                let message = format!("`{}` is deprecated, see `:doc builtins.toPath` within the REPL for more", lambda_path);
                Some(self.report().diagnostic(at, message))
            } else {
                None
            }
        }
    }
}