From 07d39899d32ed4c7ae822e98f4a8b28c72c48a99 Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 4 Nov 2021 17:06:02 +0530 Subject: new lint: deprecated_is_null --- lib/src/lints.rs | 1 + lib/src/lints/deprecated_is_null.rs | 41 +++++++++++++++++++++++++++++++++++++ lib/src/make.rs | 5 +++++ notes.txt | 2 ++ 4 files changed, 49 insertions(+) create mode 100644 lib/src/lints/deprecated_is_null.rs 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! { empty_pattern, redundant_pattern_bind, unquoted_uri, + deprecated_is_null, } 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 @@ +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, +}; + +#[lint( + name = "deprecated isNull", + 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 { + 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 + } + } + } +} 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 { pub fn empty() -> types::Root { ast_from_text("") } + +// TODO: make `op` strongly typed here +pub fn binary(lhs: &SyntaxNode, op: &str, rhs: &SyntaxNode) -> types::BinOp { + ast_from_text(&format!("{} {} {}", lhs, op, rhs)) +} diff --git a/notes.txt b/notes.txt index 0e70373..bba644c 100644 --- a/notes.txt +++ b/notes.txt @@ -47,6 +47,8 @@ Lint ideas `_: expr`) - redundant pattern `{...} @ inputs : expr`, replace with `inputs: expr` +- useless hasAttr: `if x ? a then x.a else default` can be + replaced with `x.a or default` Extensions ---------- -- cgit v1.2.3