Skip to content

Backport PR 3857 and 4550 #5277

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,16 @@ mod test {
check("\"/* abc", "abc", Some(4));
}

#[test]
fn test_find_last_uncommented() {
fn check(haystack: &str, needle: &str, expected: Option<usize>) {
assert_eq!(expected, haystack.find_last_uncommented(needle));
}
check("foo test bar test", "test", Some(13));
check("test,", "test", Some(0));
check("nothing", "test", None);
}

#[test]
fn test_filter_normal_code() {
let s = r#"
Expand Down
48 changes: 44 additions & 4 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,57 @@ impl Rewrite for ast::Local {
result.push_str(&infix);

if let Some((init, _els)) = self.kind.init_else_opt() {
let base_span = if let Some(ref ty) = self.ty {
mk_sp(ty.span.hi(), self.span.hi())
} else {
mk_sp(self.pat.span.hi(), self.span.hi())
};

let offset = context.snippet(base_span).find_uncommented("=")?;
let base_span_lo = base_span.lo();

let assign_lo = base_span_lo + BytePos(offset as u32);
let comment_start_pos = if let Some(ref ty) = self.ty {
ty.span.hi()
} else {
self.pat.span.hi()
};
let comment_before_assign = context.snippet(mk_sp(comment_start_pos, assign_lo)).trim();

let assign_hi = base_span_lo + BytePos((offset + 1) as u32);
let rhs_span_lo = init.span.lo();
let comment_end_pos = if init.attrs.is_empty() {
rhs_span_lo
} else {
let attr_span_lo = init.attrs.first().unwrap().span.lo();
// for the case using block
// ex. let x = { #![my_attr]do_something(); }
if rhs_span_lo < attr_span_lo {
rhs_span_lo
} else {
attr_span_lo
}
};

if !comment_before_assign.is_empty() {
let new_indent_str = &pat_shape
.block_indent(0)
.to_string_with_newline(context.config);
result = format!("{}{}{}", comment_before_assign, new_indent_str, result);
}

// 1 = trailing semicolon;
let nested_shape = shape.sub_width(1)?;

result = rewrite_assign_rhs(
result = rewrite_assign_rhs_with_comments(
context,
result,
init,
&RhsAssignKind::Expr(&init.kind, init.span),
nested_shape,
&RhsAssignKind::Expr(&init.kind, init.span),
RhsTactics::Default,
mk_sp(assign_hi, comment_end_pos),
true,
)?;
// todo else
}

result.push(';');
Expand Down
22 changes: 22 additions & 0 deletions tests/source/issue-3851.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
fn main() {
let before
// before_comment
=
if true {
1.0
};

let after =
// after_comment
if true {
1.0
};

let both
// before_comment
=
// after_comment
if true {
1.0
};
}
5 changes: 5 additions & 0 deletions tests/source/issue-4502.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let after =
// after_comment
1.0;
}
10 changes: 10 additions & 0 deletions tests/source/issue_4515.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub unsafe fn as_chunks_mut_unchecked<const N: usize>(&mut self) -> &mut [[T; N]] {
debug_assert_ne!(N, 0);
debug_assert_eq!(self.len() % N, 0);
let new_len =
// SAFETY: Our precondition is exactly what's needed to call this
unsafe { crate::intrinsics::exact_div(self.len(), N) };
// SAFETY: We cast a slice of `new_len * N` elements into
// a slice of `new_len` many `N` elements chunks.
unsafe { from_raw_parts_mut(self.as_mut_ptr().cast(), new_len) }
}
5 changes: 5 additions & 0 deletions tests/source/issue_5017.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let number =
// A comment
1;
}
19 changes: 19 additions & 0 deletions tests/target/issue-3851.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
fn main() {
// before_comment
let before = if true {
1.0
};

let after =
// after_comment
if true {
1.0
};

// before_comment
let both =
// after_comment
if true {
1.0
};
}
5 changes: 5 additions & 0 deletions tests/target/issue-4502.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let after =
// after_comment
1.0;
}
10 changes: 10 additions & 0 deletions tests/target/issue_4515.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub unsafe fn as_chunks_mut_unchecked<const N: usize>(&mut self) -> &mut [[T; N]] {
debug_assert_ne!(N, 0);
debug_assert_eq!(self.len() % N, 0);
let new_len =
// SAFETY: Our precondition is exactly what's needed to call this
unsafe { crate::intrinsics::exact_div(self.len(), N) };
// SAFETY: We cast a slice of `new_len * N` elements into
// a slice of `new_len` many `N` elements chunks.
unsafe { from_raw_parts_mut(self.as_mut_ptr().cast(), new_len) }
}
5 changes: 5 additions & 0 deletions tests/target/issue_5017.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let number =
// A comment
1;
}