rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

wrong collapsible else_if suggestion on allman-style block

Open codediebere opened this issue 7 months ago • 10 comments

Description

There are people that are more comfortable with writing code in allman style which prioritizes vertical alignment even though rust prefers that we use the K&R style and rustfmt will probably format your code to that... but in the case that rustfmt is not used, clippy could very much still be used and we want ensure that clippy's suggestion are still as expected. This issue is for the collapsible_else_if lint.

What I tried

let x = "hello";
let y = "world";

if x == "hello" 
{
    println!("outer-if allman style");
}
else
{
    if y == "world"
    {
        println!("inner-if allman style");
    }
}

I expected to see this happen:. It was supposed to suggest the right collapsible else-if block while maintaining structure.

help: collapse nested if block
   |
LL ~     else if y == "world"
LL +     {
LL +         println!("inner-if allman style");
LL +     }
   |

Instead, this happened. The suggestion is not really helpful and it didn't capture the supposed 'collapse'

help: collapse nested if block
   |
LL ~     if y == "world"
LL +     {
LL +         println!("inner-if allman style");
LL +     }
   |

This is what we got for autofix:

if x == "hello" 
{
    println!("outer-if allman style");
}
else
if y == "world"
{
    println!("inner-if allman style");
}

which was actually not a 'collapse'per se

Version

rustc 1.96.0-nightly (7e46c5f6f 2026-04-01)
binary: rustc
commit-hash: 7e46c5f6fb87f8cf4353e058479cef15d1d952b4
commit-date: 2026-04-01
host: x86_64-unknown-linux-gnu
release: 1.96.0-nightly
LLVM version: 22.1.2

Additional Labels

No response

codediebere avatar Apr 15 '26 05:04 codediebere

@rustbot claim

codediebere avatar Apr 15 '26 05:04 codediebere

Hi @teor2345 This was the issue I said I was going to tag you to. The issue is still under the umbrella of 'whitespaces', particularly in the collapse-else if block. I came across it when I was testing out different whitespaces and also, structure of else-if blocks. The solution involves me working on whitespaces. I have been tinkering around this for the past few days and only able to get a solution this morning. So i opened an issue for it and I want to open up a PR to push my fix.

codediebere avatar Apr 15 '26 07:04 codediebere

Feel free to open a PR, but you might want to ask if this is a bug in the clippy Zulip channel first.

I wonder if Clippy suggestions are only designed to work with the default rustfmt style? If they are, your PR might not be accepted. Asking is the fastest way to find out.

teor2345 avatar Apr 15 '26 09:04 teor2345

Thank you very much! I will go ask in the zulip channel now. I think clippy suggestions can work without the default rustfmt style though. For instance in #7318, the code was not default rustfmt style as rustfmt would always put space around "else" like this } else { But yeah... I will go ask now

codediebere avatar Apr 15 '26 10:04 codediebere

I think it's just that the suggestion text was less-than-ideal – as far as I can tell, the fixed version was nevertheless collapsed correctly, i.e. it's syntactically valid.

If your problem is with the fact that the result wasn't formatted according to the allman style: Clippy usually assumes that the input is in the default rustfmt style, and tries to keep the result of a fix formatted in the same style (on a best-effort basis) – therefore it's not surprising that input code with a non-default formatting resulted in code with poor formatting. But I don't think it's valuable (meaning, worth the added complexity) to try to get Clippy to handle this case more correctly – you usually need to run rustfmt after autofixes anyway.

ada4a avatar Apr 15 '26 11:04 ada4a

Oh okay... Thank you so much! I thought it was completely style-agnostic because I earlier came across this #7318 Was hoping to fix it since it had to do with normalizing whitespaces.

codediebere avatar Apr 15 '26 11:04 codediebere

Glad to help:)

#16473

Are you sure that's the right issue number? This one doesn't seem to be related to whitespaces 👀

ada4a avatar Apr 15 '26 11:04 ada4a

lol... sorry I just edted.. Please you can check again. It references an issue with problem arising from non-default style formatting

codediebere avatar Apr 15 '26 11:04 codediebere

I see.

The problem in that case was that non-default formatting of the input resulted in an output which was not just poorly formatted, but syntactically invalid. That is a bug that should be fixed.

But here, non-default formatting of the input just results in a non-default formatting of the output, which is acceptable.

ada4a avatar Apr 15 '26 11:04 ada4a

Okay... this makes a lot of sense.

Thank you so much!

codediebere avatar Apr 15 '26 11:04 codediebere