Skip to content

Issue6246-Let statement should rewrite rhs with comments after equal sign #6282

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
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ tests/cargo-fmt/**/target
.idea/
.vscode/
*~

# Git
.git/
17 changes: 13 additions & 4 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,23 @@ impl Rewrite for ast::Local {
.sub_width(1)
.max_width_error(shape.width, self.span())?;

result = rewrite_assign_rhs(
// should always find a comment span if init exists
let comment_span = context
.snippet_provider
.opt_span_after(self.span, "=")
.map(|op_lo| mk_sp(op_lo, init.span.lo()))
.unwrap();

result = rewrite_assign_rhs_with_comments(
context,
result,
init,
&RhsAssignKind::Expr(&init.kind, init.span),
nested_shape,
)
.max_width_error(shape.width, self.span())?;
&RhsAssignKind::Expr(&init.kind, init.span),
RhsTactics::Default,
comment_span,
true,
)?;

if let Some(block) = else_block {
let else_kw_span = init.span.between(block.span);
Expand Down
42 changes: 42 additions & 0 deletions tests/source/issue-6246.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
fn main() {
let foo =
// 114514
if true {
1919
} else {
810
};
}

fn main() {
let foo =
// line 1
// line 2
if true {
1919
} else {
810
};
}

fn main() {
let foo =
/* line 1
line 2 */
if true {
1919
} else {
810
};
}

// Test a let statement without equal sign
fn main() {
let mut foo ;
// 114514
foo = if true {
1919
} else {
810
};
}
26 changes: 26 additions & 0 deletions tests/target/issue-6246.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
fn main() {
let foo =
// 114514
if true { 1919 } else { 810 };
}

fn main() {
let foo =
// line 1
// line 2
if true { 1919 } else { 810 };
}

fn main() {
let foo =
/* line 1
line 2 */
if true { 1919 } else { 810 };
}

// Test a let statement without equal sign
fn main() {
let mut foo;
// 114514
foo = if true { 1919 } else { 810 };
}
Loading