Skip to content

Commit

Permalink
manually clean code with clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Van der Jeugt committed Sep 14, 2021
1 parent ebdd5e8 commit 5e749d6
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/agg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait Aggregator {
fn aggregate(&self, taxons: &HashMap<TaxonId, f32>) -> Result<TaxonId>;

/// Aggregates a list of taxons into a resulting taxon id.
fn counting_aggregate(&self, taxons: &Vec<TaxonId>) -> Result<TaxonId> {
fn counting_aggregate(&self, taxons: &[TaxonId]) -> Result<TaxonId> {
let taxons = taxons.iter().map(|&t| (t, 1.0));
self.aggregate(&count(taxons))
}
Expand Down
1 change: 1 addition & 0 deletions src/commands/buildindex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/commands/joinkmers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/commands/printindex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/commands/prot2kmer2lca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ where
output_mutex
.lock()
.unwrap()
.write(chunk_output.as_bytes())?;
.write_all(chunk_output.as_bytes())?;
Ok(())
})
.collect()
Expand Down
8 changes: 5 additions & 3 deletions src/commands/splitkmers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/taxa2agg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ 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::<Vec<_>>();
if split.len() != 2 {
return Err("Taxon without score".into());
}
Ok((split[0].parse::<TaxonId>()?, split[1].parse::<f32>()?))
}

fn not_scored(tid: &String) -> errors::Result<(TaxonId, f32)> {
fn not_scored(tid: &str) -> errors::Result<(TaxonId, f32)> {
Ok((tid.parse::<TaxonId>()?, 1.0))
}

Expand All @@ -162,7 +162,7 @@ pub fn taxa2agg(args: TaxaToAgg) -> errors::Result<()> {
let taxons = record
.sequence
.iter()
.map(parser)
.map(|s| parser(s))
.collect::<errors::Result<Vec<(TaxonId, f32)>>>()?;

// Create a frequency table of taxons for this read (taking into account the lower bound)
Expand Down
13 changes: 5 additions & 8 deletions src/commands/taxa2freq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,17 @@ pub fn taxa2freq(args: TaxaToFreq) -> errors::Result<()> {
}

fn count_file<T: BufRead>(
snapping: &Vec<Option<taxon::TaxonId>>,
snapping: &[Option<taxon::TaxonId>],
counts: &mut HashMap<taxon::TaxonId, Vec<usize>>,
index: usize,
numfiles: usize,
file: T,
) -> errors::Result<()> {
for line in file.lines() {
match line?.parse::<taxon::TaxonId>() {
Ok(taxon) => {
counts
.entry(snapping[taxon].unwrap_or(0))
.or_insert(vec![0; numfiles])[index] += 1
}
Err(_) => (),
if let Ok(taxon) = line?.parse::<taxon::TaxonId>() {
counts
.entry(snapping[taxon].unwrap_or(0))
.or_insert_with(|| vec![0; numfiles])[index] += 1
}
}
Ok(())
Expand Down
1 change: 1 addition & 0 deletions src/commands/taxonomy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/commands/uniq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions src/dna/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand All @@ -43,9 +43,9 @@ impl From<u8> for Nucleotide {
}
}

impl Into<u8> for Nucleotide {
fn into(self) -> u8 {
match self {
impl From<Nucleotide> for u8 {
fn from(n: Nucleotide) -> u8 {
match n {
A => b'A',
C => b'C',
G => b'G',
Expand Down Expand Up @@ -89,7 +89,7 @@ impl<'a> From<&'a Vec<String>> 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 {
Expand Down
2 changes: 1 addition & 1 deletion src/dna/translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl str::FromStr for &'static TranslationTable {
let id = s.parse::<usize>()?;
TABLES[id - 1]
.as_ref()
.ok_or(ErrorKind::UnknownTable(id).into())
.ok_or_else(|| ErrorKind::UnknownTable(id).into())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/io/fasta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<R: Read> Reader<R> {
let lines = BufReader::with_capacity(BUFFER_SIZE, reader)
.lines()
.peekable();
Reader { unwrap, lines }
Reader { lines, unwrap }
}

/// Reads the next record from the FASTA file.
Expand Down Expand Up @@ -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'])?;
Expand Down
2 changes: 1 addition & 1 deletion src/rmq/lca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl LCACalculator {
fn first_occurence(&self, taxon_id: TaxonId) -> taxon::Result<usize> {
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)
}
}
Expand Down

0 comments on commit 5e749d6

Please sign in to comment.