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 /lib/src/lints | |
parent | 5b87c6feb3e4a2fcc30ad94125be3dcd4e554754 (diff) |
new lint: deprecated_is_null
Diffstat (limited to 'lib/src/lints')
-rw-r--r-- | lib/src/lints/deprecated_is_null.rs | 41 |
1 files changed, 41 insertions, 0 deletions
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 | } | ||