Skip to content

Remove count_missing_closing_parens() #1906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 2 additions & 54 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,52 +837,6 @@ impl Rewrite for ast::NestedMetaItem {
}
}

fn count_missing_closing_parens(s: &str) -> u32 {
let mut op_parens: u32 = 0;
let mut cl_parens: u32 = 0;

#[derive(Eq, PartialEq)]
pub enum SnippetState {
Normal,
InsideStr,
InsideBulletComment,
InsideSingleLineComment,
}

let mut state = SnippetState::Normal;
let mut iter = s.chars().peekable();
let mut prev_char: Option<char> = None;
while let Some(c) = iter.next() {
let next_char = iter.peek();
match c {
'/' if state == SnippetState::Normal => match next_char {
Some(&'*') => state = SnippetState::InsideBulletComment,
Some(&'/') if prev_char.map_or(true, |c| c != '*') => {
state = SnippetState::InsideSingleLineComment;
}
_ => (),
},
'*' if state == SnippetState::InsideBulletComment &&
next_char.map_or(false, |c| *c == '/') =>
{
state = SnippetState::Normal;
}
'\n' if state == SnippetState::InsideSingleLineComment => state = SnippetState::Normal,
'"' if state == SnippetState::InsideStr && prev_char.map_or(false, |c| c != '\\') => {
state = SnippetState::Normal;
}
'"' if state == SnippetState::Normal && prev_char.map_or(false, |c| c != '\\') => {
state = SnippetState::InsideStr
}
'(' if state == SnippetState::Normal => op_parens += 1,
')' if state == SnippetState::Normal => cl_parens += 1,
_ => (),
}
prev_char = Some(c);
}
op_parens.checked_sub(cl_parens).unwrap_or(0)
}

impl Rewrite for ast::MetaItem {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
Some(match self.node {
Expand All @@ -895,21 +849,15 @@ impl Rewrite for ast::MetaItem {
.shrink_left(name.len() + 3)
.and_then(|s| s.sub_width(2))
);
let hi = self.span.hi +
BytePos(count_missing_closing_parens(&context.snippet(self.span)));
let items = itemize_list(
context.codemap,
list.iter(),
")",
|nested_meta_item| nested_meta_item.span.lo,
// FIXME: Span from MetaItem is missing closing parens.
|nested_meta_item| {
let snippet = context.snippet(nested_meta_item.span);
nested_meta_item.span.hi + BytePos(count_missing_closing_parens(&snippet))
},
|nested_meta_item| nested_meta_item.span.hi,
|nested_meta_item| nested_meta_item.rewrite(context, item_shape),
self.span.lo,
hi,
self.span.hi,
false,
);
let item_vec = items.collect::<Vec<_>>();
Expand Down