Skip to content

Disable unreachable-code diagnostic on speficic constants/comparison #241751

Description

@fekir

I have following pattern

// generated at configure time
// depends on platform/compiler flags
enum class platform{
    windows, debian, bsd, android, ...
    current = windows,
};


void foo();
void bar();

void baz1(bool condition){
    if(platform::current != platform::windows && condition){
        foo();
    } else if (!condition) {
        bar();
    }
}

but with -Wunreachable-code, clang diagnoses

<source>:16:9: warning: code will never be executed [-Wunreachable-code]
   16 |         foo();
      |         ^~~
<source>:15:50: warning: code will never be executed [-Wunreachable-code]
   15 |     if(platform::current != platform::windows && condition){
      |                                                  ^~~~~~~~~

Note that (currently) the warning is not triggered for a switch statement, but transforming an if to a switch is error-prone, and does in general not improve the legibility.

void baz2(bool condition){
    switch(platform::current){
        case platform::windows: if(!condition) bar(); break;
        default: if(condition) foo(); else bar(); break;
    }
}

It is possible to suppress the warning locally with a pragma, but adding such pragmato every if-statement is repetitive and error-prone.

Is there a way to tell clang that the constant value platform::current depends on the compiler settings, and branches that depend on it are not necessarily dead?

live example: https://compiler-explorer.com/z/Y7455nzYW

Activity

  1. added
    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"
    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer
    on Nov 12, 2026
  2. llvmorg-github-actions commented on Nov 12, 2026

    @llvmorg-github-actions

    @llvm/issue-subscribers-clang-frontend

    Author: Federico Kircheis (fekir)

    Details I have following pattern
    // generated at configure time
    // depends on platform/compiler flags
    enum class platform{
        windows, debian, bsd, android, ...
        current = windows,
    };
    
    
    void foo();
    void bar();
    
    void baz1(bool condition){
        if(platform::current != platform::windows &amp;&amp; condition){
            foo();
        } else if (!condition) {
            bar();
        }
    }

    but with -Wunreachable-code, clang diagnoses

    &lt;source&gt;:16:9: warning: code will never be executed [-Wunreachable-code]
       16 |         foo();
          |         ^~~
    &lt;source&gt;:15:50: warning: code will never be executed [-Wunreachable-code]
       15 |     if(platform::current != platform::windows &amp;&amp; condition){
          |                                                  ^~~~~~~~~
    

    Note that (currently) the warning is not triggered for a switch statement, but transforming an if to a switch is error-prone, and does in general not improve the legibility.

    void baz2(bool condition){
        switch(platform::current){
            case platform::windows: if(!condition) bar(); break;
            default: if(condition) foo(); else bar(); break;
        }
    }

    It is possible to suppress the warning locally with a pragma, but adding such pragmato every if-statement is repetitive and error-prone.

    Is there a way to tell clang that the constant value platform::current depends on the compiler settings, and branches that depend on it are not necessarily dead?

    live example: https://compiler-explorer.com/z/Y7455nzYW

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

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions