llvm / llvm/llvm-project

Clang-format : Allow multiple lines for chained ternary operators

Open
#167,357 2 comments 3 reactions 0 assignees View on GitHub
clang-format
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

This is a feature proposal for clang-format.

I would like to have clang-format be able to allow chained ternary operators in a multiline format. This format is incredibly useful for conditional assignments like checking for the bounds of a variable.

Current Behavior:
```
int bounded = (value > max) ? max : (value < min) ? min : value;
```

Preferred:
```
int bounded = (value > max) ? max :
(value < min) ? min :
value;
```

Acceptable:
```
int bounded = (value > max) ? max :
(value < min) ? min : value;
```

This behavior would require adding a line break after the colon of a ternary operator if the right-hand operand is another ternary operator. If this condition is met, I'd prefer to have a line break after all colons in the chain which would include the final value. I realize that would complicate the rule, and it would be acceptable if it was not a part of the feature.

Thank you for your consideration!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.