Fix clippy warnings

This commit is contained in:
Tiffany Bennett 2021-05-30 16:46:27 -07:00
parent 2ffe164f60
commit ffd31b320e
11 changed files with 45 additions and 44 deletions

View file

@ -19,9 +19,9 @@ pub enum DatePattern {
#[serde(into = "String", try_from = "String")]
pub struct ExprString(pub Expr);
impl Into<String> for ExprString {
fn into(self) -> String {
format!("{}", self.0)
impl From<ExprString> for String {
fn from(value: ExprString) -> String {
format!("{}", value.0)
}
}

View file

@ -33,6 +33,13 @@ pub struct Context {
pub use_humanize: bool,
}
impl Default for Context {
/// Equivalent to Context::new()
fn default() -> Self {
Context::new()
}
}
impl Context {
/// Creates a new, empty context
pub fn new() -> Context {

View file

@ -118,11 +118,8 @@ impl<'a> Iterator for TokenIterator<'a> {
// integer component
if x != '.' {
integer.push(x);
while let Some(c) = self.0.peek().cloned() {
match c {
'0'..='9' => integer.push(self.0.next().unwrap()),
_ => break,
}
while let Some('0'..='9') = self.0.peek().cloned() {
integer.push(self.0.next().unwrap());
}
} else {
integer.push('0');
@ -133,11 +130,8 @@ impl<'a> Iterator for TokenIterator<'a> {
if x != '.' {
self.0.next();
}
while let Some(c) = self.0.peek().cloned() {
match c {
'0'..='9' => buf.push(self.0.next().unwrap()),
_ => break,
}
while let Some('0'..='9') = self.0.peek().cloned() {
buf.push(self.0.next().unwrap());
}
if !buf.is_empty() {
frac = Some(buf)
@ -158,11 +152,8 @@ impl<'a> Iterator for TokenIterator<'a> {
_ => (),
}
}
while let Some(c) = self.0.peek().cloned() {
match c {
'0'..='9' => buf.push(self.0.next().unwrap()),
_ => break,
}
while let Some('0'..='9') = self.0.peek().cloned() {
buf.push(self.0.next().unwrap());
}
if !buf.is_empty() {
exp = Some(buf)
@ -195,9 +186,7 @@ impl<'a> Iterator for TokenIterator<'a> {
break;
}
}
match &*buf {
_ => Token::Ident(buf),
}
Token::Ident(buf)
}
x => Token::Error(format!("Unknown character: '{}'", x)),
};

View file

@ -29,8 +29,12 @@ println!("{}", one_line(&mut ctx, "kWh / year -> W").unwrap());
```
*/
// Maybe someday we can be clean against this.
// False positives, or make code harder to understand.
#![allow(clippy::cognitive_complexity)]
#![allow(clippy::match_like_matches_macro)]
#![allow(clippy::from_str_radix_10)]
#![allow(clippy::option_as_ref_deref)]
#![allow(clippy::needless_lifetimes)]
#[macro_use]
extern crate serde_derive;

View file

@ -760,7 +760,7 @@ impl Number {
}
}
let mut map = BTreeMap::new();
map.insert(orig.0.clone(), orig.1.clone());
map.insert(orig.0.clone(), *orig.1);
Number {
value: val,
unit: map,

View file

@ -253,19 +253,19 @@ impl From<i64> for Numeric {
}
}
impl<'a> Into<f64> for &'a Numeric {
fn into(self) -> f64 {
match *self {
impl<'a> From<&'a Numeric> for f64 {
fn from(value: &'a Numeric) -> f64 {
match value {
Numeric::Rational(ref rational) => rational.as_float(),
Numeric::Float(f) => f,
Numeric::Float(f) => *f,
}
}
}
impl Into<NumericParts> for Numeric {
fn into(self) -> NumericParts {
let (exact, approx) = self.string_repr(10, Digits::Default);
let (num, den) = self.to_rational();
impl From<Numeric> for NumericParts {
fn from(value: Numeric) -> NumericParts {
let (exact, approx) = value.string_repr(10, Digits::Default);
let (num, den) = value.to_rational();
NumericParts {
numer: num.to_string(),
denom: den.to_string(),

View file

@ -596,7 +596,7 @@ impl<'a> TokenFmt<'a> for DurationReply {
let res = join(
parts
.iter()
.map(|x| *x)
.copied()
.filter(|x| x.exact_value.as_ref().map(|x| &**x) != Some("0"))
.chain(once(&self.seconds))
.map(|x| Span::child(x)),

View file

@ -261,7 +261,7 @@ impl Substance {
.chain(self.properties.properties.iter().map(func))
.collect::<Result<Vec<Option<PropertyReply>>, String>>()?
.into_iter()
.filter_map(|x| x)
.flatten()
.collect(),
})
}
@ -353,7 +353,7 @@ impl Substance {
.chain(self.properties.properties.iter().map(func))
.collect::<Result<Vec<Option<PropertyReply>>, String>>()?
.into_iter()
.filter_map(|x| x)
.flatten()
.collect(),
})
}
@ -396,6 +396,7 @@ impl<'a, 'b> Div<&'b Number> for &'a Substance {
impl<'a, 'b> Add<&'b Substance> for &'a Substance {
type Output = Result<Substance, String>;
#[allow(clippy::suspicious_arithmetic_impl)]
fn add(self, other: &'b Substance) -> Self::Output {
let res = Substance {
amount: Number::one(),

View file

@ -789,7 +789,7 @@ pub fn parse_query(iter: &mut Iter<'_>) -> Query {
match iter.next() {
Some(Token::Decimal(int, None, None)) => {
match u64::from_str_radix(&*int, 10) {
Ok(v) if v >= 2 && v <= 36 => Some(v as u8),
Ok(v @ 2..=36) => Some(v as u8),
Ok(v) => {
return Query::Error(format!(
"Unsupported base {}, must be from 2 to 36",

View file

@ -11,7 +11,6 @@ use rink_core::context::Context;
use rink_core::fmt::FmtToken;
use rink_core::{ast, date, gnu_units, CURRENCY_FILE, DATES_FILE, DEFAULT_FILE};
use serde_derive::Deserialize;
use serde_json;
use std::ffi::OsString;
use std::io::{ErrorKind, Read, Seek, SeekFrom};
use std::path::Path;
@ -181,7 +180,7 @@ impl Default for Colors {
impl Config {
pub fn get_theme(&self) -> &Theme {
if self.colors.enabled == false {
if !self.colors.enabled {
&self.disabled_theme
} else {
let name = &self.colors.theme;
@ -339,9 +338,10 @@ fn download_to_file(path: &Path, url: &str, timeout: Duration) -> Result<File> {
temp_file.as_file_mut().sync_all()?;
temp_file.as_file_mut().seek(SeekFrom::Start(0))?;
Ok(temp_file
temp_file
.persist(path)
.wrap_err("Failed to write to cache dir")?)
.wrap_err("Failed to write to cache dir")
}
fn cached(filename: &str, url: &str, expiration: Duration, timeout: Duration) -> Result<File> {

View file

@ -48,9 +48,9 @@ fn parse_color(input: &str) -> Option<Color> {
let value = input.parse::<u8>();
if let Ok(value) = value {
Some(Color::Fixed(value))
} else if input.starts_with("rgb(") && input.ends_with(")") {
} else if input.starts_with("rgb(") && input.ends_with(')') {
let input = &input[4..input.len() - 1];
let mut colors = input.split(",").map(|num| num.parse::<u8>().ok()).flatten();
let mut colors = input.split(',').map(|num| num.parse::<u8>().ok()).flatten();
let r = colors.next();
let g = colors.next();
let b = colors.next();
@ -58,8 +58,8 @@ fn parse_color(input: &str) -> Option<Color> {
(Some(r), Some(g), Some(b)) => Some(Color::RGB(r, g, b)),
_ => None,
}
} else if input.starts_with("#") {
let input = input.trim_start_matches("#");
} else if input.starts_with('#') {
let input = input.trim_start_matches('#');
let value = u32::from_str_radix(input, 16);
if let Ok(value) = value {
let r = (value >> 16) as u8;
@ -83,7 +83,7 @@ where
let string = des.deserialize_str(StringVisitor)?;
let mut style = Style::new();
let mut next_color_is_bg = false;
for word in string.split(" ") {
for word in string.split(' ') {
match word {
"bold" => style = style.bold(),
"italic" => style = style.italic(),