Skip to content

clang 17.0.2: No "unreachable code" warning if source file included in another source file #71046

@pkl97

Description

@pkl97

When compiling unreachable_warning.cpp (with the content below) using option -Wunreachable-code-break, a warning is triggered as expected:

void f(int i)
{
    switch (i)
    {
        case 3: return; break;
    }
}

The warning emitted:

[user@host]$ clang++ -c -Wunreachable-code-break unreachable_warning.cpp
unreachable_warning.cpp:5:25: warning: 'break' will never be executed [-Wunreachable-code-break]
        case 3: return; break;
                        ^~~~~
1 warning generated.

However if unreachable_warning.cpp is wrapped in another source file (as is frequently done in "unity builds" or "jumbo builds") then the warning is no longer emitted:

[user@host]$ cat unity_build.cpp
#include "unreachable_warning.cpp"

[user@host]$ clang++ -c -Wunreachable-code-break unity_build.cpp
[user@host]$

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerinvalidResolved as invalid, i.e. not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions