Lints and suggestions for the Nix programming language
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #120 from molybdenumsoftware/remove-faster-rules

feat: rm faster_groupby and faster_zipattrswith

authored by

Shahar "Dawn" Or and committed by
GitHub
7acc0e88 607f45a4

-224
-15
bin/tests/data/faster_groupby.nix
··· 1 - { 2 - # trivial case 3 - _ = lib.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 ]; 4 - 5 - # offer lint heuristically on this too 6 - _ = nixpkgs.lib.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 ]; 7 - 8 - # do not lint on `builtins` 9 - _ = builtins.groupBy (x: x.name) [ 10 - { name = "foo"; idx = 1; } 11 - { name = "foo"; idx = 2; } 12 - { name = "bar"; idx = 1; } 13 - { name = "bar"; idx = 2; } 14 - ]; 15 - }
-13
bin/tests/data/faster_zipattrswith.nix
··· 1 - { 2 - # trivial case 3 - _ = lib.zipAttrsWith (name: values: values) [{ a = 1; } { a = 2; b = 3; }]; 4 - 5 - # offer lint heuristically on this too 6 - _ = nixpkgs.lib.zipAttrsWith (name: values: values) [{ a = 1; } { a = 2; b = 3; }]; 7 - 8 - # do not lint on `builtins` 9 - _ = builtins.zipAttrsWith (name: values: values) [ 10 - { a = 1; } 11 - { a = 2; b = 3; } 12 - ]; 13 - }
-2
bin/tests/main.rs
··· 80 80 redundant_pattern_bind, 81 81 unquoted_uri, 82 82 empty_inherit, 83 - faster_groupby => session_info!("2.5"), 84 - faster_zipattrswith => session_info!("2.6"), 85 83 deprecated_to_path => session_info!("2.4"), 86 84 bool_simplification, 87 85 useless_has_attr,
-5
bin/tests/snapshots/main__faster_groupby_fix.snap
··· 1 - --- 2 - source: bin/tests/main.rs 3 - expression: "& stdout" 4 - --- 5 -
-18
bin/tests/snapshots/main__faster_groupby_lint.snap
··· 1 - --- 2 - source: bin/tests/main.rs 3 - expression: "& out" 4 - --- 5 - [W15] Warning: Found lib.groupBy 6 - ╭─[data/faster_groupby.nix:3:7] 7 - 8 - 3 │ _ = lib.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 ]; 9 - · ─────┬───── 10 - · ╰─────── Prefer builtins.groupBy over lib.groupBy 11 - ───╯ 12 - [W15] Warning: Found lib.groupBy 13 - ╭─[data/faster_groupby.nix:6:7] 14 - 15 - 6 │ _ = nixpkgs.lib.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 ]; 16 - · ─────────┬───────── 17 - · ╰─────────── Prefer builtins.groupBy over nixpkgs.lib.groupBy 18 - ───╯
-5
bin/tests/snapshots/main__faster_zipattrswith_fix.snap
··· 1 - --- 2 - source: bin/tests/main.rs 3 - expression: "& stdout" 4 - --- 5 -
-18
bin/tests/snapshots/main__faster_zipattrswith_lint.snap
··· 1 - --- 2 - source: bin/tests/main.rs 3 - expression: "& out" 4 - --- 5 - [W16] Warning: Found lib.zipAttrsWith 6 - ╭─[data/faster_zipattrswith.nix:3:7] 7 - 8 - 3 │ _ = lib.zipAttrsWith (name: values: values) [{ a = 1; } { a = 2; b = 3; }]; 9 - · ────────┬─────── 10 - · ╰───────── Prefer builtins.zipAttrsWith over lib.zipAttrsWith 11 - ───╯ 12 - [W16] Warning: Found lib.zipAttrsWith 13 - ╭─[data/faster_zipattrswith.nix:6:7] 14 - 15 - 6 │ _ = nixpkgs.lib.zipAttrsWith (name: values: values) [{ a = 1; } { a = 2; b = 3; }]; 16 - · ────────────┬─────────── 17 - · ╰───────────── Prefer builtins.zipAttrsWith over nixpkgs.lib.zipAttrsWith 18 - ───╯
-2
lib/src/lints.rs
··· 14 14 redundant_pattern_bind, 15 15 unquoted_uri, 16 16 empty_inherit, 17 - faster_groupby, 18 - faster_zipattrswith, 19 17 deprecated_to_path, 20 18 bool_simplification, 21 19 useless_has_attr,
-72
lib/src/lints/faster_groupby.rs
··· 1 - use crate::{ 2 - make, 3 - session::{SessionInfo, Version}, 4 - Metadata, Report, Rule, Suggestion, 5 - }; 6 - 7 - use if_chain::if_chain; 8 - use macros::lint; 9 - use rnix::{ 10 - types::{Select, TypedNode}, 11 - NodeOrToken, SyntaxElement, SyntaxKind, 12 - }; 13 - 14 - /// ## What it does 15 - /// Checks for `lib.groupBy`. 16 - /// 17 - /// ## Why is this bad? 18 - /// Nix 2.5 introduces `builtins.groupBy` which is faster and does 19 - /// not require a lib import. 20 - /// 21 - /// ## Example 22 - /// 23 - /// ```nix 24 - /// lib.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 6 ]; 25 - /// # { big = [ 3 4 5 6 ]; small = [ 1 2 ]; } 26 - /// ``` 27 - /// 28 - /// Replace `lib.groupBy` with `builtins.groupBy`: 29 - /// 30 - /// ```nix 31 - /// builtins.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 6 ]; 32 - /// ``` 33 - #[lint( 34 - name = "faster_groupby", 35 - note = "Found lib.groupBy", 36 - code = 15, 37 - match_with = SyntaxKind::NODE_SELECT 38 - )] 39 - struct FasterGroupBy; 40 - 41 - impl Rule for FasterGroupBy { 42 - fn validate(&self, node: &SyntaxElement, sess: &SessionInfo) -> Option<Report> { 43 - let lint_version = "2.5".parse::<Version>().unwrap(); 44 - if_chain! { 45 - if sess.version() >= &lint_version; 46 - if let NodeOrToken::Node(node) = node; 47 - if let Some(select_expr) = Select::cast(node.clone()); 48 - if let Some(select_from) = select_expr.set(); 49 - if let Some(group_by_attr) = select_expr.index(); 50 - 51 - // a heuristic to lint on nixpkgs.lib.groupBy 52 - // and lib.groupBy and its variants 53 - if select_from.text() != "builtins"; 54 - if group_by_attr.text() == "groupBy"; 55 - 56 - then { 57 - let at = node.text_range(); 58 - let replacement = { 59 - let builtins = make::ident("builtins"); 60 - make::select(builtins.node(), &group_by_attr).node().clone() 61 - }; 62 - let message = format!("Prefer `builtins.groupBy` over `{}.groupBy`", select_from); 63 - Some( 64 - self.report() 65 - .suggest(at, message, Suggestion::new(at, replacement)), 66 - ) 67 - } else { 68 - None 69 - } 70 - } 71 - } 72 - }
-72
lib/src/lints/faster_zipattrswith.rs
··· 1 - use crate::{ 2 - make, 3 - session::{SessionInfo, Version}, 4 - Metadata, Report, Rule, Suggestion, 5 - }; 6 - 7 - use if_chain::if_chain; 8 - use macros::lint; 9 - use rnix::{ 10 - types::{Select, TypedNode}, 11 - NodeOrToken, SyntaxElement, SyntaxKind, 12 - }; 13 - 14 - /// ## What it does 15 - /// Checks for `lib.zipAttrsWith`. 16 - /// 17 - /// ## Why is this bad? 18 - /// Nix 2.6 introduces `builtins.zipAttrsWith` which is faster and does 19 - /// not require a lib import. 20 - /// 21 - /// ## Example 22 - /// 23 - /// ```nix 24 - /// lib.zipAttrsWith (name: values: values) [ {a = "x";} {a = "y"; b = "z";} ] 25 - /// # { a = ["x" "y"]; b = ["z"] } 26 - /// ``` 27 - /// 28 - /// Replace `lib.zipAttrsWith` with `builtins.zipAttrsWith`: 29 - /// 30 - /// ```nix 31 - /// builtins.zipAttrsWith (name: values: values) [ {a = "x";} {a = "y"; b = "z";} ] 32 - /// ``` 33 - #[lint( 34 - name = "faster_zipattrswith", 35 - note = "Found lib.zipAttrsWith", 36 - code = 16, 37 - match_with = SyntaxKind::NODE_SELECT 38 - )] 39 - struct FasterZipAttrsWith; 40 - 41 - impl Rule for FasterZipAttrsWith { 42 - fn validate(&self, node: &SyntaxElement, sess: &SessionInfo) -> Option<Report> { 43 - let lint_version = "2.6".parse::<Version>().unwrap(); 44 - if_chain! { 45 - if sess.version() >= &lint_version; 46 - if let NodeOrToken::Node(node) = node; 47 - if let Some(select_expr) = Select::cast(node.clone()); 48 - if let Some(select_from) = select_expr.set(); 49 - if let Some(zip_attrs_with) = select_expr.index(); 50 - 51 - // a heuristic to lint on nixpkgs.lib.zipAttrsWith 52 - // and lib.zipAttrsWith and its variants 53 - if select_from.text() != "builtins"; 54 - if zip_attrs_with.text() == "zipAttrsWith"; 55 - 56 - then { 57 - let at = node.text_range(); 58 - let replacement = { 59 - let builtins = make::ident("builtins"); 60 - make::select(builtins.node(), &zip_attrs_with).node().clone() 61 - }; 62 - let message = format!("Prefer `builtins.zipAttrsWith` over `{}.zipAttrsWith`", select_from); 63 - Some( 64 - self.report() 65 - .suggest(at, message, Suggestion::new(at, replacement)), 66 - ) 67 - } else { 68 - None 69 - } 70 - } 71 - } 72 - }
-2
readme.md
··· 137 137 redundant_pattern_bind 138 138 unquoted_uri 139 139 empty_inherit 140 - faster_groupby 141 - faster_zipattrswith 142 140 deprecated_to_path 143 141 bool_simplification 144 142 useless_has_attr