diff options
author | Akshay <[email protected]> | 2021-11-04 11:36:02 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2021-11-04 11:36:02 +0000 |
commit | 07d39899d32ed4c7ae822e98f4a8b28c72c48a99 (patch) | |
tree | 93968790365f238e8138669182cefab764ceae31 | |
parent | 5b87c6feb3e4a2fcc30ad94125be3dcd4e554754 (diff) |
new lint: deprecated_is_null
-rw-r--r-- | lib/src/lints.rs | 1 | ||||
-rw-r--r-- | lib/src/lints/deprecated_is_null.rs | 41 | ||||
-rw-r--r-- | lib/src/make.rs | 5 | ||||
-rw-r--r-- | notes.txt | 2 |
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 @@ | |||
1 | use crate::{make, Metadata, Report, Rule, Suggestion}; | ||
2 | |||
3 | use if_chain::if_chain; | ||
4 | use macros::lint; | ||
5 | use 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 | )] | ||
16 | struct DeprecatedIsNull; | ||
17 | |||
18 | impl 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 { | |||
83 | pub fn empty() -> types::Root { | 83 | pub fn empty() -> types::Root { |
84 | ast_from_text("") | 84 | ast_from_text("") |
85 | } | 85 | } |
86 | |||
87 | // TODO: make `op` strongly typed here | ||
88 | pub fn binary(lhs: &SyntaxNode, op: &str, rhs: &SyntaxNode) -> types::BinOp { | ||
89 | ast_from_text(&format!("{} {} {}", lhs, op, rhs)) | ||
90 | } | ||
@@ -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 | ||
51 | Extensions | 53 | Extensions |
52 | ---------- | 54 | ---------- |