Search code examples
c++visual-studio-codeclangcoding-styleclang-format

How to make clang-format not add new line before opening brace of a function with requires clause?


When I auto-format with Clang-format, it is always inserting a newline before brace when requires clause is there.

This

constexpr auto size() const noexcept
  requires(!Unique) {
    return stdr::fold_left(views | stdv::transform(stdr::size), 0, std::plus<int>{});
}

is becoming this

constexpr auto size() const noexcept
  requires(!Unique)
{
    return stdr::fold_left(views | stdv::transform(stdr::size), 0, std::plus<int>{});
}

How to avoid it?

I have tried

RequiresClausePosition: WithPreceding
BraceWrapping:
  AfterClass:      false
  AfterControlStatement: false
  AfterEnum:       false
  AfterFunction:   false
  AfterNamespace:  false
  AfterObjCDeclaration: false
  AfterStruct:     false
  AfterUnion:      false
  BeforeCatch:     false
  BeforeElse:      false
  IndentBraces:    false
BreakBeforeBraces: Custom

but these are not working.


Solution

  • It looks like you want RequiresClausePosition: SingleLine:

    RequiresClausePosition: SingleLine
    

    Try to put everything in the same line if possible. Otherwise normal line breaking rules take over.

    Example with ColumnLimit: 80:

    constexpr auto size() const noexcept requires(!Unique) {
        return stdr::fold_left(views | stdv::transform(stdr::size), 0,
                               std::plus<int>{});
    }