-
Notifications
You must be signed in to change notification settings - Fork 56
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
blocks #21
Comments
Here are things I think need to be covered:
Single line blocks are probably the most interesting aspect. Rustfmt currently only allows these for simple unsafe blocks, e.g.,
IIRC, Rustfmt's definition of simple is:
|
and
|
Empty blocks should be fn foo() {} |
Yeah, +1, Rustfmt currently does |
I am in favor of short and simple ones to be on one line no matter what it contains. Less than 20-25 chars. Empty should definitely be |
+1 for Personally, I would recommend always using one-line blocks as long as 1) the contents are expressions, not statements, and 2) the entire result fits on a line. The moment the line goes over the threshold you feel comfortable with, the block is the very first thing to get line breaks. And any block containing a statement should get newlines and indentation. So, in particular, I'd write: let x = if cond { f() } else { g() };
let y = h(if cond { f() } else { g() });
let z = unsafe { u.field }; |
@joshtriplett I'm specifically talking about bare blocks here, not those as part of
basically and want to prevent it. I'm not sure how I feel about
I'm not actually sure that either of those should ever happen - I don't think there is a use case, but I might be wrong. Oh also, I don't like
where the result is not used - although I'm not actually sure if Rustfmt can know about that. |
@nrc so a block in expression position may be one-line, but one in statement position must not be? I like that, personally. |
@ubsan yes |
@ubsan Exactly. |
Some of the basics which haven't been covered: Non simple blocks should look like:
Avoid comments or attributes on the same lines as the braces. With a let, we indent once:
With these definitions and the sum of discussion in the thread, I think this issue is ready for FCP. |
Should we specify whether spaces are required surrounding the statement in a single line block? i.e. |
I agree that this should be the threshold and in particular there shouldn't be a separate threshold independent of the line length. |
It seems to me we have rough consensus and thus we are ready for a PR here. |
Not a big thing, but I'd actually argue that to be consistent you'd have a space between the set braces if you also have spaces in single-line blocks. E.g. if you have |
|
@jplatte I'm personally okay with either, and can see the arguments for both. I'm not sure we should decide this now, but in it's own issue. |
@jplatte I'd rather be inconsistent and have
|
@ubsan The |
We've changed our process: rather than requiring a full RFC, a PR to close this issue only requires making the changes discussed here to the style guide. You can see more details about the process in the readme for this repo. If you'd like to help with this work, let me know, I'd be very happy to help you help us. |
Simple blocks,
{ ... }
.The text was updated successfully, but these errors were encountered: