Skip to content
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

[unstable option] match_arm_blocks #3373

Open
Tracked by #1895
scampi opened this issue Feb 13, 2019 · 11 comments
Open
Tracked by #1895

[unstable option] match_arm_blocks #3373

scampi opened this issue Feb 13, 2019 · 11 comments
Labels
unstable option tracking issue of an unstable option

Comments

@scampi
Copy link
Contributor

scampi commented Feb 13, 2019

Tracking issue for unstable option: match_arm_blocks

@scampi scampi added the unstable option tracking issue of an unstable option label Feb 13, 2019
@scampi scampi changed the title [unstable option] unstable option: match_arm_blocks [unstable option] match_arm_blocks Feb 18, 2019
@Iron-E
Copy link

Iron-E commented Feb 26, 2021

With the following configuration I can get rustfmt to produce errors:

control_brace_style = 'AlwaysNextLine'
match_arm_blocks = false

It says:

error[internal]: left behind trailing whitespace

The lines it refers to are these, which went over the specified maximum width before formatting:

MatchWhen::HasAll(required_values) =>

	required_values.len() == 1 && required_values.contains(value),

It seems like it leaves a blank line in-between the arm and the expression that's causing the error. I believe it wants to do this:

MatchWhen::HasAll(required_values) =>
	required_values.len() == 1 && required_values.contains(value),

Changing either setting from the sampled TOML config will fix the issue.

@RalfJung
Copy link
Member

Would it be possible to also support Preserve for match_arm_blocks? I think that would be very useful. :)

@calebcartwright
Copy link
Member

Would it be possible to also support Preserve for match_arm_blocks? I think that would be very useful. :)

Sounds quite reasonable to me. We added support for the same on closure bodies fairly recently and of course have opt-in support for preservation of leading pipes on arms so feels rational to support the same here.

At first blush it seems like it should be feasible as well, so I'll open a separate issue later with some implementation pointers and considerations for anyone interested in working on it

@calebcartwright
Copy link
Member

With the following configuration I can get rustfmt to produce errors:

@Iron-E - apologies I missed your post. Could you do me a favor whenever you get a chance and open a new issue with a complete and minimal snippet to reproduce?

@RalfJung
Copy link
Member

RalfJung commented May 18, 2021

Another possibly reasonable value for match_arm_blocks might be Force (Always?), which makes all arms always have a block. I am not sure if this is ever suited "globally", but there are some matches where I think this is better than deciding on a per-arm basis -- so together with #4843 this could be really useful for match formatting.

That said, Force can (hopefully) be emulated with Preserve by just manually putting each arm between braces.

@Iron-E
Copy link

Iron-E commented May 19, 2021

With the following configuration I can get rustfmt to produce errors:

@Iron-E - apologies I missed your post. Could you do me a favor whenever you get a chance and open a new issue with a complete and minimal snippet to reproduce?

I opened #4844 with a minimal reproduction 👍

@gilescope
Copy link

In general I really like this option set to false but when the line above is already indented due to it being too long it's probably best to add the branckets (or not add the newline):

NodeKeyConfig::Ed25519(sc_network::config::Secret::Input(ref ski))
    if node_key_type == NodeKeyType::Ed25519 && &sk[..] == ski.as_ref() =>
    Ok(()),

versus

NodeKeyConfig::Ed25519(sc_network::config::Secret::Input(ref ski))
    if node_key_type == NodeKeyType::Ed25519 && &sk[..] == ski.as_ref() =>
{
    Ok(())
},

Can you see why I feel a little uncomfortable about match_arm_blocks=false in these situations?

@RalfJung
Copy link
Member

Yeah I agree, having braces is strictly better in that case @gilescope.

@Iron-E
Copy link

Iron-E commented Jul 14, 2021

I feel one more indent would look better, while taking up less space:

NodeKeyConfig::Ed25519(sc_network::config::Secret::Input(ref ski))
  if node_key_type == NodeKeyType::Ed25519 && &sk[..] == ski.as_ref() =>
    Ok(()),

@calebcartwright
Copy link
Member

calebcartwright commented Jul 18, 2021

In general I really like this option set to false but when the line above is already indented due to it being too long it's probably best to add the branckets (or not add the newline):

This is more a reflection of a bug in the option that didn't consider the scenario. The currently produced formatting is operating under the assumption of the extra line breaks from the braces which is why the body isn't indented from the guard (as per the Style Guide prescriptions).

We do need to change something here but I don't want to get into a situation where the boolean match_arm_blocks needs a bunch of "well, unless" clauses.

Given the fairly narrow use case the option applies to (non-block required body whose first line can't fit on the same line as the operator) I'd be much more inclined to apply the same indent to the body as the line of the => operator to be consistent with how the rule works in all other scenarios, as @Iron-E suggested above

For cases where folks want more flexibility/control over the braces I think the upcoming Preserve variant for the new option that'll replace match_arm_blocks (refs #4896) would be a better approach

@yhm-amber
Copy link

In general I really like this option set to false but when the line above is already indented due to it being too long it's probably best to add the branckets (or not add the newline):

NodeKeyConfig::Ed25519(sc_network::config::Secret::Input(ref ski))
    if node_key_type == NodeKeyType::Ed25519 && &sk[..] == ski.as_ref() =>
    Ok(()),

versus

NodeKeyConfig::Ed25519(sc_network::config::Secret::Input(ref ski))
    if node_key_type == NodeKeyType::Ed25519 && &sk[..] == ski.as_ref() =>
{
    Ok(())
},

Can you see why I feel a little uncomfortable about match_arm_blocks=false in these situations?

Agree too.

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

No branches or pull requests

6 participants