Skip to content

[clang-tidy] Invalid fixit from cppcoreguidelines-init-variables in the presence of goto #150397

@chrchr-github

Description

@chrchr-github
void g(int);
int f(int i) {
    if (i == -1)
        goto end;
    int x;
    if (i > 1)
        x = 1;
    else
        x = 0;
    g(x);
end:
    return 0;
}
<source>:5:9: warning: variable 'x' is not initialized [cppcoreguidelines-init-variables]
    5 |     int x;
      |         ^
      |           = 0

Applying the fix results in

<source>:4:9: error: cannot jump from this goto statement to its label [clang-diagnostic-error]
    4 |         goto end;
      |         ^
<source>:5:9: note: jump bypasses variable initialization
    5 |     int x{};
      |         ^

https://godbolt.org/z/j43cdjYvo

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