diff --git a/bin/src/config.rs b/bin/src/config.rs index a03c047..d235857 100644 --- a/bin/src/config.rs +++ b/bin/src/config.rs @@ -317,7 +317,7 @@ impl ConfFile { } pub fn lints(&self) -> LintMap { utils::lint_map_of( - (&*LINTS) + (*LINTS) .iter() .filter(|l| !self.disabled.iter().any(|check| check == l.name())) .cloned() @@ -329,10 +329,7 @@ impl ConfFile { if let Some(v) = &self.nix_version { v.parse::() .map_err(|_| ConfigErr::ConfFileVersionParse(v.clone())) - } else if let Some(v) = utils::get_version_info() - .map(|o| o.parse::().ok()) - .flatten() - { + } else if let Some(v) = utils::get_version_info().and_then(|o| o.parse::().ok()) { Ok(v) } else { Ok(utils::default_nix_version().parse::().unwrap()) @@ -374,9 +371,9 @@ fn parse_warning_code(src: &str) -> Result { fn vfs(files: Vec) -> Result { let mut vfs = ReadOnlyVfs::default(); for file in files.iter() { - if let Ok(data) = fs::read_to_string(&file) { - let _id = vfs.alloc_file_id(&file); - vfs.set_file_contents(&file, data.as_bytes()); + if let Ok(data) = fs::read_to_string(file) { + let _id = vfs.alloc_file_id(file); + vfs.set_file_contents(file, data.as_bytes()); } else { println!("`{}` contains non-utf8 content", file.display()); }; diff --git a/bin/src/fix/all.rs b/bin/src/fix/all.rs index d7f7fff..3ddd6aa 100644 --- a/bin/src/fix/all.rs +++ b/bin/src/fix/all.rs @@ -61,7 +61,7 @@ fn reorder(mut reports: Vec) -> Vec { impl<'a> Iterator for FixResult<'a> { type Item = FixResult<'a>; fn next(&mut self) -> Option { - let all_reports = collect_fixes(&self.src, self.lints, &self.sess).ok()?; + let all_reports = collect_fixes(&self.src, self.lints, self.sess).ok()?; if all_reports.is_empty() { return None; } diff --git a/bin/src/fix/single.rs b/bin/src/fix/single.rs index 67b6b8f..e162a94 100644 --- a/bin/src/fix/single.rs +++ b/bin/src/fix/single.rs @@ -49,15 +49,15 @@ fn find(offset: TextSize, src: &str, sess: &SessionInfo) -> Result( +pub fn single<'a>( line: usize, col: usize, src: &'a str, - sess: &'b SessionInfo, + sess: &SessionInfo, ) -> Result, SingleFixErr> { let mut src = Cow::from(src); - let offset = pos_to_byte(line, col, &*src)?; - let report = find(offset, &*src, &sess)?; + let offset = pos_to_byte(line, col, &src)?; + let report = find(offset, &src, sess)?; report.apply(src.to_mut()); diff --git a/bin/src/lint.rs b/bin/src/lint.rs index b7529ba..cfa9e5d 100644 --- a/bin/src/lint.rs +++ b/bin/src/lint.rs @@ -36,7 +36,7 @@ pub fn lint_with(vfs_entry: VfsEntry, lints: &LintMap, sess: &SessionInfo) -> Li } pub fn lint(vfs_entry: VfsEntry, sess: &SessionInfo) -> LintResult { - lint_with(vfs_entry, &utils::lint_map(), &sess) + lint_with(vfs_entry, &utils::lint_map(), sess) } pub mod main { @@ -68,9 +68,9 @@ pub mod main { .filter(|lr| !lr.reports.is_empty()) .collect::>(); - if results.len() != 0 { + if !results.is_empty() { for r in &results { - stdout.write(&r, &vfs, check_config.format).unwrap(); + stdout.write(r, &vfs, check_config.format).unwrap(); } std::process::exit(1); } diff --git a/bin/src/list.rs b/bin/src/list.rs index 4fe3039..c8d422b 100644 --- a/bin/src/list.rs +++ b/bin/src/list.rs @@ -4,8 +4,8 @@ pub mod main { use lib::LINTS; pub fn main() -> Result<(), StatixErr> { - let mut lints = (&*LINTS).clone(); - lints.as_mut_slice().sort_by(|a, b| a.code().cmp(&b.code())); + let mut lints = (*LINTS).clone(); + lints.as_mut_slice().sort_by_key(|a| a.code()); for l in lints { println!("W{:02} {}", l.code(), l.name()); } diff --git a/bin/src/session.rs b/bin/src/session.rs index e69de29..8b13789 100644 --- a/bin/src/session.rs +++ b/bin/src/session.rs @@ -0,0 +1 @@ + diff --git a/bin/src/utils.rs b/bin/src/utils.rs index d3bff54..490d098 100644 --- a/bin/src/utils.rs +++ b/bin/src/utils.rs @@ -20,7 +20,7 @@ pub fn lint_map_of( } pub fn lint_map() -> HashMap>> { - lint_map_of(&*LINTS) + lint_map_of(&LINTS) } pub fn get_version_info() -> Option { diff --git a/lib/src/lints/bool_comparison.rs b/lib/src/lints/bool_comparison.rs index 5402f31..fd2a410 100644 --- a/lib/src/lints/bool_comparison.rs +++ b/lib/src/lints/bool_comparison.rs @@ -107,10 +107,9 @@ enum NixBoolean { // not entirely accurate, underhanded nix programmers might write `true = false` fn boolean_ident(node: &SyntaxNode) -> Option { Ident::cast(node.clone()) - .map(|ident_expr| match ident_expr.as_str() { + .and_then(|ident_expr| match ident_expr.as_str() { "true" => Some(NixBoolean::True), "false" => Some(NixBoolean::False), _ => None, }) - .flatten() } diff --git a/lib/src/lints/bool_simplification.rs b/lib/src/lints/bool_simplification.rs index 9ccb1f6..a5582ab 100644 --- a/lib/src/lints/bool_simplification.rs +++ b/lib/src/lints/bool_simplification.rs @@ -38,7 +38,7 @@ impl Rule for BoolSimplification { if let Some(unary_expr) = UnaryOp::cast(node.clone()); if unary_expr.operator() == UnaryOpKind::Invert; if let Some(value_expr) = unary_expr.value(); - if let Some(paren_expr) = Paren::cast(value_expr.clone()); + if let Some(paren_expr) = Paren::cast(value_expr); if let Some(inner_expr) = paren_expr.inner(); if let Some(bin_expr) = BinOp::cast(inner_expr); if let Some(BinOpKind::Equal) = bin_expr.operator(); diff --git a/lib/src/lints/empty_pattern.rs b/lib/src/lints/empty_pattern.rs index 14c3b5e..163528b 100644 --- a/lib/src/lints/empty_pattern.rs +++ b/lib/src/lints/empty_pattern.rs @@ -48,7 +48,7 @@ impl Rule for EmptyPattern { if let Some(arg) = lambda_expr.arg(); if let Some(body) = lambda_expr.body(); - if let Some(pattern) = Pattern::cast(arg.clone()); + if let Some(pattern) = Pattern::cast(arg); // no patterns within `{ }` if pattern.entries().count() == 0; @@ -75,8 +75,7 @@ fn is_module(body: &SyntaxNode) -> bool { if let Some(attr_set) = AttrSet::cast(body.clone()); if attr_set .entries() - .map(|e| e.key()) - .flatten() + .filter_map(|e| e.key()) .any(|k| k.node().to_string() == "imports"); then { true diff --git a/lib/src/lints/faster_groupby.rs b/lib/src/lints/faster_groupby.rs index 2a36f5f..619af3d 100644 --- a/lib/src/lints/faster_groupby.rs +++ b/lib/src/lints/faster_groupby.rs @@ -50,8 +50,8 @@ impl Rule for FasterGroupBy { // 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"; + if select_from.text() != "builtins"; + if group_by_attr.text() == "groupBy"; then { let at = node.text_range(); diff --git a/lib/src/lints/faster_zipattrswith.rs b/lib/src/lints/faster_zipattrswith.rs index 139d499..23faf5e 100644 --- a/lib/src/lints/faster_zipattrswith.rs +++ b/lib/src/lints/faster_zipattrswith.rs @@ -50,8 +50,8 @@ impl Rule for FasterZipAttrsWith { // a heuristic to lint on nixpkgs.lib.zipAttrsWith // and lib.zipAttrsWith and its variants - if select_from.text().to_string() != "builtins"; - if zip_attrs_with.text().to_string() == "zipAttrsWith"; + if select_from.text() != "builtins"; + if zip_attrs_with.text() == "zipAttrsWith"; then { let at = node.text_range(); diff --git a/lib/src/lints/repeated_keys.rs b/lib/src/lints/repeated_keys.rs index 71b015a..6b9fef0 100644 --- a/lib/src/lints/repeated_keys.rs +++ b/lib/src/lints/repeated_keys.rs @@ -55,7 +55,7 @@ impl Rule for RepeatedKeys { if components.next().is_some(); if let Some(parent_node) = node.parent(); - if let Some(parent_attr_set) = AttrSet::cast(parent_node.clone()); + if let Some(parent_attr_set) = AttrSet::cast(parent_node); if !parent_attr_set.recursive(); let occurrences = parent_attr_set.entries().filter_map(|kv_scrutinee| { @@ -92,8 +92,8 @@ impl Rule for RepeatedKeys { let third_message = { let remaining_occurrences = iter.count(); let mut message = match remaining_occurrences { - 0 => format!("... and here."), - 1 => format!("... and here (`1` occurrence omitted)."), + 0 => "... and here.".to_string(), + 1 => "... and here (`1` occurrence omitted).".to_string(), n => format!("... and here (`{}` occurrences omitted).", n), }; message.push_str(&format!(" Try `{} = {{ {}=...; {}=...; {}=...; }}` instead.", first_component_ident.as_str(), first_subkey, second_subkey, third_subkey)); diff --git a/lib/src/lints/useless_has_attr.rs b/lib/src/lints/useless_has_attr.rs index 2dbdb5b..22c5fc8 100644 --- a/lib/src/lints/useless_has_attr.rs +++ b/lib/src/lints/useless_has_attr.rs @@ -39,7 +39,7 @@ impl Rule for UselessHasAttr { if let Some(if_else_expr) = IfElse::cast(node.clone()); if let Some(condition_expr) = if_else_expr.condition(); if let Some(default_expr) = if_else_expr.else_body(); - if let Some(cond_bin_expr) = BinOp::cast(condition_expr.clone()); + if let Some(cond_bin_expr) = BinOp::cast(condition_expr); if let Some(BinOpKind::IsSet) = cond_bin_expr.operator(); // set ? attr_path @@ -50,7 +50,7 @@ impl Rule for UselessHasAttr { // check if body of the `if` expression is of the form `set.attr_path` if let Some(body_expr) = if_else_expr.body(); - if let Some(body_select_expr) = Select::cast(body_expr.clone()); + if let Some(body_select_expr) = Select::cast(body_expr); let expected_body = make::select(&set, &attr_path); // text comparison will do for now diff --git a/lib/src/make.rs b/lib/src/make.rs index 2c33ac8..e58a764 100644 --- a/lib/src/make.rs +++ b/lib/src/make.rs @@ -7,7 +7,8 @@ use rnix::{ fn ast_from_text(text: &str) -> N { let parse = rnix::parse(text); - let node = match parse.node().descendants().find_map(N::cast) { + + match parse.node().descendants().find_map(N::cast) { Some(it) => it, None => { panic!( @@ -16,8 +17,7 @@ fn ast_from_text(text: &str) -> N { text ) } - }; - node + } } pub fn parenthesize(node: &SyntaxNode) -> types::Paren { diff --git a/lib/src/session.rs b/lib/src/session.rs index 0fb8ae2..f5a7bae 100644 --- a/lib/src/session.rs +++ b/lib/src/session.rs @@ -22,7 +22,7 @@ impl PartialOrd for Version { fn parse_number(s: &str) -> Option { s.chars() - .take_while(|c| c.is_digit(10)) + .take_while(|c| c.is_ascii_digit()) .collect::() .parse::() .ok() @@ -32,7 +32,7 @@ fn parse_version(s: &str) -> Option { let mut parts = s.split('.'); let major = parse_number(parts.next()?)?; let minor = parse_number(parts.next()?)?; - let patch = parts.next().map(|p| parse_number(p)).flatten(); + let patch = parts.next().and_then(parse_number); Some(Version { major, minor, diff --git a/macros/src/metadata.rs b/macros/src/metadata.rs index 98e2a72..72739af 100644 --- a/macros/src/metadata.rs +++ b/macros/src/metadata.rs @@ -83,29 +83,29 @@ impl<'μ> LintMeta<'μ> { fn generate_name_fn(&self) -> TokenStream2 { let name_str = self.name; - return quote! { + quote! { fn name(&self) -> &'static str { #name_str } - }; + } } fn generate_note_fn(&self) -> TokenStream2 { let note_str = self.note; - return quote! { + quote! { fn note(&self) -> &'static str { #note_str } - }; + } } fn generate_code_fn(&self) -> TokenStream2 { let code_int = self.code; - return quote! { + quote! { fn code(&self) -> u32 { #code_int } - }; + } } fn generate_match_with_fn(&self) -> TokenStream2 { diff --git a/vfs/src/lib.rs b/vfs/src/lib.rs index 2d7577d..b0a0929 100644 --- a/vfs/src/lib.rs +++ b/vfs/src/lib.rs @@ -65,7 +65,7 @@ impl ReadOnlyVfs { self.data.insert(file_id, contents.to_owned()); } pub fn iter(&self) -> impl Iterator { - self.data.iter().map(move |(file_id, _)| VfsEntry { + self.data.keys().map(move |file_id| VfsEntry { file_id: *file_id, file_path: self.file_path(*file_id), contents: self.get_str(*file_id),