Skip to content

Commit

Permalink
Remove unused peekable operations from swayfmt (#2915)
Browse files Browse the repository at this point in the history
Closes #2496
  • Loading branch information
eureka-cpu authored Oct 8, 2022
1 parent 2e32477 commit 61d6d65
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 37 deletions.
25 changes: 16 additions & 9 deletions swayfmt/src/items/item_enum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use crate::{
},
};
use std::fmt::Write;
use sway_ast::{token::Delimiter, ItemEnum};
use sway_ast::{
token::{Delimiter, PunctKind},
ItemEnum,
};
use sway_types::Spanned;

#[cfg(test)]
Expand Down Expand Up @@ -69,7 +72,7 @@ impl Format for ItemEnum {
}
});

let mut value_pairs_iter = value_pairs.iter().enumerate().peekable();
let value_pairs_iter = value_pairs.iter().enumerate();
for (var_index, (type_field, comma_token)) in value_pairs_iter.clone() {
write!(
formatted_code,
Expand Down Expand Up @@ -98,13 +101,17 @@ impl Format for ItemEnum {
type_field.colon_token.span().as_str(),
)?;
type_field.ty.format(formatted_code, formatter)?;
if value_pairs_iter.peek().is_some() {
writeln!(formatted_code, "{}", comma_token.span().as_str())?;
} else if let Some(final_value) = &fields.final_value_opt {
// TODO: Handle annotation
let final_value = &final_value.value;
write!(formatted_code, "{}", final_value.span().as_str())?;
}
writeln!(formatted_code, "{}", comma_token.span().as_str())?;
}
if let Some(final_value) = &fields.final_value_opt {
// TODO: Handle annotation
let final_value = &final_value.value;
writeln!(
formatted_code,
"{}{}",
final_value.span().as_str(),
PunctKind::Comma.as_char(),
)?;
}
}
FieldAlignment::Off => fields.format(formatted_code, formatter)?,
Expand Down
11 changes: 5 additions & 6 deletions swayfmt/src/items/item_storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Format for ItemStorage {
}
});

let mut value_pairs_iter = value_pairs.iter().enumerate().peekable();
let value_pairs_iter = value_pairs.iter().enumerate();
for (field_index, (storage_field, comma_token)) in value_pairs_iter.clone()
{
write!(
Expand Down Expand Up @@ -99,11 +99,10 @@ impl Format for ItemStorage {
storage_field
.initializer
.format(formatted_code, formatter)?;
if value_pairs_iter.peek().is_some() {
writeln!(formatted_code, "{}", comma_token.ident().as_str())?;
} else if let Some(final_value) = &fields.final_value_opt {
final_value.format(formatted_code, formatter)?;
}
writeln!(formatted_code, "{}", comma_token.ident().as_str())?;
}
if let Some(final_value) = &fields.final_value_opt {
final_value.format(formatted_code, formatter)?;
}
}
FieldAlignment::Off => fields.format(formatted_code, formatter)?,
Expand Down
15 changes: 7 additions & 8 deletions swayfmt/src/items/item_struct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Format for ItemStruct {
}
});

let mut value_pairs_iter = value_pairs.iter().enumerate().peekable();
let value_pairs_iter = value_pairs.iter().enumerate();
for (var_index, (type_field, comma_token)) in value_pairs_iter.clone() {
write!(
formatted_code,
Expand Down Expand Up @@ -98,13 +98,12 @@ impl Format for ItemStruct {
type_field.colon_token.span().as_str(),
)?;
type_field.ty.format(formatted_code, formatter)?;
if value_pairs_iter.peek().is_some() {
writeln!(formatted_code, "{}", comma_token.span().as_str())?;
} else if let Some(final_value) = &fields.final_value_opt {
// TODO: Handle annotation
let final_value = &final_value.value;
write!(formatted_code, "{}", final_value.span().as_str())?;
}
writeln!(formatted_code, "{}", comma_token.span().as_str())?;
}
if let Some(final_value) = &fields.final_value_opt {
// TODO: Handle annotation
let final_value = &final_value.value;
write!(formatted_code, "{}", final_value.span().as_str())?;
}
}
FieldAlignment::Off => {
Expand Down
8 changes: 3 additions & 5 deletions swayfmt/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ impl Format for Module {
dependency.format(formatted_code, formatter)?;
}

let mut iter = self.items.iter().peekable();
while let Some(item) = iter.next() {
let iter = self.items.iter();
for item in iter.clone() {
item.format(formatted_code, formatter)?;
if iter.peek().is_some() {
writeln!(formatted_code)?;
}
writeln!(formatted_code)?;
}

Ok(())
Expand Down
14 changes: 5 additions & 9 deletions swayfmt/src/utils/language/punctuated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ where
}
LineStyle::Inline => {
write!(formatted_code, " ")?;
let mut value_pairs_iter = self.value_separator_pairs.iter().peekable();
let value_pairs_iter = self.value_separator_pairs.iter();
for (type_field, punctuation) in value_pairs_iter.clone() {
type_field.format(formatted_code, formatter)?;
punctuation.format(formatted_code, formatter)?;

if value_pairs_iter.peek().is_some() {
write!(formatted_code, " ")?;
}
write!(formatted_code, " ")?;
}
if let Some(final_value) = &self.final_value_opt {
final_value.format(formatted_code, formatter)?;
Expand All @@ -54,7 +52,7 @@ where
}
LineStyle::Multiline => {
writeln!(formatted_code)?;
let mut value_pairs_iter = self.value_separator_pairs.iter().peekable();
let value_pairs_iter = self.value_separator_pairs.iter();
for (type_field, comma_token) in value_pairs_iter.clone() {
write!(
formatted_code,
Expand All @@ -63,10 +61,8 @@ where
)?;
type_field.format(formatted_code, formatter)?;

if value_pairs_iter.peek().is_some() {
comma_token.format(formatted_code, formatter)?;
writeln!(formatted_code)?;
}
comma_token.format(formatted_code, formatter)?;
writeln!(formatted_code)?;
}
if let Some(final_value) = &self.final_value_opt {
write!(
Expand Down

0 comments on commit 61d6d65

Please sign in to comment.