Skip to content

False positive [whitespace/indent] "Closing brace should be aligned with beginning of struct X" for multi-line signatures with multiple struct parameters #486

Backlog

Description

@piotrjurkiewicz

Summary

[whitespace/indent] false positive: Closing brace should be aligned with beginning of struct X is reported for the closing brace of a function definition whenever its multi-line parameter list contains two or more parameters of struct type on their own lines. cpplint mistakes the parameter declarations for struct type definitions.

Minimal reproducer

// test.c
static int other_fn(int x) {
    return x;
}

static int foo(const struct bar* a,
               struct context* ctx,
               struct nexthop** selected_nh,
               unsigned int* count,
               unsigned int* table_id) {
    if (a) {
        return 1;
    }
    return 0;
}
$ cpplint --filter=-legal/copyright test.c
test.c:15:  Closing brace should be aligned with beginning of struct context  [whitespace/indent] [3]

Reproduced with cpplint 1.5.5 (Ubuntu 22.04 package) and with current master (1.7.0, cpplint.py as of 2025-12).

Expected behavior

No diagnostic — the closing brace at column 0 correctly terminates the function definition.

Root cause analysis

I traced this through NestingState.Update():

  1. self.stack starts empty at file scope; blocks are pushed only when { is seen.
  2. The parenthesis guard that is supposed to protect parameter lists —
    if (class_decl_match and
        (not self.stack or self.stack[-1].open_parentheses == 0)):
    — never engages here, because open_parentheses is only counted when self.stack is non-empty, and at file scope (before the function body's {) the stack is empty. So every parameter-list line starting with struct X (after optional whitespace) is pushed as a phantom _ClassInfo.
  3. There is a corrective mechanism further down — when a ; or ) token is encountered and the innermost block has not yet seen its opening brace, the phantom is popped as "function arguments with extra class/struct keywords". But it pops only one phantom per ) token, while a parameter list produces one phantom per struct-typed parameter line and has only a single closing ).
  4. With two or more struct parameters, one phantom survives. The function body's { is then attributed to it (seen_open_brace = True), so the phantom is treated as a real struct whose body spans the entire function body, and when the function's closing } at indent 0 is reached, CheckEnd reports the indent mismatch against the phantom's class_indent of 4.

Note this also affects real-world C code formatted with AlignAfterOpenBracket: AlwaysBreak + BinPackParameters: false (one parameter per line), which makes struct-leading parameter lines unavoidable. A single struct parameter escapes because the lone ) pops the only phantom.

Prior art

PR #189 ("Fix issue when function arguments with multiply class or struct keywords", Feb 2022) identified and attempted to fix this exact bug, but was closed unmerged while waiting for a unit test. The analysis above may help whoever picks it up — happy to test any proposed fix against my codebase.

Workaround

} // NOLINT(whitespace/indent) on the affected function's closing brace.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions