Skip to content

Fix short-if formatting #2778

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 6 commits into from
Closed

Fix short-if formatting #2778

wants to merge 6 commits into from

Conversation

csmoe
Copy link
Member

@csmoe csmoe commented Jun 9, 2018

Cloese #2777

@csmoe
Copy link
Member Author

csmoe commented Jun 10, 2018

@nrc how can I auto-update the target files formatting when making some changes in rustfmt codebase? I used to update them with cargo run --bin rustfmt xxx.rs, but when the formatting is controlled by config, this won't work.

Copy link
Contributor

@topecongiro topecongiro left a comment

Choose a reason for hiding this comment

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

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

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 {} }
Copy link
Contributor

Choose a reason for hiding this comment

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

This should not happen, we never put a block statement in a single line.

}
}
}
{ { { {} } } }
Copy link
Contributor

Choose a reason for hiding this comment

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

So is this one.

{
"inner-block"
}
{ "inner-block" }
Copy link
Contributor

Choose a reason for hiding this comment

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

And this one too.

@nrc
Copy link
Member

nrc commented Jun 11, 2018

how can I auto-update the target files formatting when making some changes in rustfmt codebase?

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 csmoe force-pushed the if branch 4 times, most recently from 20aec94 to c766393 Compare July 2, 2018 09:44
@csmoe
Copy link
Member Author

csmoe commented Jul 14, 2018

@topecongiro ping for review.

Copy link
Contributor

@topecongiro topecongiro left a comment

Choose a reason for hiding this comment

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

Thank you for the update! Though the current implementation is not correct yet: we only want to put an if statement on a single line only if it is the last statement.

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() }
}

src/expr.rs Outdated
@@ -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 ") {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bit hacky - you can inspect the ast::ExprKind to see whether the given expression is if or not.

@petrochenkov
Copy link
Contributor

@topecongiro @csmoe
Any way to move this forward / prioritize?

@topecongiro
Copy link
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants