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/lib.rs | 4 +- lib/src/lints.rs | 1 + 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 +- lib/src/session.rs | 103 ++++++++++++++++++++++++++++++++ 18 files changed, 207 insertions(+), 29 deletions(-) create mode 100644 lib/src/lints/faster_groupby.rs create mode 100644 lib/src/session.rs (limited to 'lib/src') diff --git a/lib/src/lib.rs b/lib/src/lib.rs index a25b814..cc4818a 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,9 +1,11 @@ #![recursion_limit = "1024"] mod lints; mod make; +pub mod session; mod utils; pub use lints::LINTS; +use session::SessionInfo; use rnix::{parser::ParseError, SyntaxElement, SyntaxKind, TextRange}; use std::{convert::Into, default::Default}; @@ -221,7 +223,7 @@ impl Serialize for Suggestion { /// Lint logic is defined via this trait. Do not implement manually, /// look at the `lint` attribute macro instead for implementing rules pub trait Rule { - fn validate(&self, node: &SyntaxElement) -> Option; + fn validate(&self, node: &SyntaxElement, sess: &SessionInfo) -> Option; } /// Contains information about the lint itself. Do not implement manually, diff --git a/lib/src/lints.rs b/lib/src/lints.rs index 5de6b65..0add458 100644 --- a/lib/src/lints.rs +++ b/lib/src/lints.rs @@ -15,4 +15,5 @@ lints! { unquoted_uri, deprecated_is_null, empty_inherit, + faster_groupby, } 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()); diff --git a/lib/src/session.rs b/lib/src/session.rs new file mode 100644 index 0000000..8d142ec --- /dev/null +++ b/lib/src/session.rs @@ -0,0 +1,103 @@ +use std::{cmp::Ordering, str::FromStr}; + +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub struct Version { + major: u16, + minor: u16, + patch: Option, +} + +impl Ord for Version { + fn cmp(&self, other: &Self) -> Ordering { + let score = |v: &Version| v.major * 100 + v.minor * 10 + v.patch.unwrap_or(0); + score(self).cmp(&score(other)) + } +} + +impl PartialOrd for Version { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +fn parse_number(s: &str) -> Option { + s.chars() + .take_while(|c| c.is_digit(10)) + .collect::() + .parse::() + .ok() +} + +fn parse_version(s: &str) -> Option { + match s.split(' ').collect::>().as_slice() { + [_, _, version] => { + let mut parts = version.split('.'); + let major = parse_number(parts.next()?)?; + let minor = parse_number(parts.next()?)?; + let patch = parts.next().map(|p| parse_number(p)).flatten(); + Some(Version { + major, + minor, + patch, + }) + } + _ => None, + } +} + +impl FromStr for Version { + type Err = (); + fn from_str(s: &str) -> Result { + parse_version(s).ok_or(()) + } +} + +#[non_exhaustive] +pub struct SessionInfo { + nix_version: Version, +} + +impl SessionInfo { + pub fn from_version(nix_version: Version) -> Self { + Self { nix_version } + } + + pub fn version(&self) -> &Version { + &self.nix_version + } +} + +pub fn get_nix_version() -> Option { + "nix (Nix) 2.4pre20211006_53e4794".parse::().ok() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_trivial() { + let v = "nix (Nix) 1.6.1".parse::().ok(); + assert!(v.is_some()) + } + + #[test] + fn parse() { + let v = "nix (Nix) 2.4pre20211006_53e4794".parse::().ok(); + assert!(v.is_some()) + } + + #[test] + fn compare_trivial() { + let v1 = "nix (Nix) 1.6.1".parse::().ok(); + let v2 = "nix (Nix) 1.7.2".parse::().ok(); + assert!(v2 > v1); + } + + #[test] + fn compare() { + let v1 = "nix (Nix) 1.7".parse::().ok(); + let v2 = "nix (Nix) 2.4pre20211006_53e4794".parse::().ok(); + assert!(v2 >= v1); + } +} -- cgit v1.2.3