Skip to content

Missing warning if variable is used in else clause of if (type_t var = ...) #145506

@Alcaro

Description

@Alcaro
int* get_something();
int* get_something_else();
int square() {
    if (int* ptr1 = get_something())
        return ptr1[0] * ptr1[0];
    else if (int* ptr2 = get_something_else())
        return ptr1[0] * ptr2[0]; // whoops, typoed!
    else
        return -1;
}

Expected: Compiler warning about how ptr1 is always null on the commented line. (Simplifying the example further returns an unused-variable warning for ptr2; that's not what I'm asking for.)

Actual: No output, just pretends ptr1[0] is 0 and optimizes accordingly. (Ideally, ptr1 would only be in scope in its then-clause, not throughout the entire if-statement, but that's not how the C++ spec looks.)

https://godbolt.org/z/118vGjE43

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerundefined behaviour

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions