Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upFix short-if formatting #2778
Conversation
csmoe
force-pushed the
csmoe:if
branch
from
e469bb4
to
b31cfdc
Jun 9, 2018
This comment has been minimized.
This comment has been minimized.
|
@nrc how can I auto-update the target files formatting when making some changes in rustfmt codebase? I used to update them with |
topecongiro
requested changes
Jun 11, 2018
|
Thanks for the PR! |
| @@ -708,7 +708,7 @@ impl Rewrite for ast::Stmt { | |||
| }; | |||
|
|
|||
| let shape = shape.sub_width(suffix.len())?; | |||
| format_expr(ex, ExprType::Statement, context, shape).map(|s| s + suffix) | |||
This comment has been minimized.
This comment has been minimized.
topecongiro
Jun 11, 2018
Collaborator
I think that this is incorrect. We should only put the if expression in a single line if it is the last statement of the block.
| { | ||
| for _ in 0..10 {} | ||
| } | ||
| { for _ in 0..10 {} } |
This comment has been minimized.
This comment has been minimized.
topecongiro
Jun 11, 2018
Collaborator
This should not happen, we never put a block statement in a single line.
| { | ||
| "inner-block" | ||
| } | ||
| { "inner-block" } |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There's no good way to do this, sorry. If all the tests have the same config, then you can just create a rustfmt.toml and put it in some surrounding directory, then run rustfmt. Otherwise you have to do things manually. |
csmoe
force-pushed the
csmoe:if
branch
4 times, most recently
from
20aec94
to
c766393
Jul 2, 2018
This comment has been minimized.
This comment has been minimized.
|
@topecongiro ping for review. |
topecongiro
requested changes
Jul 14, 2018
|
Thank you for the update! Though the current implementation is not correct yet: we only want to put an So for example, fn f() {
if x { foo() } else { bar() }
if x { foo() } else { bar() }
}should become fn f() {
if x {
foo()
} else {
bar()
}
if x { foo() } else { bar() }
} |
| @@ -710,7 +710,13 @@ impl Rewrite for ast::Stmt { | |||
| }; | |||
|
|
|||
| let shape = shape.sub_width(suffix.len())?; | |||
| format_expr(ex, ExprType::Statement, context, shape).map(|s| s + suffix) | |||
| let expr_type = | |||
| if stmt_is_expr(&self) && context.snippet(self.span).starts_with("if ") { | |||
This comment has been minimized.
This comment has been minimized.
topecongiro
Jul 14, 2018
Collaborator
This is a bit hacky - you can inspect the ast::ExprKind to see whether the given expression is if or not.
csmoe
added some commits
Jun 9, 2018
csmoe
force-pushed the
csmoe:if
branch
from
c766393
to
d157383
Jul 15, 2018
csmoe
force-pushed the
csmoe:if
branch
from
d157383
to
820e212
Jul 15, 2018
This comment has been minimized.
This comment has been minimized.
|
@topecongiro @csmoe |
This comment has been minimized.
This comment has been minimized.
|
@csmoe @petrochenkov My apologies for the late reply. I missed the notification of new commits being pushed to this PR. Since this could not get merged before 1.0 release, the formatting change needs to be version gated. Also we need to fix the merge conflicts. I think in the current form getting these done are a bit tough, so I would rather fix the issue in a separate PR. @csmoe Again, I am very sorry that I could not review the PR in a timely manner. I really appreciate your contribution to rustfmt. |
csmoe commentedJun 9, 2018
Cloese #2777