Skip to content

Commit

Permalink
chore: fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowee committed Jun 22, 2023
1 parent 360e1b4 commit 586a517
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 58 deletions.
8 changes: 2 additions & 6 deletions src/inferrer/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,16 @@ impl<'a> DerefMut for TypeArenaWithDSU<'a> {
impl<'a> ITypeArena for TypeArenaWithDSU<'a> {
#[inline(always)]
fn get(&self, i: ArenaIndex) -> Option<&Type> {

// dbg!(i, &t, self.find_representative(i));
self
.find_representative(i) // if it is in the DSU
self.find_representative(i) // if it is in the DSU
.or(Some(i)) // O.W. it should be a newly added type during unioning
.and_then(|arni| self.arena.get(arni)) // TODO: clean up
}

#[inline(always)]
fn get_mut(&mut self, i: ArenaIndex) -> Option<&mut Type> {

// dbg!(i, &t);
self
.find_representative(i) // if it is in the DSU
self.find_representative(i) // if it is in the DSU
.or(Some(i)) // O.W. it should be a newly added type during unioning
.and_then(move |arni| self.arena.get_mut(arni))
// FIX: borrowing issue
Expand Down
4 changes: 2 additions & 2 deletions src/inferrer/unioner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a, T: ITypeArena> UnionerClosure<'a, T> {
.expect("The type should be present in the arena during unioning")
{
Type::Union(_) => {
let Union { name_hints, types } = if first_union.is_none() {
let Union { name_hints, types } = if let Some(..) = first_union {
first_union = Some(r#type);
mem::take(self.arena.get_mut(r#type).unwrap())
.into_union()
Expand All @@ -80,7 +80,7 @@ impl<'a, T: ITypeArena> UnionerClosure<'a, T> {
match *self.arena.get(r#type).unwrap() {
Type::Map(_) => {
let map;
if first_map.is_none() {
if let Some(..) = first_map {
// If this is the first map in the union, just take its inner out so that
// its slot can be reused again with ArenaIndex left intact.
first_map = Some(r#type);
Expand Down
6 changes: 6 additions & 0 deletions src/schema/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ pub struct TypeArena {
primitive_types: [ArenaIndex; 9],
}

impl Default for TypeArena {
fn default() -> Self {
Self::new()
}
}

impl TypeArena {
pub fn new() -> Self {
let mut arena = Arena::<Type>::new();
Expand Down
40 changes: 8 additions & 32 deletions src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ pub struct Schema {
pub root: ArenaIndex,
}

#[derive(Debug, Clone)]
#[derive(Default)]
#[derive(Debug, Clone, Default)]
pub enum Type {
// TODO: doc
Map(Map),
Expand Down Expand Up @@ -166,48 +165,27 @@ impl Type {
}

pub fn is_null(&self) -> bool {
match *self {
Self::Null => true,
_ => false,
}
matches!(*self, Self::Null)
}

pub fn is_missing(&self) -> bool {
match *self {
Self::Missing => true,
_ => false,
}
matches!(*self, Self::Missing)
}
pub fn is_bool(&self) -> bool {
match *self {
Self::Bool => true,
_ => false,
}
matches!(*self, Self::Bool)
}
pub fn is_int(&self) -> bool {
match *self {
Self::Int => true,
_ => false,
}
matches!(*self, Self::Int)
}
pub fn is_float(&self) -> bool {
match *self {
Self::Float => true,
_ => false,
}
matches!(*self, Self::Float)
}
pub fn is_string(&self) -> bool {
match *self {
Self::String => true,
_ => false,
}
matches!(*self, Self::String)
}

pub fn is_any(&self) -> bool {
match *self {
Self::Any => true,
_ => false,
}
matches!(*self, Self::Any)
}

pub fn into_array(self) -> Option<ArenaIndex> {
Expand Down Expand Up @@ -253,5 +231,3 @@ impl Type {
self.as_union().is_some()
}
}


5 changes: 2 additions & 3 deletions src/schema/name_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ impl NameHints {

impl fmt::Display for NameHints {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.iter()
.map(String::as_str)
.intersperse("Or").try_for_each(|s| write!(fmt, "{}", s))
Itertools::intersperse(self.iter().map(String::as_str), "Or")
.try_for_each(|s| write!(fmt, "{}", s))
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/target/python_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,8 @@ fn write_output(
"typing"
};
write!(header, "from {} import ", typing_mod)?;
imports_from_typing
.into_iter()
.intersperse(", ").try_for_each(|e| write!(header, "{}", e))?;
Itertools::intersperse(imports_from_typing.into_iter(), ", ")
.try_for_each(|e| write!(header, "{}", e))?;
if typing_mod == "typing_extensions" {
write!(header, " # For Python < 3.11, pip install typing_extensions; For Python >= 3.11, just change it to `typing`")?;
}
Expand All @@ -198,7 +197,7 @@ fn write_output(
Ok(())
}

impl<'i, 'c> Display for Contexted<&'c Type, Context<'c>> {
impl<'i, 'c> Display for Contexted<&'i Type, Context<'c>> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &Contexted {
inner: r#type,
Expand Down Expand Up @@ -276,7 +275,7 @@ impl<'i, 'c> Display for Contexted<&'c Type, Context<'c>> {
}

// inner of Union
impl<'i, 'c> Display for Contexted<&'c HashSet<ArenaIndex>, Context<'c>> {
impl<'i, 'c> Display for Contexted<&'i HashSet<ArenaIndex>, Context<'c>> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &Contexted {
inner: arnis,
Expand All @@ -298,8 +297,9 @@ impl<'i, 'c> Display for Contexted<&'c HashSet<ArenaIndex>, Context<'c>> {
options.kind != Kind::TypedDict || !r#type.is_missing() || arnis.len() == 1
})
.filter(|&r#type| {
!(options.to_generate_type_alias_for_union && is_non_trivial)
|| !r#type.is_null()
!(options.to_generate_type_alias_for_union
&& is_non_trivial
&& r#type.is_null())
}),
);
let _ = iter.peek(); // Discard the first
Expand All @@ -326,7 +326,7 @@ impl<'i, 'c> Display for Contexted<&'c HashSet<ArenaIndex>, Context<'c>> {
}
}

impl<'i, 'c> Display for Contexted<&'c IndexMap<String, ArenaIndex>, Context<'c>> {
impl<'i, 'c> Display for Contexted<&'i IndexMap<String, ArenaIndex>, Context<'c>> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &Contexted {
inner: fields,
Expand Down
7 changes: 3 additions & 4 deletions src/target/python_inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@ fn write_output(
write!(header, ", ")?;
}
}
imports_from_typing
.into_iter()
.intersperse(", ").try_for_each(|e| write!(header, "{}", e))?;
Itertools::intersperse(imports_from_typing.into_iter(), ", ")
.try_for_each(|e| write!(header, "{}", e))?;
writeln!(header)?;
}
if importing_datetime {
Expand All @@ -169,7 +168,7 @@ fn write_output(
Ok(())
}

impl<'i, 'c> Display for Contexted<ArenaIndex, Context<'c>> {
impl<'c> Display for Contexted<ArenaIndex, Context<'c>> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &Contexted {
inner: arni,
Expand Down
6 changes: 3 additions & 3 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn test_quicktype() {
to_nest_when_possible: true,
to_mark_optional_as_not_total: false,
}
.generate(&mut schema);
.generate(&schema);
println!("{}", output.header);
println!("{}", output.body);
println!("{}", now.elapsed().as_millis());
Expand All @@ -50,7 +50,7 @@ fn test_githubstatus() {
to_generate_type_alias_for_union: false,
indentation: Indentation::Space(4),
}
.generate(&mut schema);
.generate(&schema);
}

#[test]
Expand All @@ -69,5 +69,5 @@ fn test_tree_recursion() {
to_generate_type_alias_for_union: false,
indentation: Indentation::Space(4),
}
.generate(&mut schema);
.generate(&schema);
}

0 comments on commit 586a517

Please sign in to comment.