From d1ff222bcf94152cd657233cffd8c14a45788c26 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 29 Dec 2021 10:53:38 +0530 Subject: allow for version based lints --- lib/src/lints/bool_comparison.rs | 4 +- lib/src/lints/collapsible_let_in.rs | 4 +- lib/src/lints/deprecated_is_null.rs | 4 +- lib/src/lints/empty_inherit.rs | 4 +- lib/src/lints/empty_let_in.rs | 4 +- lib/src/lints/empty_pattern.rs | 4 +- lib/src/lints/eta_reduction.rs | 4 +- lib/src/lints/faster_groupby.rs | 72 +++++++++++++++++++++++++++++++++ lib/src/lints/legacy_let_syntax.rs | 4 +- lib/src/lints/manual_inherit.rs | 4 +- lib/src/lints/manual_inherit_from.rs | 4 +- lib/src/lints/redundant_pattern_bind.rs | 4 +- lib/src/lints/unquoted_splice.rs | 4 +- lib/src/lints/unquoted_uri.rs | 4 +- lib/src/lints/useless_parens.rs | 4 +- 15 files changed, 100 insertions(+), 28 deletions(-) create mode 100644 lib/src/lints/faster_groupby.rs (limited to 'lib/src/lints') diff --git a/lib/src/lints/bool_comparison.rs b/lib/src/lints/bool_comparison.rs index ed1e8bb..ef7f5d2 100644 --- a/lib/src/lints/bool_comparison.rs +++ b/lib/src/lints/bool_comparison.rs @@ -1,4 +1,4 @@ -use crate::{make, Metadata, Report, Rule, Suggestion}; +use crate::{make, session::SessionInfo, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -35,7 +35,7 @@ use rnix::{ struct BoolComparison; impl Rule for BoolComparison { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { if_chain! { if let NodeOrToken::Node(node) = node; if let Some(bin_expr) = BinOp::cast(node.clone()); diff --git a/lib/src/lints/collapsible_let_in.rs b/lib/src/lints/collapsible_let_in.rs index aa7e5a7..1c04d9c 100644 --- a/lib/src/lints/collapsible_let_in.rs +++ b/lib/src/lints/collapsible_let_in.rs @@ -1,4 +1,4 @@ -use crate::{make, Metadata, Report, Rule, Suggestion}; +use crate::{make, session::SessionInfo, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -45,7 +45,7 @@ use rowan::Direction; struct CollapsibleLetIn; impl Rule for CollapsibleLetIn { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { if_chain! { if let NodeOrToken::Node(node) = node; if let Some(let_in_expr) = LetIn::cast(node.clone()); diff --git a/lib/src/lints/deprecated_is_null.rs b/lib/src/lints/deprecated_is_null.rs index c814e87..9e7c293 100644 --- a/lib/src/lints/deprecated_is_null.rs +++ b/lib/src/lints/deprecated_is_null.rs @@ -1,4 +1,4 @@ -use crate::{make, Metadata, Report, Rule, Suggestion}; +use crate::{make, session::SessionInfo, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -35,7 +35,7 @@ use rnix::{ struct DeprecatedIsNull; impl Rule for DeprecatedIsNull { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { if_chain! { if let NodeOrToken::Node(node) = node; if let Some(apply) = Apply::cast(node.clone()); diff --git a/lib/src/lints/empty_inherit.rs b/lib/src/lints/empty_inherit.rs index 9ae4cf2..871c218 100644 --- a/lib/src/lints/empty_inherit.rs +++ b/lib/src/lints/empty_inherit.rs @@ -1,4 +1,4 @@ -use crate::{make, utils, Metadata, Report, Rule, Suggestion}; +use crate::{make, session::SessionInfo, utils, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -29,7 +29,7 @@ use rnix::{ struct EmptyInherit; impl Rule for EmptyInherit { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { if_chain! { if let NodeOrToken::Node(node) = node; if let Some(inherit_stmt) = Inherit::cast(node.clone()); diff --git a/lib/src/lints/empty_let_in.rs b/lib/src/lints/empty_let_in.rs index d33d0ae..e42f658 100644 --- a/lib/src/lints/empty_let_in.rs +++ b/lib/src/lints/empty_let_in.rs @@ -1,4 +1,4 @@ -use crate::{Metadata, Report, Rule, Suggestion}; +use crate::{session::SessionInfo, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -34,7 +34,7 @@ use rnix::{ struct EmptyLetIn; impl Rule for EmptyLetIn { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { if_chain! { if let NodeOrToken::Node(node) = node; if let Some(let_in_expr) = LetIn::cast(node.clone()); diff --git a/lib/src/lints/empty_pattern.rs b/lib/src/lints/empty_pattern.rs index f66a3b1..e03708b 100644 --- a/lib/src/lints/empty_pattern.rs +++ b/lib/src/lints/empty_pattern.rs @@ -1,4 +1,4 @@ -use crate::{make, Metadata, Report, Rule, Suggestion}; +use crate::{make, session::SessionInfo, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -43,7 +43,7 @@ use rnix::{ struct EmptyPattern; impl Rule for EmptyPattern { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { if_chain! { if let NodeOrToken::Node(node) = node; if let Some(pattern) = Pattern::cast(node.clone()); diff --git a/lib/src/lints/eta_reduction.rs b/lib/src/lints/eta_reduction.rs index 580f4a0..8e9d2a3 100644 --- a/lib/src/lints/eta_reduction.rs +++ b/lib/src/lints/eta_reduction.rs @@ -1,4 +1,4 @@ -use crate::{Metadata, Report, Rule, Suggestion}; +use crate::{session::SessionInfo, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -42,7 +42,7 @@ use rnix::{ struct EtaReduction; impl Rule for EtaReduction { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { if_chain! { if let NodeOrToken::Node(node) = node; if let Some(lambda_expr) = Lambda::cast(node.clone()); diff --git a/lib/src/lints/faster_groupby.rs b/lib/src/lints/faster_groupby.rs new file mode 100644 index 0000000..c496125 --- /dev/null +++ b/lib/src/lints/faster_groupby.rs @@ -0,0 +1,72 @@ +use crate::{ + make, + session::{SessionInfo, Version}, + Metadata, Report, Rule, Suggestion, +}; + +use if_chain::if_chain; +use macros::lint; +use rnix::{ + types::{Select, TypedNode}, + NodeOrToken, SyntaxElement, SyntaxKind, +}; + +/// ## What it does +/// Checks for `lib.groupBy`. +/// +/// ## Why is this bad? +/// Nix 2.5 introduces `builtins.groupBy` which is faster and does +/// not require a lib import. +/// +/// ## Example +/// +/// ```nix +/// lib.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 6 ]; +/// # { big = [ 3 4 5 6 ]; small = [ 1 2 ]; } +/// ``` +/// +/// Replace `lib.groupBy` with `builtins.groupBy`: +/// +/// ``` +/// builtins.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 6 ]; +/// ``` +#[lint( + name = "faster_groupby", + note = "Found lib.groupBy", + code = 15, + match_with = SyntaxKind::NODE_SELECT +)] +struct FasterGroupBy; + +impl Rule for FasterGroupBy { + fn validate(&self, node: &SyntaxElement, sess: &SessionInfo) -> Option { + let lint_version = "nix (Nix) 2.5".parse::().unwrap(); + if_chain! { + if sess.version() >= &lint_version; + if let NodeOrToken::Node(node) = node; + if let Some(select_expr) = Select::cast(node.clone()); + if let Some(select_from) = select_expr.set(); + if let Some(group_by_attr) = select_expr.index(); + + // a heuristic to lint on nixpkgs.lib.groupBy + // and lib.groupBy and its variants + if select_from.text().to_string() != "builtins"; + if group_by_attr.text().to_string() == "groupBy"; + + then { + let at = node.text_range(); + let replacement = { + let builtins = make::ident("builtins"); + make::select(builtins.node(), &group_by_attr).node().clone() + }; + let message = format!("Prefer `builtins.groupBy` over `{}.groupBy`", select_from); + Some( + self.report() + .suggest(at, message, Suggestion::new(at, replacement)), + ) + } else { + None + } + } + } +} diff --git a/lib/src/lints/legacy_let_syntax.rs b/lib/src/lints/legacy_let_syntax.rs index 5d0028b..e0b8980 100644 --- a/lib/src/lints/legacy_let_syntax.rs +++ b/lib/src/lints/legacy_let_syntax.rs @@ -1,4 +1,4 @@ -use crate::{make, Metadata, Report, Rule, Suggestion}; +use crate::{make, session::SessionInfo, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -44,7 +44,7 @@ use rnix::{ struct ManualInherit; impl Rule for ManualInherit { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { if_chain! { if let NodeOrToken::Node(node) = node; if let Some(legacy_let) = LegacyLet::cast(node.clone()); diff --git a/lib/src/lints/manual_inherit.rs b/lib/src/lints/manual_inherit.rs index 7717dc9..4fddce5 100644 --- a/lib/src/lints/manual_inherit.rs +++ b/lib/src/lints/manual_inherit.rs @@ -1,4 +1,4 @@ -use crate::{make, Metadata, Report, Rule, Suggestion}; +use crate::{make, session::SessionInfo, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -40,7 +40,7 @@ use rnix::{ struct ManualInherit; impl Rule for ManualInherit { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { if_chain! { if let NodeOrToken::Node(node) = node; if let Some(key_value_stmt) = KeyValue::cast(node.clone()); diff --git a/lib/src/lints/manual_inherit_from.rs b/lib/src/lints/manual_inherit_from.rs index 05d6bc8..a62a6c7 100644 --- a/lib/src/lints/manual_inherit_from.rs +++ b/lib/src/lints/manual_inherit_from.rs @@ -1,4 +1,4 @@ -use crate::{make, Metadata, Report, Rule, Suggestion}; +use crate::{make, session::SessionInfo, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -40,7 +40,7 @@ use rnix::{ struct ManualInherit; impl Rule for ManualInherit { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { if_chain! { if let NodeOrToken::Node(node) = node; if let Some(key_value_stmt) = KeyValue::cast(node.clone()); diff --git a/lib/src/lints/redundant_pattern_bind.rs b/lib/src/lints/redundant_pattern_bind.rs index 88ce4b0..56957ce 100644 --- a/lib/src/lints/redundant_pattern_bind.rs +++ b/lib/src/lints/redundant_pattern_bind.rs @@ -1,4 +1,4 @@ -use crate::{Metadata, Report, Rule, Suggestion}; +use crate::{session::SessionInfo, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -35,7 +35,7 @@ use rnix::{ struct RedundantPatternBind; impl Rule for RedundantPatternBind { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { if_chain! { if let NodeOrToken::Node(node) = node; if let Some(pattern) = Pattern::cast(node.clone()); diff --git a/lib/src/lints/unquoted_splice.rs b/lib/src/lints/unquoted_splice.rs index 7649fbc..ba1641a 100644 --- a/lib/src/lints/unquoted_splice.rs +++ b/lib/src/lints/unquoted_splice.rs @@ -1,4 +1,4 @@ -use crate::{make, Metadata, Report, Rule, Suggestion}; +use crate::{make, session::SessionInfo, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -40,7 +40,7 @@ use rnix::{ struct UnquotedSplice; impl Rule for UnquotedSplice { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { if_chain! { if let NodeOrToken::Node(node) = node; if Dynamic::cast(node.clone()).is_some(); diff --git a/lib/src/lints/unquoted_uri.rs b/lib/src/lints/unquoted_uri.rs index 8835338..440b278 100644 --- a/lib/src/lints/unquoted_uri.rs +++ b/lib/src/lints/unquoted_uri.rs @@ -1,4 +1,4 @@ -use crate::{make, Metadata, Report, Rule, Suggestion}; +use crate::{make, session::SessionInfo, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -46,7 +46,7 @@ use rnix::{types::TypedNode, NodeOrToken, SyntaxElement, SyntaxKind}; struct UnquotedUri; impl Rule for UnquotedUri { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { if_chain! { if let NodeOrToken::Token(token) = node; then { diff --git a/lib/src/lints/useless_parens.rs b/lib/src/lints/useless_parens.rs index 09d6f04..9cba4b3 100644 --- a/lib/src/lints/useless_parens.rs +++ b/lib/src/lints/useless_parens.rs @@ -1,4 +1,4 @@ -use crate::{Diagnostic, Metadata, Report, Rule, Suggestion}; +use crate::{session::SessionInfo, Diagnostic, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; use macros::lint; @@ -45,7 +45,7 @@ use rnix::{ struct UselessParens; impl Rule for UselessParens { - fn validate(&self, node: &SyntaxElement) -> Option { + fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option { if_chain! { if let NodeOrToken::Node(node) = node; if let Some(parsed_type_node) = ParsedType::cast(node.clone()); -- cgit v1.2.3