0

The answer of the related post how to make clang-format add new line before opening brace of a function? does not help.

I am using clang-format 9.0.0 with Cppstyle in Eclipse CDT on Windows. clang-format formats the following getter like this:

int returnNumber() { return 3; }

but I prefer the format

int returnNumber()
{
    return 3;
}

I have not been able to make clang-format do that, neither with breaking style BS_Allman nor with a custom style. Is there another solution than manual formatting?

My example source file looks like this:

Header.h

#pragma once

namespace Test
{

class MyClass
{
public:
    int returnNumber() { return 3; }
};

} /* namespace Test */

And my configuration file looks like this:

Language: Cpp

AlwaysBreakTemplateDeclarations: 'true'

BreakBeforeBraces: Allman

ColumnLimit: '80'

IndentWidth: '2'

NamespaceIndentation: None

Standard: Cpp11

TabWidth: '2'

UseTab: Always

PointerAlignment: Left

AlignAfterOpenBracket: DontAlign

BreakConstructorInitializers: AfterColon

MaxEmptyLinesToKeep: 2
Benjamin Bihler
  • 1,612
  • 11
  • 32

1 Answers1

1

The problem with your config is that you missed this option that controls the behavior of clang-format with short function.

Add this to your config and eveything will be good:

AllowShortFunctionsOnASingleLine: None

Quoting from the clang-format documentation:

AllowShortFunctionsOnASingleLine (ShortFunctionStyle)

Dependent on the value, int f() { return 0; } can be put on a single line.

Possible values:

  • SFS_None (in configuration: None) Never merge functions into a single line.

  • SFS_InlineOnly (in configuration: InlineOnly) Only merge functions defined inside a class. Same as “inline”, except it does not implies “empty”: i.e. top level empty functions are not merged either.

  • SFS_Empty (in configuration: Empty) Only merge empty functions.

  • SFS_Inline (in configuration: Inline) Only merge functions defined inside a class. Implies “empty”.

  • SFS_All (in configuration: All) Merge all functions fitting on a single line.

Davide Spataro
  • 7,319
  • 1
  • 24
  • 36
  • For some reason `AllowShortFunctionsOnASingleLine: None` doesn't do anything. I'm afraid it conflicts with maybe another option. But I'm really lost now. – Melroy van den Berg Jan 02 '22 at 00:47