Lints and suggestions for the Nix programming language
1
fork

Configure Feed

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

refactor parsing code, simplify tests

Akshay 7d732a05 d1ff222b

+22 -29
+3 -1
bin/src/utils.rs
··· 30 30 .output() 31 31 .expect("failed to execute"); 32 32 std::str::from_utf8(&program.stdout) 33 - .ok() 33 + .ok()? 34 + .split(' ') 35 + .nth(2) 34 36 .map(ToOwned::to_owned) 35 37 }
+2 -2
bin/tests/main.rs
··· 19 19 test_lint!($($tail)*); 20 20 }; 21 21 ($tname:ident) => { 22 - test_lint!($tname => session_info!("nix (Nix) 2.5")); 22 + test_lint!($tname => session_info!("2.5")); 23 23 }; 24 24 ($tname:ident => $sess:expr) => { 25 25 #[test] ··· 61 61 unquoted_uri, 62 62 deprecated_is_null, 63 63 empty_inherit, 64 - faster_groupby => session_info!("nix (Nix) 2.5") 64 + faster_groupby => session_info!("2.5") 65 65 }
+2 -2
lib/src/lints/faster_groupby.rs
··· 27 27 /// 28 28 /// Replace `lib.groupBy` with `builtins.groupBy`: 29 29 /// 30 - /// ``` 30 + /// ```nix 31 31 /// builtins.groupBy (x: if x > 2 then "big" else "small") [ 1 2 3 4 5 6 ]; 32 32 /// ``` 33 33 #[lint( ··· 40 40 41 41 impl Rule for FasterGroupBy { 42 42 fn validate(&self, node: &SyntaxElement, sess: &SessionInfo) -> Option<Report> { 43 - let lint_version = "nix (Nix) 2.5".parse::<Version>().unwrap(); 43 + let lint_version = "2.5".parse::<Version>().unwrap(); 44 44 if_chain! { 45 45 if sess.version() >= &lint_version; 46 46 if let NodeOrToken::Node(node) = node;
+15 -24
lib/src/session.rs
··· 29 29 } 30 30 31 31 fn parse_version(s: &str) -> Option<Version> { 32 - match s.split(' ').collect::<Vec<_>>().as_slice() { 33 - [_, _, version] => { 34 - let mut parts = version.split('.'); 35 - let major = parse_number(parts.next()?)?; 36 - let minor = parse_number(parts.next()?)?; 37 - let patch = parts.next().map(|p| parse_number(p)).flatten(); 38 - Some(Version { 39 - major, 40 - minor, 41 - patch, 42 - }) 43 - } 44 - _ => None, 45 - } 32 + let mut parts = s.split('.'); 33 + let major = parse_number(parts.next()?)?; 34 + let minor = parse_number(parts.next()?)?; 35 + let patch = parts.next().map(|p| parse_number(p)).flatten(); 36 + Some(Version { 37 + major, 38 + minor, 39 + patch, 40 + }) 46 41 } 47 42 48 43 impl FromStr for Version { ··· 67 62 } 68 63 } 69 64 70 - pub fn get_nix_version() -> Option<Version> { 71 - "nix (Nix) 2.4pre20211006_53e4794".parse::<Version>().ok() 72 - } 73 - 74 65 #[cfg(test)] 75 66 mod tests { 76 67 use super::*; 77 68 78 69 #[test] 79 70 fn parse_trivial() { 80 - let v = "nix (Nix) 1.6.1".parse::<Version>().ok(); 71 + let v = "1.6.1".parse::<Version>().ok(); 81 72 assert!(v.is_some()) 82 73 } 83 74 84 75 #[test] 85 76 fn parse() { 86 - let v = "nix (Nix) 2.4pre20211006_53e4794".parse::<Version>().ok(); 77 + let v = "2.4pre20211006_53e4794".parse::<Version>().ok(); 87 78 assert!(v.is_some()) 88 79 } 89 80 90 81 #[test] 91 82 fn compare_trivial() { 92 - let v1 = "nix (Nix) 1.6.1".parse::<Version>().ok(); 93 - let v2 = "nix (Nix) 1.7.2".parse::<Version>().ok(); 83 + let v1 = "1.6.1".parse::<Version>().ok(); 84 + let v2 = "1.7.2".parse::<Version>().ok(); 94 85 assert!(v2 > v1); 95 86 } 96 87 97 88 #[test] 98 89 fn compare() { 99 - let v1 = "nix (Nix) 1.7".parse::<Version>().ok(); 100 - let v2 = "nix (Nix) 2.4pre20211006_53e4794".parse::<Version>().ok(); 90 + let v1 = "1.7".parse::<Version>().ok(); 91 + let v2 = "2.4pre20211006_53e4794".parse::<Version>().ok(); 101 92 assert!(v2 >= v1); 102 93 } 103 94 }