From 5e749d62f908e9ecf45063e70b71e9242751b507 Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Tue, 14 Sep 2021 14:08:48 +0200 Subject: [PATCH] manually clean code with clippy --- src/agg/mod.rs | 2 +- src/commands/buildindex.rs | 1 + src/commands/joinkmers.rs | 1 + src/commands/printindex.rs | 1 + src/commands/prot2kmer2lca.rs | 2 +- src/commands/splitkmers.rs | 8 +++++--- src/commands/taxa2agg.rs | 6 +++--- src/commands/taxa2freq.rs | 13 +++++-------- src/commands/taxonomy.rs | 1 + src/commands/uniq.rs | 9 ++++++--- src/dna/mod.rs | 18 +++++++++--------- src/dna/translation.rs | 2 +- src/io/fasta.rs | 4 ++-- src/rmq/lca.rs | 2 +- 14 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/agg/mod.rs b/src/agg/mod.rs index 4a4b95f..d65578f 100644 --- a/src/agg/mod.rs +++ b/src/agg/mod.rs @@ -14,7 +14,7 @@ pub trait Aggregator { fn aggregate(&self, taxons: &HashMap) -> Result; /// Aggregates a list of taxons into a resulting taxon id. - fn counting_aggregate(&self, taxons: &Vec) -> Result { + fn counting_aggregate(&self, taxons: &[TaxonId]) -> Result { let taxons = taxons.iter().map(|&t| (t, 1.0)); self.aggregate(&count(taxons)) } diff --git a/src/commands/buildindex.rs b/src/commands/buildindex.rs index a8c38f2..7806441 100644 --- a/src/commands/buildindex.rs +++ b/src/commands/buildindex.rs @@ -6,6 +6,7 @@ use crate::errors; #[derive(Debug, StructOpt)] #[structopt(verbatim_doc_comment)] +#[allow(clippy::tabs_in_doc_comments)] /// Builds an index mapping short strings to taxon IDs /// /// The `umgap buildindex` command takes tab-separated strings and taxon IDs, and creates a diff --git a/src/commands/joinkmers.rs b/src/commands/joinkmers.rs index c0603fd..82e5db3 100644 --- a/src/commands/joinkmers.rs +++ b/src/commands/joinkmers.rs @@ -13,6 +13,7 @@ use crate::tree; #[derive(Debug, StructOpt)] #[structopt(verbatim_doc_comment)] +#[allow(clippy::tabs_in_doc_comments)] /// Aggregates a TSV stream of peptides and taxon IDs /// /// The `umgap joinkmers` command takes tab-separated peptides and taxon IDs, aggregates the diff --git a/src/commands/printindex.rs b/src/commands/printindex.rs index 407a955..1208ebd 100644 --- a/src/commands/printindex.rs +++ b/src/commands/printindex.rs @@ -9,6 +9,7 @@ use crate::errors; #[derive(Debug, StructOpt)] #[structopt(verbatim_doc_comment)] +#[allow(clippy::tabs_in_doc_comments)] /// Prints the key/value pairs in an index /// /// Outputs the string keys and taxon ID values in TSV format, mostly for debugging purposes. diff --git a/src/commands/prot2kmer2lca.rs b/src/commands/prot2kmer2lca.rs index cf61222..734cd71 100644 --- a/src/commands/prot2kmer2lca.rs +++ b/src/commands/prot2kmer2lca.rs @@ -186,7 +186,7 @@ where output_mutex .lock() .unwrap() - .write(chunk_output.as_bytes())?; + .write_all(chunk_output.as_bytes())?; Ok(()) }) .collect() diff --git a/src/commands/splitkmers.rs b/src/commands/splitkmers.rs index dd831e4..7bc3280 100644 --- a/src/commands/splitkmers.rs +++ b/src/commands/splitkmers.rs @@ -68,10 +68,12 @@ pub fn splitkmers(args: SplitKmers) -> errors::Result<()> { continue; } for kmer in sequence.as_bytes().windows(args.length) { - if byte.is_none() { + if let Some(&b) = byte { + if b == kmer[0] { + writer.serialize((String::from_utf8_lossy(&kmer[1..]), tid))?; + } + } else { writer.serialize((String::from_utf8_lossy(kmer), tid))?; - } else if *byte.unwrap() == kmer[0] { - writer.serialize((String::from_utf8_lossy(&kmer[1..]), tid))?; } } } diff --git a/src/commands/taxa2agg.rs b/src/commands/taxa2agg.rs index efa42bf..57c1118 100644 --- a/src/commands/taxa2agg.rs +++ b/src/commands/taxa2agg.rs @@ -139,7 +139,7 @@ pub fn taxa2agg(args: TaxaToAgg) -> errors::Result<()> { }; let aggregator = aggregator?; - fn with_score(pair: &String) -> errors::Result<(TaxonId, f32)> { + fn with_score(pair: &str) -> errors::Result<(TaxonId, f32)> { let split = pair.split('=').collect::>(); if split.len() != 2 { return Err("Taxon without score".into()); @@ -147,7 +147,7 @@ pub fn taxa2agg(args: TaxaToAgg) -> errors::Result<()> { Ok((split[0].parse::()?, split[1].parse::()?)) } - fn not_scored(tid: &String) -> errors::Result<(TaxonId, f32)> { + fn not_scored(tid: &str) -> errors::Result<(TaxonId, f32)> { Ok((tid.parse::()?, 1.0)) } @@ -162,7 +162,7 @@ pub fn taxa2agg(args: TaxaToAgg) -> errors::Result<()> { let taxons = record .sequence .iter() - .map(parser) + .map(|s| parser(s)) .collect::>>()?; // Create a frequency table of taxons for this read (taking into account the lower bound) diff --git a/src/commands/taxa2freq.rs b/src/commands/taxa2freq.rs index 5992c03..8523416 100644 --- a/src/commands/taxa2freq.rs +++ b/src/commands/taxa2freq.rs @@ -152,20 +152,17 @@ pub fn taxa2freq(args: TaxaToFreq) -> errors::Result<()> { } fn count_file( - snapping: &Vec>, + snapping: &[Option], counts: &mut HashMap>, index: usize, numfiles: usize, file: T, ) -> errors::Result<()> { for line in file.lines() { - match line?.parse::() { - Ok(taxon) => { - counts - .entry(snapping[taxon].unwrap_or(0)) - .or_insert(vec![0; numfiles])[index] += 1 - } - Err(_) => (), + if let Ok(taxon) = line?.parse::() { + counts + .entry(snapping[taxon].unwrap_or(0)) + .or_insert_with(|| vec![0; numfiles])[index] += 1 } } Ok(()) diff --git a/src/commands/taxonomy.rs b/src/commands/taxonomy.rs index 816f871..1140c42 100644 --- a/src/commands/taxonomy.rs +++ b/src/commands/taxonomy.rs @@ -12,6 +12,7 @@ use crate::taxon; #[rustfmt::skip] #[derive(Debug, StructOpt)] #[structopt(verbatim_doc_comment)] +#[allow(clippy::tabs_in_doc_comments)] /// Includes info in a stream of taxon IDs /// /// The `umgap taxonomy` command takes one or more taxon IDs as input, searches for them in a diff --git a/src/commands/uniq.rs b/src/commands/uniq.rs index 5d48c25..1c35b6e 100644 --- a/src/commands/uniq.rs +++ b/src/commands/uniq.rs @@ -59,9 +59,12 @@ pub fn uniq(args: Uniq) -> errors::Result<()> { for record in fasta::Reader::new(io::stdin(), false).records() { let mut record = record?; if let Some(ref delimiter) = args.delimiter { - record - .header - .truncate(record.header.find(delimiter).unwrap_or(record.header.len())); + record.header.truncate( + record + .header + .find(delimiter) + .unwrap_or_else(|| record.header.len()), + ); } if let Some(ref mut rec) = last { if rec.header == record.header { diff --git a/src/dna/mod.rs b/src/dna/mod.rs index 62d3134..c68cc4f 100644 --- a/src/dna/mod.rs +++ b/src/dna/mod.rs @@ -22,11 +22,11 @@ impl Nucleotide { /// Complement of the given nucleotide. pub fn complement(&self) -> Self { match self { - &A => T, - &C => G, - &G => C, - &T => A, - &N => N, + A => T, + C => G, + G => C, + T => A, + N => N, } } } @@ -43,9 +43,9 @@ impl From for Nucleotide { } } -impl Into for Nucleotide { - fn into(self) -> u8 { - match self { +impl From for u8 { + fn from(n: Nucleotide) -> u8 { + match n { A => b'A', C => b'C', G => b'G', @@ -89,7 +89,7 @@ impl<'a> From<&'a Vec> for Strand { impl Strand { /// A reading frame of this strand, 1-indexed. - pub fn frame<'a>(&'a self, start: usize) -> Frame<'a> { + pub fn frame(&self, start: usize) -> Frame<'_> { Frame(if self.0.len() > start - 1 { &self.0[start - 1..] } else { diff --git a/src/dna/translation.rs b/src/dna/translation.rs index 8c7b822..2223692 100644 --- a/src/dna/translation.rs +++ b/src/dna/translation.rs @@ -180,7 +180,7 @@ impl str::FromStr for &'static TranslationTable { let id = s.parse::()?; TABLES[id - 1] .as_ref() - .ok_or(ErrorKind::UnknownTable(id).into()) + .ok_or_else(|| ErrorKind::UnknownTable(id).into()) } } diff --git a/src/io/fasta.rs b/src/io/fasta.rs index 52e0d12..a97366b 100644 --- a/src/io/fasta.rs +++ b/src/io/fasta.rs @@ -31,7 +31,7 @@ impl Reader { let lines = BufReader::with_capacity(BUFFER_SIZE, reader) .lines() .peekable(); - Reader { unwrap, lines } + Reader { lines, unwrap } } /// Reads the next record from the FASTA file. @@ -166,7 +166,7 @@ impl<'a, W: Write> Writer<'a, W> { let sequence = record.sequence.join(self.separator); if !self.wrap { self.buffer.write_all(&[b'\n'])?; - self.buffer.write(sequence.as_bytes())?; + self.buffer.write_all(sequence.as_bytes())?; } else { for subseq in sequence.as_bytes().chunks(FASTA_WIDTH) { self.buffer.write_all(&[b'\n'])?; diff --git a/src/rmq/lca.rs b/src/rmq/lca.rs index d54b63f..0b04710 100644 --- a/src/rmq/lca.rs +++ b/src/rmq/lca.rs @@ -49,7 +49,7 @@ impl LCACalculator { fn first_occurence(&self, taxon_id: TaxonId) -> taxon::Result { self.first_occurences .get(&taxon_id) - .ok_or(taxon::ErrorKind::UnknownTaxon(taxon_id).into()) + .ok_or_else(|| taxon::ErrorKind::UnknownTaxon(taxon_id).into()) .map(|t| *t) } }