Skip to content

Issue4392 resolution - No new line after operation #4397

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

Closed
wants to merge 3 commits into from
Closed
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: 9 additions & 1 deletion src/formatting/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,15 @@ pub(crate) fn recover_comment_removed(
let snippet = context.snippet(span);
let includes_comment = contains_comment(snippet);
if snippet != new && includes_comment && changed_comment_content(snippet, &new) {
Some(snippet.to_owned())
/* Trim white spaces at end of lines */
let mut c = String::from("");
for line in snippet.to_owned().split('\n') {
if c != "" {
c.push('\n')
};
c.push_str(line.trim_end());
}
Some(c.to_owned())
Comment on lines +1607 to +1615
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've got these same changes in three separate PRs. Please keep various commits and changes separate for easier review and potential for merging.

As I noted in #4391 (comment), I do not believe this is the right approach to address the related issue.

} else {
Some(new)
}
Expand Down
23 changes: 22 additions & 1 deletion src/formatting/pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ fn rewrite_pairs_multiline<T: Rewrite>(

result.push_str(&list.list[0].1.as_ref()?);

/* Determine the expression kind */
let (e, _s) = match list.list.first() {
Some(x) => x,
None => return None,
};
let list_len = list.list.len();
/* ???? Is there better way than transmute() ???? */
let list_expr = unsafe { std::mem::transmute::<&T, &ast::Expr>(e) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted on the other thread, there's a root problem with comments between the subexpr and type in cast expressions which is most certainly impacting the formatting you are seeing with your snippets.

We really need to focus on the addressing the core issue instead of associated items, especially an approach like this as there are currently no unsafe blocks within the rustfmt source to my knowledge.

let list_expr_lit = match list_expr.kind {
ast::ExprKind::Lit(_) => true,
_ => false,
};

for ((e, default_rw), s) in list.list[1..].iter().zip(list.separators.iter()) {
// The following test checks if we should keep two subexprs on the same
// line. We do this if not doing so would create an orphan and there is
Expand All @@ -125,7 +138,15 @@ fn rewrite_pairs_multiline<T: Rewrite>(
} else {
shape.used_width()
};
if last_line_width(&result) + offset <= nested_shape.used_width() {

/* Do not force adding add new line between Lit operation and righ operand */
let w = if list_len > 2 || !list_expr_lit {
nested_shape.used_width()
} else {
nested_shape.width
};

if last_line_width(&result) + offset <= w {
// We must snuggle the next line onto the previous line to avoid an orphan.
if let Some(line_shape) =
shape.offset_left(s.len() + 2 + trimmed_last_line_width(&result))
Expand Down
47 changes: 47 additions & 0 deletions tests/source/issue-4392.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* No new line should be added after the operation ('==', etc.) */

fn main() {
if 'a' == b /* x */
as char {}
}

fn main() {
if 'a' == 0
/* x */ as char {} }

fn main() {
if 1234 == 0
/* x */ & cc & dd {} }

fn main() {
if 1234 == 'a'
/* x */ as i32 {} }

fn main() {
if 0 == 'a'
/* x */ as i32 {} }

fn main() {
if 'a' == b /* x */ as char { println!("Match"); }
}

fn main() {
while aaaaaa < bbbbb
as char /* x */ {} }

fn main() {
if 'a' == b // x
as char {}
}

fn main() {
if 'a' == /* x */ b
as char {}
}

fn main() {
if 'a' == b
as char {}

let a = 5; // inline
}