Skip to content

[clang-format] Aligning assignments gives incorrect function argument alignment #53150

@petterssonandreas

Description

@petterssonandreas

I noticed a problem (or unexpected behaviour) with clang-format-13 regarding alignment of assignments in combination with indentation/alignment of function arguments.

Minimal .clang-format:

AlignConsecutiveAssignments: Consecutive
AllowAllArgumentsOnNextLine: false
BinPackArguments: false
ColumnLimit: 100
IndentWidth: 4

Example code before formatting:

void my_function() {
    // ... more code

    int my_long_variable_name_with_init = 1;
    int ret_code = some_function(first_long_argument, second_long_argument, third_long_argument, fourth_long_argument);

    // ... more code
}

After formatting:

void my_function() {
    // ... more code

    int my_long_variable_name_with_init = 1;
    int ret_code                        = some_function(first_long_argument,
                                 second_long_argument,
                                 third_long_argument,
                                 fourth_long_argument);

    // ... more code
}

As you can see, the indentation of second_long_argument and the following is bad. If we didn't have the alignment of assignments it would have been correct, being at the level of the open bracket. I would have expected the following output:

void my_function() {
    // ... more code

    int my_long_variable_name_with_init = 1;
    int ret_code                        = some_function(first_long_argument,
                                                        second_long_argument,
                                                        third_long_argument,
                                                        fourth_long_argument);

    // ... more code
}

Even if I think that looks quite ugly, it's better than what I get currently, and also it's "correct" given the formatting rules provided.

Is this a known problem? Or is this actually expected output?

clang-format version: Ubuntu clang-format version 13.0.1-++20220108083114+bfb1bd1b9906-1~exp1~20220108083151.49

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions