Skip to content

clang-format: IndentAccessModifiers causes an extra indent to be added in struct defined inside class declaration #61631

Open
@cquick01

Description

@cquick01

So this might just be me doing something wrong, but it seems like Clang-Format (15.0.6) is adding an extra indent inside my struct

Using .clang-format

---
BasedOnStyle: LLVM
BreakBeforeBraces: Allman
IndentWidth: 4
IndentAccessModifiers: true

I have a class that looks like this

// this is my desired formatting
class MyClass
{
    public:
        MyClass();
        ~MyClass();

        struct MyStruct
        {
            bool first;
            bool second;
        };
};

And when trying to format with Clang-Format, it gets formatted like this with an extra indent before each variable within MyStruct.

class MyClass
{
    public:
        MyClass();
        ~MyClass();

        struct MyStruct
        {
                bool first;
                bool second;
        };
};

If I change to

---
BasedOnStyle: LLVM
BreakBeforeBraces: Allman
IndentWidth: 4
AccessModifierOffset: -4
IndentAccessModifiers: false

it looks a bit better, but I'd rather the access modifier block was indented like the original

class MyClass
{
public:
    MyClass();
    ~MyClass();

    struct MyStruct
    {
        bool first;
        bool second;
    };
};

Trying to set both IndentAccessModifiers: true and AccessModifierOffset: -4 still looks like

class MyClass
{
    public:
        MyClass();
        ~MyClass();

        struct MyStruct
        {
                bool first;
                bool second;
        };
};

Can anyone help fix my configuration to achieve my desired formatting? Thanks in advance.


Edit: So omitting IndentAccessModifiers and AccessModifierOffset gives this, which is a bit closer

class MyClass
{
  public:
    MyClass();
    ~MyClass();

    struct MyStruct
    {
        bool first;
        bool second;
    };
};

but is it possible to adjust the indent size of the access modifiers as shown?

class MyClass
{
  public:
^^  // adjust 2 character indent here?
    MyClass();
  ^^ // and here?
    ~MyClass();

    struct MyStruct
    {
        bool first;
        bool second;
    };
};

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