Skip to content

Commit 99681ec

Browse files
author
Alexander Glusker
committed
fix additional semicolon during changine parentheses style in lazy_static macro
1 parent 202fa22 commit 99681ec

File tree

6 files changed

+40
-2
lines changed

6 files changed

+40
-2
lines changed

src/macros.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ fn rewrite_macro_inner(
230230
// Format well-known macros which cannot be parsed as a valid AST.
231231
if macro_name == "lazy_static!" && !has_comment {
232232
if let success @ Some(..) = format_lazy_static(context, shape, ts.clone()) {
233+
if original_style == Delimiter::Parenthesis {
234+
context.lazy_static_success.replace(true);
235+
}
233236
return success;
234237
}
235238
}

src/missed_spans.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ impl<'a> FmtVisitor<'a> {
5050
self.format_missing_inner(end, |this, last_snippet, _| this.push_str(last_snippet))
5151
}
5252

53+
pub(crate) fn format_missing_ignore_semicolon(&mut self, end: BytePos) {
54+
let missing_snippet = self.snippet(mk_sp(self.last_pos, end));
55+
if missing_snippet.trim() == ";" {
56+
self.last_pos = end;
57+
return;
58+
}
59+
self.format_missing_inner(end, |this, last_snippet, _| this.push_str(last_snippet))
60+
}
61+
5362
pub(crate) fn format_missing_with_indent(&mut self, end: BytePos) {
5463
self.format_missing_indent(end, true)
5564
}

src/rewrite.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub(crate) struct RewriteContext<'a> {
4343
pub(crate) report: FormatReport,
4444
pub(crate) skip_context: SkipContext,
4545
pub(crate) skipped_range: Rc<RefCell<Vec<(usize, usize)>>>,
46+
pub(crate) lazy_static_success: Cell<bool>,
4647
}
4748

4849
pub(crate) struct InsideMacroGuard {

src/visitor.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub(crate) struct FmtVisitor<'a> {
8787
pub(crate) report: FormatReport,
8888
pub(crate) skip_context: SkipContext,
8989
pub(crate) is_macro_def: bool,
90+
pub(crate) lazy_static_success: bool,
9091
}
9192

9293
impl<'a> Drop for FmtVisitor<'a> {
@@ -174,10 +175,15 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
174175
stmt.span(),
175176
get_span_without_attrs(stmt.as_ast_node()),
176177
);
178+
self.format_missing(stmt.span().hi());
177179
} else {
178180
self.visit_mac(&mac_stmt.mac, None, MacroPosition::Statement);
181+
if self.lazy_static_success {
182+
self.format_missing_ignore_semicolon(stmt.span().hi());
183+
} else {
184+
self.format_missing(stmt.span().hi());
185+
}
179186
}
180-
self.format_missing(stmt.span().hi());
181187
}
182188
ast::StmtKind::Empty => (),
183189
}
@@ -792,6 +798,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
792798
skipped_range: Rc::new(RefCell::new(vec![])),
793799
is_macro_def: false,
794800
macro_rewrite_failure: false,
801+
lazy_static_success: false,
795802
report,
796803
skip_context,
797804
}
@@ -995,8 +1002,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
9951002
{
9961003
let context = self.get_context();
9971004
let result = f(&context);
998-
1005+
let buf = context.lazy_static_success.get();
9991006
self.macro_rewrite_failure |= context.macro_rewrite_failure.get();
1007+
self.lazy_static_success = buf;
10001008
result
10011009
}
10021010

@@ -1014,6 +1022,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
10141022
report: self.report.clone(),
10151023
skip_context: self.skip_context.clone(),
10161024
skipped_range: self.skipped_range.clone(),
1025+
lazy_static_success: Cell::new(false),
10171026
}
10181027
}
10191028
}

tests/source/issue-6013/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
lazy_static!(
3+
static ref DYNAMODB_CLIENT: Option<aws_sdk_dynamodb::Client> = None;
4+
static ref CASCADE_IP: String = std::env::var("CASCADE_IP").unwrap_or("127.0.0.1".to_string());
5+
static ref CASCADE_PORT: String = std::env::var("CASCADE_PORT").unwrap_or("4000".to_string());
6+
) ;
7+
}

tests/target/issue-6013/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
lazy_static! {
3+
static ref DYNAMODB_CLIENT: Option<aws_sdk_dynamodb::Client> = None;
4+
static ref CASCADE_IP: String =
5+
std::env::var("CASCADE_IP").unwrap_or("127.0.0.1".to_string());
6+
static ref CASCADE_PORT: String =
7+
std::env::var("CASCADE_PORT").unwrap_or("4000".to_string());
8+
}
9+
}

0 commit comments

Comments
 (0)