Lints and suggestions for the Nix programming language
1
fork

Configure Feed

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

do not raise empty_pattern on nixos modules

Akshay 8e4eeb97 479e3a27

+37 -7
+8
bin/tests/data/empty_pattern.nix
··· 5 5 # don't match 6 6 ({ a, ... }: a) 7 7 ({ ... } @ inputs: inputs) 8 + 9 + # nixos module, don't match 10 + ({ ... }: { 11 + imports = [ 12 + ./module.nix 13 + /path/to/absolute/module.nix 14 + ]; 15 + }) 8 16 ] 9 17
+29 -7
lib/src/lints/empty_pattern.rs
··· 3 3 use if_chain::if_chain; 4 4 use macros::lint; 5 5 use rnix::{ 6 - types::{Pattern, TypedNode}, 7 - NodeOrToken, SyntaxElement, SyntaxKind, 6 + types::{AttrSet, EntryHolder, Lambda, Pattern, TypedNode}, 7 + NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode, 8 8 }; 9 9 10 10 /// ## What it does ··· 20 20 /// 21 21 /// ```nix 22 22 /// client = { ... }: { 23 - /// imports = [ self.nixosModules.irmaseal-pkg ]; 24 23 /// services.irmaseal-pkg.enable = true; 25 24 /// }; 26 25 /// ``` ··· 30 29 /// 31 30 /// ```nix 32 31 /// client = _: { 33 - /// imports = [ self.nixosModules.irmaseal-pkg ]; 34 32 /// services.irmaseal-pkg.enable = true; 35 33 /// }; 36 34 /// ``` ··· 38 36 name = "empty_pattern", 39 37 note = "Found empty pattern in function argument", 40 38 code = 10, 41 - match_with = SyntaxKind::NODE_PATTERN 39 + match_with = SyntaxKind::NODE_LAMBDA 42 40 )] 43 41 struct EmptyPattern; 44 42 ··· 46 44 fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option<Report> { 47 45 if_chain! { 48 46 if let NodeOrToken::Node(node) = node; 49 - if let Some(pattern) = Pattern::cast(node.clone()); 47 + if let Some(lambda_expr) = Lambda::cast(node.clone()); 48 + if let Some(arg) = lambda_expr.arg(); 49 + if let Some(body) = lambda_expr.body(); 50 + 51 + if let Some(pattern) = Pattern::cast(arg.clone()); 50 52 // no patterns within `{ }` 51 53 if pattern.entries().count() == 0; 52 54 // pattern is not bound 53 55 if pattern.at().is_none(); 56 + 57 + // not a nixos module 58 + if !is_module(&body); 59 + 54 60 then { 55 - let at = node.text_range(); 61 + let at = pattern.node().text_range(); 56 62 let message = "This pattern is empty, use `_` instead"; 57 63 let replacement = make::ident("_").node().clone(); 58 64 Some(self.report().suggest(at, message, Suggestion::new(at, replacement))) ··· 62 68 } 63 69 } 64 70 } 71 + 72 + fn is_module(body: &SyntaxNode) -> bool { 73 + if_chain! { 74 + if let Some(attr_set) = AttrSet::cast(body.clone()); 75 + if attr_set 76 + .entries() 77 + .map(|e| e.key()) 78 + .flatten() 79 + .any(|k| k.node().to_string() == "imports"); 80 + then { 81 + true 82 + } else { 83 + false 84 + } 85 + } 86 + }