aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/src/lints.rs1
-rw-r--r--lib/src/lints/deprecated_is_null.rs41
-rw-r--r--lib/src/make.rs5
-rw-r--r--notes.txt2
4 files changed, 49 insertions, 0 deletions
diff --git a/lib/src/lints.rs b/lib/src/lints.rs
index 44d5922..8b0f92b 100644
--- a/lib/src/lints.rs
+++ b/lib/src/lints.rs
@@ -13,4 +13,5 @@ lint_map! {
13 empty_pattern, 13 empty_pattern,
14 redundant_pattern_bind, 14 redundant_pattern_bind,
15 unquoted_uri, 15 unquoted_uri,
16 deprecated_is_null,
16} 17}
diff --git a/lib/src/lints/deprecated_is_null.rs b/lib/src/lints/deprecated_is_null.rs
new file mode 100644
index 0000000..99fb3fb
--- /dev/null
+++ b/lib/src/lints/deprecated_is_null.rs
@@ -0,0 +1,41 @@
1use crate::{make, Metadata, Report, Rule, Suggestion};
2
3use if_chain::if_chain;
4use macros::lint;
5use rnix::{
6 types::{Apply, Ident, TokenWrapper, TypedNode},
7 NodeOrToken, SyntaxElement, SyntaxKind,
8};
9
10#[lint(
11 name = "deprecated isNull",
12 note = "Found usage of deprecated builtin isNull",
13 code = 13,
14 match_with = SyntaxKind::NODE_APPLY
15)]
16struct DeprecatedIsNull;
17
18impl Rule for DeprecatedIsNull {
19 fn validate(&self, node: &SyntaxElement) -> Option<Report> {
20 if_chain! {
21 if let NodeOrToken::Node(node) = node;
22 if let Some(apply) = Apply::cast(node.clone());
23 if let Some(ident) = Ident::cast(apply.lambda()?);
24 if ident.as_str() == "isNull";
25
26 if let Some(value) = apply.value();
27 then {
28 let null = make::ident("null");
29 let binop = make::binary(&value, "==", null.node());
30 let parenthesized = make::parenthesize(binop.node());
31
32 let at = node.text_range();
33 let replacement = parenthesized.node().clone();
34 let message = "`isNull` is deprecated, check equality with `null` instead";
35 Some(self.report().suggest(at, message, Suggestion::new(at, replacement)))
36 } else {
37 None
38 }
39 }
40 }
41}
diff --git a/lib/src/make.rs b/lib/src/make.rs
index 44adae6..c36fa7e 100644
--- a/lib/src/make.rs
+++ b/lib/src/make.rs
@@ -83,3 +83,8 @@ pub fn ident(text: &str) -> types::Ident {
83pub fn empty() -> types::Root { 83pub fn empty() -> types::Root {
84 ast_from_text("") 84 ast_from_text("")
85} 85}
86
87// TODO: make `op` strongly typed here
88pub fn binary(lhs: &SyntaxNode, op: &str, rhs: &SyntaxNode) -> types::BinOp {
89 ast_from_text(&format!("{} {} {}", lhs, op, rhs))
90}
diff --git a/notes.txt b/notes.txt
index 0e70373..bba644c 100644
--- a/notes.txt
+++ b/notes.txt
@@ -47,6 +47,8 @@ Lint ideas
47 `_: expr`) 47 `_: expr`)
48- redundant pattern `{...} @ inputs : expr`, replace with 48- redundant pattern `{...} @ inputs : expr`, replace with
49 `inputs: expr` 49 `inputs: expr`
50- useless hasAttr: `if x ? a then x.a else default` can be
51 replaced with `x.a or default`
50 52
51Extensions 53Extensions
52---------- 54----------