Skip to content

Clang-format : Allow multiple lines for chained ternary operators #167357

@theyyg

Description

@theyyg

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!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions