[clang] [clang-format] Fix the indent of the ternary operator when AlignOperands and BreakBeforeTernaryOperators is specified. (PR #100860)

John Ericson via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 2 13:44:51 PST 2025


Ericson2314 wrote:

In Nix (github.com/nixos/nix) I am getting formatting like 

```c++
        auto variant = scheme == "auto" ? (StoreReference::Variant{Specified::Auto{}})
                                        : (StoreReference::Variant{Specified::Specified{
                                              .scheme = getString,
                                              .authority = getString(valueAt(obj, "authority")),
                                          }});
```

and I am not really sure why, but I gather it might have something to do with this.

IMO a key rule of formatting to me is that it should not depend on the the exact lengths of identifiers and literals. Making the `:` in match `?` when `?` like this is a no-go because the horizontal position of `?` matches `variant`, `scheme`, and `"auto"`. Whereas something like
```c++
        auto variant = scheme == "auto"
                ? (StoreReference::Variant{Specified::Auto{}})
                : (StoreReference::Variant{Specified::Specified{
                    .scheme = getString,
                    .authority = getString(valueAt(obj, "authority")),
                }});
```
doesn't depend on those things, and so is a a valid choice.

I would hope there would at least be some combination of settings to enforce this invariant.

https://github.com/llvm/llvm-project/pull/100860


More information about the cfe-commits mailing list