Skip to content

Commit

Permalink
Apply some Clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
mo8it committed May 16, 2024
1 parent 4bb7876 commit c75846a
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ impl From<String> for Error {
impl Display for Kind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Kind::Message(msg) => f.write_str(&msg),
Kind::Message(msg) => f.write_str(msg),
Kind::InvalidType(v, exp) => {
write!(f, "invalid type: found {}, expected {}", v, exp)
}
Expand Down
3 changes: 1 addition & 2 deletions src/figment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ impl Figment {
(Err(e), Err(prev)) => Err(e.retagged(tag).chain(prev)),
(Ok(mut new), Ok(old)) => {
new.iter_mut()
.map(|(p, map)| std::iter::repeat(p).zip(map.values_mut()))
.flatten()
.flat_map(|(p, map)| std::iter::repeat(p).zip(map.values_mut()))
.for_each(|(p, v)| v.map_tag(|t| *t = tag.for_profile(p)));

Ok(old.coalesce(new, order))
Expand Down
6 changes: 2 additions & 4 deletions src/jail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,7 @@ impl Jail {
pub fn clear_env(&mut self) {
for (key, val) in std::env::vars_os() {
std::env::remove_var(&key);
if !self.saved_env_vars.contains_key(&key) {
self.saved_env_vars.insert(key, Some(val));
}
self.saved_env_vars.entry(key).or_insert(Some(val));
}
}

Expand Down Expand Up @@ -356,7 +354,7 @@ impl Jail {

impl Drop for Jail {
fn drop(&mut self) {
for (key, value) in self.saved_env_vars.iter() {
for (key, value) in &self.saved_env_vars {
match value {
Some(val) => std::env::set_var(key, val),
None => std::env::remove_var(key)
Expand Down
2 changes: 1 addition & 1 deletion src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl fmt::Display for Source {
Source::File(p) => {
use {std::env::current_dir, crate::util::diff_paths};

match current_dir().ok().and_then(|cwd| diff_paths(p, &cwd)) {
match current_dir().ok().and_then(|cwd| diff_paths(p, cwd)) {
Some(r) if r.iter().count() < p.iter().count() => r.display().fmt(f),
Some(_) | None => p.display().fmt(f)
}
Expand Down
8 changes: 4 additions & 4 deletions src/providers/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ impl<F: Format> Provider for Data<F> {
use Source::*;
let map: Result<Map<Profile, Dict>, _> = match (&self.source, &self.profile) {
(File(None), _) => return Ok(Map::new()),
(File(Some(path)), None) => F::from_path(&path),
(String(s), None) => F::from_str(&s),
(File(Some(path)), Some(prof)) => F::from_path(&path).map(|v| prof.collect(v)),
(String(s), Some(prof)) => F::from_str(&s).map(|v| prof.collect(v)),
(File(Some(path)), None) => F::from_path(path),
(String(s), None) => F::from_str(s),
(File(Some(path)), Some(prof)) => F::from_path(path).map(|v| prof.collect(v)),
(String(s), Some(prof)) => F::from_str(s).map(|v| prof.collect(v)),
};

Ok(map.map_err(|e| e.to_string())?)
Expand Down
6 changes: 3 additions & 3 deletions src/providers/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl Env {
pub fn filter<F: Clone + 'static>(self, filter: F) -> Self
where F: Fn(&UncasedStr) -> bool
{
self.chain(move |prev| prev.filter(|v| filter(&v)))
self.chain(move |prev| prev.filter(|v| filter(v)))
}

/// Applys an additional mapping to the keys of environment variables being
Expand Down Expand Up @@ -432,7 +432,7 @@ impl Env {
/// });
/// ```
pub fn ignore(self, keys: &[&str]) -> Self {
let keys: Vec<String> = keys.iter().map(|s| s.to_string()).collect();
let keys: Vec<String> = keys.iter().map(|s| (*s).to_string()).collect();
self.filter(move |key| !keys.iter().any(|k| k.as_str() == key))
}

Expand Down Expand Up @@ -461,7 +461,7 @@ impl Env {
/// });
/// ```
pub fn only(self, keys: &[&str]) -> Self {
let keys: Vec<String> = keys.iter().map(|s| s.to_string()).collect();
let keys: Vec<String> = keys.iter().map(|s| (*s).to_string()).collect();
self.filter(move |key| keys.iter().any(|k| k.as_str() == key))
}

Expand Down
4 changes: 2 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ pub fn diff_paths<P, B>(path: P, base: B) -> Option<PathBuf>
}
(None, _) => comps.push(Component::ParentDir),
(Some(a), Some(b)) if comps.is_empty() && a == b => (),
(Some(a), Some(b)) if b == Component::CurDir => comps.push(a),
(Some(_), Some(b)) if b == Component::ParentDir => return None,
(Some(a), Some(Component::CurDir)) => comps.push(a),
(Some(_), Some(Component::ParentDir)) => return None,
(Some(a), Some(_)) => {
comps.push(Component::ParentDir);
for _ in itb {
Expand Down
10 changes: 5 additions & 5 deletions src/value/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<'de: 'c, 'c, I: Interpreter> Deserializer<'de> for ConfiguredValueDe<'c, I>
_ => visitor.visit_some(self)
};

result.map_err(|e| e.retagged(tag).resolved(&config))
result.map_err(|e| e.retagged(tag).resolved(config))
}

fn deserialize_struct<V: Visitor<'de>>(
Expand Down Expand Up @@ -145,7 +145,7 @@ impl<'de: 'c, 'c, I: Interpreter> Deserializer<'de> for ConfiguredValueDe<'c, I>
_ => self.deserialize_any(v)
};

result.map_err(|e| e.retagged(tag).resolved(&config))
result.map_err(|e| e.retagged(tag).resolved(config))
}

fn deserialize_newtype_struct<V: Visitor<'de>>(
Expand Down Expand Up @@ -432,10 +432,10 @@ impl<'de> Deserialize<'de> for Value {
// Total hack to "fingerprint" our deserializer by checking if
// human_readable changes, which does for ours but shouldn't for others.
let (a, b) = (de.is_human_readable(), de.is_human_readable());
if a != b {
de.deserialize_struct(Value::NAME, Value::FIELDS, ValueVisitor)
} else {
if a == b {
de.deserialize_any(ValueVisitor)
} else {
de.deserialize_struct(Value::NAME, Value::FIELDS, ValueVisitor)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/value/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn escape(string: &str) -> Result<Cow<'_, str>, Error> {
Some((_, 'n')) => val.push('\n'),
Some((_, 'r')) => val.push('\r'),
Some((_, 't')) => val.push('\t'),
Some((i, c @ 'u')) | Some((i, c @ 'U')) => {
Some((i, c @ ('u' | 'U'))) => {
let len = if c == 'u' { 4 } else { 8 };
val.push(hex(&mut chars, i, len)?);
}
Expand All @@ -64,7 +64,7 @@ fn hex<I>(mut chars: I, i: usize, len: usize) -> Result<char, Error>
let mut buf = String::with_capacity(len);
for _ in 0..len {
match chars.next() {
Some((_, ch)) if ch as u32 <= 0x7F && ch.is_digit(16) => buf.push(ch),
Some((_, ch)) if ch as u32 <= 0x7F && ch.is_ascii_hexdigit() => buf.push(ch),
Some((i, ch)) => return Err(Error::InvalidHexEscape(i, ch)),
None => return Err(Error::UnterminatedString(0)),
}
Expand Down
4 changes: 2 additions & 2 deletions src/value/magic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl Magic for RelativePathBuf {
}

// If we have this struct with no metadata_path, still use the value.
let value = de.value.find_ref(Self::FIELDS[1]).unwrap_or(&de.value);
let value = de.value.find_ref(Self::FIELDS[1]).unwrap_or(de.value);
map.insert(Self::FIELDS[1].into(), value.clone());
visitor.visit_map(MapDe::new(&map, |v| ConfiguredValueDe::<I>::from(config, v)))
}
Expand Down Expand Up @@ -592,7 +592,7 @@ impl<T: for<'de> Deserialize<'de>> Magic for Tagged<T> {
}

// If we have this struct with default tag, use the value.
let value = de.value.find_ref(Self::FIELDS[1]).unwrap_or(&de.value);
let value = de.value.find_ref(Self::FIELDS[1]).unwrap_or(de.value);
map.insert(Self::FIELDS[0].into(), de.value.tag().into());
map.insert(Self::FIELDS[1].into(), value.clone());
visitor.visit_map(MapDe::new(&map, |v| ConfiguredValueDe::<I>::from(config, v)))
Expand Down
19 changes: 10 additions & 9 deletions src/value/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,15 @@ impl SeqSerializer {
}
}

impl<'a> ser::SerializeSeq for SeqSerializer {
impl ser::SerializeSeq for SeqSerializer {
type Ok = Value;
type Error = Error;

fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
where T: Serialize
{
Ok(self.sequence.push(value.serialize(ValueSerializer)?))
self.sequence.push(value.serialize(ValueSerializer)?);
Ok(())
}

fn end(self) -> Result<Self::Ok> {
Expand All @@ -250,7 +251,7 @@ impl<'a> ser::SerializeSeq for SeqSerializer {
}
}

impl<'a> ser::SerializeTuple for SeqSerializer {
impl ser::SerializeTuple for SeqSerializer {
type Ok = Value;
type Error = Error;

Expand All @@ -266,7 +267,7 @@ impl<'a> ser::SerializeTuple for SeqSerializer {
}

// Same thing but for tuple structs.
impl<'a> ser::SerializeTupleStruct for SeqSerializer {
impl ser::SerializeTupleStruct for SeqSerializer {
type Ok = Value;
type Error = Error;

Expand All @@ -281,7 +282,7 @@ impl<'a> ser::SerializeTupleStruct for SeqSerializer {
}
}

impl<'a> ser::SerializeTupleVariant for SeqSerializer {
impl ser::SerializeTupleVariant for SeqSerializer {
type Ok = Value;
type Error = Error;

Expand All @@ -306,7 +307,7 @@ impl MapSerializer {
}
}

impl<'a> ser::SerializeMap for MapSerializer {
impl ser::SerializeMap for MapSerializer {
type Ok = Value;
type Error = Error;

Expand All @@ -329,7 +330,7 @@ impl<'a> ser::SerializeMap for MapSerializer {
}

fn end(self) -> Result<Self::Ok> {
let iter = self.keys.into_iter().zip(self.values.into_iter());
let iter = self.keys.into_iter().zip(self.values);
let value: Value = iter.collect::<Dict>().into();
match self.tag {
Some(tag) => Ok(crate::util::map![tag => value].into()),
Expand All @@ -338,7 +339,7 @@ impl<'a> ser::SerializeMap for MapSerializer {
}
}

impl<'a> ser::SerializeStruct for MapSerializer {
impl ser::SerializeStruct for MapSerializer {
type Ok = Value;
type Error = Error;

Expand All @@ -354,7 +355,7 @@ impl<'a> ser::SerializeStruct for MapSerializer {
}
}

impl<'a> ser::SerializeStructVariant for MapSerializer {
impl ser::SerializeStructVariant for MapSerializer {
type Ok = Value;
type Error = Error;

Expand Down
14 changes: 7 additions & 7 deletions src/value/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,21 @@ impl PartialEq for Tag {

impl Eq for Tag { }

impl PartialOrd for Tag {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.metadata_id().partial_cmp(&other.metadata_id())
}
}

impl Ord for Tag {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.metadata_id().cmp(&other.metadata_id())
}
}

impl PartialOrd for Tag {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl std::hash::Hash for Tag {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
state.write_u64(self.metadata_id())
state.write_u64(self.metadata_id());
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/value/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl Value {
/// assert!(value.find_ref("pineapple").is_none());
/// ```
pub fn find_ref<'a>(&'a self, path: &str) -> Option<&'a Value> {
fn find<'a, 'v>(mut keys: Split<'a, char>, value: &'v Value) -> Option<&'v Value> {
fn find<'v>(mut keys: Split<char>, value: &'v Value) -> Option<&'v Value> {
match keys.next() {
Some(k) if !k.is_empty() => find(keys, value.as_dict()?.get(k)?),
Some(_) | None => Some(value)
Expand Down Expand Up @@ -462,7 +462,7 @@ impl<'a, T: Into<Value> + Clone> From<&'a [T]> for Value {
}
}

impl<'a, T: Into<Value>> From<Vec<T>> for Value {
impl<T: Into<Value>> From<Vec<T>> for Value {
fn from(vec: Vec<T>) -> Value {
let vector = vec.into_iter().map(|v| v.into()).collect();
Value::Array(Tag::Default, vector)
Expand Down Expand Up @@ -577,7 +577,7 @@ impl Num {
Some(match self {
Num::U8(v) => v as u32,
Num::U16(v) => v as u32,
Num::U32(v) => v as u32,
Num::U32(v) => v,
_ => return None,
})
}
Expand All @@ -598,7 +598,7 @@ impl Num {
Num::U16(v) => v as u128,
Num::U32(v) => v as u128,
Num::U64(v) => v as u128,
Num::U128(v) => v as u128,
Num::U128(v) => v,
Num::USize(v) => v as u128,
_ => return None,
})
Expand All @@ -624,7 +624,7 @@ impl Num {
Num::U16(v) => v as u128,
Num::U32(v) => v as u128,
Num::U64(v) => v as u128,
Num::U128(v) => v as u128,
Num::U128(v) => v,
Num::USize(v) => v as u128,
Num::I8(v) if v >= 0 => v as u128,
Num::I16(v) if v >= 0 => v as u128,
Expand Down Expand Up @@ -653,7 +653,7 @@ impl Num {
Num::I16(v) => v as i128,
Num::I32(v) => v as i128,
Num::I64(v) => v as i128,
Num::I128(v) => v as i128,
Num::I128(v) => v,
Num::ISize(v) => v as i128,
_ => return None,
})
Expand All @@ -673,7 +673,7 @@ impl Num {
pub fn to_f64(&self) -> Option<f64> {
Some(match *self {
Num::F32(v) => v as f64,
Num::F64(v) => v as f64,
Num::F64(v) => v,
_ => return None,
})
}
Expand Down Expand Up @@ -703,16 +703,16 @@ impl Num {
Num::U16(v) => Actual::Unsigned(v as u128),
Num::U32(v) => Actual::Unsigned(v as u128),
Num::U64(v) => Actual::Unsigned(v as u128),
Num::U128(v) => Actual::Unsigned(v as u128),
Num::U128(v) => Actual::Unsigned(v),
Num::USize(v) => Actual::Unsigned(v as u128),
Num::I8(v) => Actual::Signed(v as i128),
Num::I16(v) => Actual::Signed(v as i128),
Num::I32(v) => Actual::Signed(v as i128),
Num::I64(v) => Actual::Signed(v as i128),
Num::I128(v) => Actual::Signed(v as i128),
Num::I128(v) => Actual::Signed(v),
Num::ISize(v) => Actual::Signed(v as i128),
Num::F32(v) => Actual::Float(v as f64),
Num::F64(v) => Actual::Float(v as f64),
Num::F64(v) => Actual::Float(v),
}
}
}
Expand Down

0 comments on commit c75846a

Please sign in to comment.