aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lints/deprecated_is_null.rs
blob: c814e87a2647d1d59292a24d137b2acd749f66ea (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
60
use crate::{make, Metadata, Report, Rule, Suggestion};

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

/// ## What it does
/// Checks for usage of the `isNull` function.
///
/// ## Why is this bad?
/// `isNull` is deprecated.
///
/// ## Example
///
/// Instead of `isNull` for `null` checks,
///
/// ```nix
/// isNull e
/// ```
///
/// use the equality operator:
///
/// ```nix
/// e == null
/// ```
#[lint(
    name = "deprecated_is_null",
    note = "Found usage of deprecated builtin isNull",
    code = 13,
    match_with = SyntaxKind::NODE_APPLY
)]
struct DeprecatedIsNull;

impl Rule for DeprecatedIsNull {
    fn validate(&self, node: &SyntaxElement) -> Option<Report> {
        if_chain! {
            if let NodeOrToken::Node(node) = node;
            if let Some(apply) = Apply::cast(node.clone());
            if let Some(ident) = Ident::cast(apply.lambda()?);
            if ident.as_str() == "isNull";

            if let Some(value) = apply.value();
            then {
                let null = make::ident("null");
                let binop = make::binary(&value, "==", null.node());
                let parenthesized = make::parenthesize(binop.node());

                let at = node.text_range();
                let replacement = parenthesized.node().clone();
                let message = "`isNull` is deprecated, check equality with `null` instead";
                Some(self.report().suggest(at, message, Suggestion::new(at, replacement)))
            } else {
                None
            }
        }
    }
}