Lints and suggestions for the Nix programming language
1
fork

Configure Feed

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

apply clippy suggestions (#73)

authored by

figsoda and committed by
GitHub
3c7136a2 ec55198a

+42 -46
+5 -8
bin/src/config.rs
··· 317 317 } 318 318 pub fn lints(&self) -> LintMap { 319 319 utils::lint_map_of( 320 - (&*LINTS) 320 + (*LINTS) 321 321 .iter() 322 322 .filter(|l| !self.disabled.iter().any(|check| check == l.name())) 323 323 .cloned() ··· 329 329 if let Some(v) = &self.nix_version { 330 330 v.parse::<Version>() 331 331 .map_err(|_| ConfigErr::ConfFileVersionParse(v.clone())) 332 - } else if let Some(v) = utils::get_version_info() 333 - .map(|o| o.parse::<Version>().ok()) 334 - .flatten() 335 - { 332 + } else if let Some(v) = utils::get_version_info().and_then(|o| o.parse::<Version>().ok()) { 336 333 Ok(v) 337 334 } else { 338 335 Ok(utils::default_nix_version().parse::<Version>().unwrap()) ··· 374 371 fn vfs(files: Vec<PathBuf>) -> Result<ReadOnlyVfs, ConfigErr> { 375 372 let mut vfs = ReadOnlyVfs::default(); 376 373 for file in files.iter() { 377 - if let Ok(data) = fs::read_to_string(&file) { 378 - let _id = vfs.alloc_file_id(&file); 379 - vfs.set_file_contents(&file, data.as_bytes()); 374 + if let Ok(data) = fs::read_to_string(file) { 375 + let _id = vfs.alloc_file_id(file); 376 + vfs.set_file_contents(file, data.as_bytes()); 380 377 } else { 381 378 println!("`{}` contains non-utf8 content", file.display()); 382 379 };
+1 -1
bin/src/fix/all.rs
··· 61 61 impl<'a> Iterator for FixResult<'a> { 62 62 type Item = FixResult<'a>; 63 63 fn next(&mut self) -> Option<Self::Item> { 64 - let all_reports = collect_fixes(&self.src, self.lints, &self.sess).ok()?; 64 + let all_reports = collect_fixes(&self.src, self.lints, self.sess).ok()?; 65 65 if all_reports.is_empty() { 66 66 return None; 67 67 }
+4 -4
bin/src/fix/single.rs
··· 49 49 .ok_or(SingleFixErr::NoOp) 50 50 } 51 51 52 - pub fn single<'a, 'b>( 52 + pub fn single<'a>( 53 53 line: usize, 54 54 col: usize, 55 55 src: &'a str, 56 - sess: &'b SessionInfo, 56 + sess: &SessionInfo, 57 57 ) -> Result<SingleFixResult<'a>, SingleFixErr> { 58 58 let mut src = Cow::from(src); 59 - let offset = pos_to_byte(line, col, &*src)?; 60 - let report = find(offset, &*src, &sess)?; 59 + let offset = pos_to_byte(line, col, &src)?; 60 + let report = find(offset, &src, sess)?; 61 61 62 62 report.apply(src.to_mut()); 63 63
+3 -3
bin/src/lint.rs
··· 36 36 } 37 37 38 38 pub fn lint(vfs_entry: VfsEntry, sess: &SessionInfo) -> LintResult { 39 - lint_with(vfs_entry, &utils::lint_map(), &sess) 39 + lint_with(vfs_entry, &utils::lint_map(), sess) 40 40 } 41 41 42 42 pub mod main { ··· 68 68 .filter(|lr| !lr.reports.is_empty()) 69 69 .collect::<Vec<_>>(); 70 70 71 - if results.len() != 0 { 71 + if !results.is_empty() { 72 72 for r in &results { 73 - stdout.write(&r, &vfs, check_config.format).unwrap(); 73 + stdout.write(r, &vfs, check_config.format).unwrap(); 74 74 } 75 75 std::process::exit(1); 76 76 }
+2 -2
bin/src/list.rs
··· 4 4 use lib::LINTS; 5 5 6 6 pub fn main() -> Result<(), StatixErr> { 7 - let mut lints = (&*LINTS).clone(); 8 - lints.as_mut_slice().sort_by(|a, b| a.code().cmp(&b.code())); 7 + let mut lints = (*LINTS).clone(); 8 + lints.as_mut_slice().sort_by_key(|a| a.code()); 9 9 for l in lints { 10 10 println!("W{:02} {}", l.code(), l.name()); 11 11 }
+1
bin/src/session.rs
··· 1 +
+1 -1
bin/src/utils.rs
··· 20 20 } 21 21 22 22 pub fn lint_map() -> HashMap<SyntaxKind, Vec<&'static Box<dyn Lint>>> { 23 - lint_map_of(&*LINTS) 23 + lint_map_of(&LINTS) 24 24 } 25 25 26 26 pub fn get_version_info() -> Option<String> {
+1 -2
lib/src/lints/bool_comparison.rs
··· 107 107 // not entirely accurate, underhanded nix programmers might write `true = false` 108 108 fn boolean_ident(node: &SyntaxNode) -> Option<NixBoolean> { 109 109 Ident::cast(node.clone()) 110 - .map(|ident_expr| match ident_expr.as_str() { 110 + .and_then(|ident_expr| match ident_expr.as_str() { 111 111 "true" => Some(NixBoolean::True), 112 112 "false" => Some(NixBoolean::False), 113 113 _ => None, 114 114 }) 115 - .flatten() 116 115 }
+1 -1
lib/src/lints/bool_simplification.rs
··· 38 38 if let Some(unary_expr) = UnaryOp::cast(node.clone()); 39 39 if unary_expr.operator() == UnaryOpKind::Invert; 40 40 if let Some(value_expr) = unary_expr.value(); 41 - if let Some(paren_expr) = Paren::cast(value_expr.clone()); 41 + if let Some(paren_expr) = Paren::cast(value_expr); 42 42 if let Some(inner_expr) = paren_expr.inner(); 43 43 if let Some(bin_expr) = BinOp::cast(inner_expr); 44 44 if let Some(BinOpKind::Equal) = bin_expr.operator();
+2 -3
lib/src/lints/empty_pattern.rs
··· 48 48 if let Some(arg) = lambda_expr.arg(); 49 49 if let Some(body) = lambda_expr.body(); 50 50 51 - if let Some(pattern) = Pattern::cast(arg.clone()); 51 + if let Some(pattern) = Pattern::cast(arg); 52 52 53 53 // no patterns within `{ }` 54 54 if pattern.entries().count() == 0; ··· 75 75 if let Some(attr_set) = AttrSet::cast(body.clone()); 76 76 if attr_set 77 77 .entries() 78 - .map(|e| e.key()) 79 - .flatten() 78 + .filter_map(|e| e.key()) 80 79 .any(|k| k.node().to_string() == "imports"); 81 80 then { 82 81 true
+2 -2
lib/src/lints/faster_groupby.rs
··· 50 50 51 51 // a heuristic to lint on nixpkgs.lib.groupBy 52 52 // and lib.groupBy and its variants 53 - if select_from.text().to_string() != "builtins"; 54 - if group_by_attr.text().to_string() == "groupBy"; 53 + if select_from.text() != "builtins"; 54 + if group_by_attr.text() == "groupBy"; 55 55 56 56 then { 57 57 let at = node.text_range();
+2 -2
lib/src/lints/faster_zipattrswith.rs
··· 50 50 51 51 // a heuristic to lint on nixpkgs.lib.zipAttrsWith 52 52 // and lib.zipAttrsWith and its variants 53 - if select_from.text().to_string() != "builtins"; 54 - if zip_attrs_with.text().to_string() == "zipAttrsWith"; 53 + if select_from.text() != "builtins"; 54 + if zip_attrs_with.text() == "zipAttrsWith"; 55 55 56 56 then { 57 57 let at = node.text_range();
+3 -3
lib/src/lints/repeated_keys.rs
··· 55 55 if components.next().is_some(); 56 56 57 57 if let Some(parent_node) = node.parent(); 58 - if let Some(parent_attr_set) = AttrSet::cast(parent_node.clone()); 58 + if let Some(parent_attr_set) = AttrSet::cast(parent_node); 59 59 60 60 if !parent_attr_set.recursive(); 61 61 let occurrences = parent_attr_set.entries().filter_map(|kv_scrutinee| { ··· 92 92 let third_message = { 93 93 let remaining_occurrences = iter.count(); 94 94 let mut message = match remaining_occurrences { 95 - 0 => format!("... and here."), 96 - 1 => format!("... and here (`1` occurrence omitted)."), 95 + 0 => "... and here.".to_string(), 96 + 1 => "... and here (`1` occurrence omitted).".to_string(), 97 97 n => format!("... and here (`{}` occurrences omitted).", n), 98 98 }; 99 99 message.push_str(&format!(" Try `{} = {{ {}=...; {}=...; {}=...; }}` instead.", first_component_ident.as_str(), first_subkey, second_subkey, third_subkey));
+2 -2
lib/src/lints/useless_has_attr.rs
··· 39 39 if let Some(if_else_expr) = IfElse::cast(node.clone()); 40 40 if let Some(condition_expr) = if_else_expr.condition(); 41 41 if let Some(default_expr) = if_else_expr.else_body(); 42 - if let Some(cond_bin_expr) = BinOp::cast(condition_expr.clone()); 42 + if let Some(cond_bin_expr) = BinOp::cast(condition_expr); 43 43 if let Some(BinOpKind::IsSet) = cond_bin_expr.operator(); 44 44 45 45 // set ? attr_path ··· 50 50 51 51 // check if body of the `if` expression is of the form `set.attr_path` 52 52 if let Some(body_expr) = if_else_expr.body(); 53 - if let Some(body_select_expr) = Select::cast(body_expr.clone()); 53 + if let Some(body_select_expr) = Select::cast(body_expr); 54 54 let expected_body = make::select(&set, &attr_path); 55 55 56 56 // text comparison will do for now
+3 -3
lib/src/make.rs
··· 7 7 8 8 fn ast_from_text<N: TypedNode>(text: &str) -> N { 9 9 let parse = rnix::parse(text); 10 - let node = match parse.node().descendants().find_map(N::cast) { 10 + 11 + match parse.node().descendants().find_map(N::cast) { 11 12 Some(it) => it, 12 13 None => { 13 14 panic!( ··· 16 17 text 17 18 ) 18 19 } 19 - }; 20 - node 20 + } 21 21 } 22 22 23 23 pub fn parenthesize(node: &SyntaxNode) -> types::Paren {
+2 -2
lib/src/session.rs
··· 22 22 23 23 fn parse_number(s: &str) -> Option<u16> { 24 24 s.chars() 25 - .take_while(|c| c.is_digit(10)) 25 + .take_while(|c| c.is_ascii_digit()) 26 26 .collect::<String>() 27 27 .parse::<u16>() 28 28 .ok() ··· 32 32 let mut parts = s.split('.'); 33 33 let major = parse_number(parts.next()?)?; 34 34 let minor = parse_number(parts.next()?)?; 35 - let patch = parts.next().map(|p| parse_number(p)).flatten(); 35 + let patch = parts.next().and_then(parse_number); 36 36 Some(Version { 37 37 major, 38 38 minor,
+6 -6
macros/src/metadata.rs
··· 83 83 84 84 fn generate_name_fn(&self) -> TokenStream2 { 85 85 let name_str = self.name; 86 - return quote! { 86 + quote! { 87 87 fn name(&self) -> &'static str { 88 88 #name_str 89 89 } 90 - }; 90 + } 91 91 } 92 92 93 93 fn generate_note_fn(&self) -> TokenStream2 { 94 94 let note_str = self.note; 95 - return quote! { 95 + quote! { 96 96 fn note(&self) -> &'static str { 97 97 #note_str 98 98 } 99 - }; 99 + } 100 100 } 101 101 102 102 fn generate_code_fn(&self) -> TokenStream2 { 103 103 let code_int = self.code; 104 - return quote! { 104 + quote! { 105 105 fn code(&self) -> u32 { 106 106 #code_int 107 107 } 108 - }; 108 + } 109 109 } 110 110 111 111 fn generate_match_with_fn(&self) -> TokenStream2 {
+1 -1
vfs/src/lib.rs
··· 65 65 self.data.insert(file_id, contents.to_owned()); 66 66 } 67 67 pub fn iter(&self) -> impl Iterator<Item = VfsEntry> { 68 - self.data.iter().map(move |(file_id, _)| VfsEntry { 68 + self.data.keys().map(move |file_id| VfsEntry { 69 69 file_id: *file_id, 70 70 file_path: self.file_path(*file_id), 71 71 contents: self.get_str(*file_id),