Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 34333ba

Browse files
authored
Merge pull request rust-lang#3189 from scampi/issue3032
fix logic for adding or not a newline after a missed span
2 parents 4e2f741 + d121d72 commit 34333ba

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

src/missed_spans.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,16 @@ impl<'a> FmtVisitor<'a> {
259259
status.last_wspace = None;
260260
status.line_start = offset + subslice.len();
261261

262-
if let Some('/') = subslice.chars().nth(1) {
263-
// Only add a newline if the last line is a line comment
264-
if !subslice.trim_end().ends_with("*/") {
265-
self.push_str("\n");
266-
}
267-
} else if status.line_start <= snippet.len() {
268-
// For other comments add a newline if there isn't one at the end already
262+
// Add a newline:
263+
// - if there isn't one already
264+
// - otherwise, only if the last line is a line comment
265+
if status.line_start <= snippet.len() {
269266
match snippet[status.line_start..].chars().next() {
270-
Some('\n') | Some('\r') => (),
267+
Some('\n') | Some('\r') => {
268+
if !subslice.trim_end().ends_with("*/") {
269+
self.push_str("\n");
270+
}
271+
}
271272
_ => self.push_str("\n"),
272273
}
273274
}

src/visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
236236
}
237237
}
238238

239-
let unindent_comment = (self.is_if_else_block && !b.stmts.is_empty()) && {
239+
let unindent_comment = self.is_if_else_block && !b.stmts.is_empty() && {
240240
let end_pos = source!(self, b.span).hi() - brace_compensation - remove_len;
241241
let snippet = self.snippet(mk_sp(self.last_pos, end_pos));
242242
snippet.contains("//") || snippet.contains("/*")

tests/target/issue-3032.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
pub fn get_array_index_from_id(_cx: *mut JSContext, id: HandleId) -> Option<u32> {
2+
let raw_id = id.into();
3+
unsafe {
4+
if RUST_JSID_IS_INT(raw_id) {
5+
return Some(RUST_JSID_TO_INT(raw_id) as u32);
6+
}
7+
None
8+
}
9+
// if id is length atom, -1, otherwise
10+
/*return if JSID_IS_ATOM(id) {
11+
let atom = JSID_TO_ATOM(id);
12+
//let s = *GetAtomChars(id);
13+
if s > 'a' && s < 'z' {
14+
return -1;
15+
}
16+
17+
let i = 0;
18+
let str = AtomToLinearString(JSID_TO_ATOM(id));
19+
return if StringIsArray(str, &mut i) != 0 { i } else { -1 }
20+
} else {
21+
IdToInt32(cx, id);
22+
}*/
23+
}
24+
25+
impl Foo {
26+
fn bar() -> usize {
27+
42
28+
/* a block comment */
29+
}
30+
31+
fn baz() -> usize {
32+
42
33+
// this is a line
34+
/* a block comment */
35+
}
36+
}

0 commit comments

Comments
 (0)