apply clippy suggestions (#73)

This commit is contained in:
figsoda 2023-02-20 05:24:02 -05:00 committed by GitHub
parent ec55198a39
commit 3c7136a23f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 42 additions and 46 deletions

View File

@ -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::<Version>()
.map_err(|_| ConfigErr::ConfFileVersionParse(v.clone()))
} else if let Some(v) = utils::get_version_info()
.map(|o| o.parse::<Version>().ok())
.flatten()
{
} else if let Some(v) = utils::get_version_info().and_then(|o| o.parse::<Version>().ok()) {
Ok(v)
} else {
Ok(utils::default_nix_version().parse::<Version>().unwrap())
@ -374,9 +371,9 @@ fn parse_warning_code(src: &str) -> Result<u32, ConfigErr> {
fn vfs(files: Vec<PathBuf>) -> Result<ReadOnlyVfs, ConfigErr> {
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());
};

View File

@ -61,7 +61,7 @@ fn reorder(mut reports: Vec<Report>) -> Vec<Report> {
impl<'a> Iterator for FixResult<'a> {
type Item = FixResult<'a>;
fn next(&mut self) -> Option<Self::Item> {
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;
}

View File

@ -49,15 +49,15 @@ fn find(offset: TextSize, src: &str, sess: &SessionInfo) -> Result<Report, Singl
.ok_or(SingleFixErr::NoOp)
}
pub fn single<'a, 'b>(
pub fn single<'a>(
line: usize,
col: usize,
src: &'a str,
sess: &'b SessionInfo,
sess: &SessionInfo,
) -> Result<SingleFixResult<'a>, 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());

View File

@ -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::<Vec<_>>();
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);
}

View File

@ -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());
}

View File

@ -0,0 +1 @@

View File

@ -20,7 +20,7 @@ pub fn lint_map_of(
}
pub fn lint_map() -> HashMap<SyntaxKind, Vec<&'static Box<dyn Lint>>> {
lint_map_of(&*LINTS)
lint_map_of(&LINTS)
}
pub fn get_version_info() -> Option<String> {

View File

@ -107,10 +107,9 @@ enum NixBoolean {
// not entirely accurate, underhanded nix programmers might write `true = false`
fn boolean_ident(node: &SyntaxNode) -> Option<NixBoolean> {
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()
}

View File

@ -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();

View File

@ -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

View File

@ -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();

View File

@ -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();

View File

@ -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));

View File

@ -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

View File

@ -7,7 +7,8 @@ use rnix::{
fn ast_from_text<N: TypedNode>(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<N: TypedNode>(text: &str) -> N {
text
)
}
};
node
}
}
pub fn parenthesize(node: &SyntaxNode) -> types::Paren {

View File

@ -22,7 +22,7 @@ impl PartialOrd for Version {
fn parse_number(s: &str) -> Option<u16> {
s.chars()
.take_while(|c| c.is_digit(10))
.take_while(|c| c.is_ascii_digit())
.collect::<String>()
.parse::<u16>()
.ok()
@ -32,7 +32,7 @@ fn parse_version(s: &str) -> Option<Version> {
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,

View File

@ -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 {

View File

@ -65,7 +65,7 @@ impl ReadOnlyVfs {
self.data.insert(file_id, contents.to_owned());
}
pub fn iter(&self) -> impl Iterator<Item = VfsEntry> {
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),