Skip to content

[Clang] -Wreturn-type false negative with switch statement on enum #115345

@bjacob

Description

@bjacob

In this compiler explorer experiment, GCC warns "warning: control reaches end of non-void function", while Clang does not. Pasting it here for self-containedness:

enum class B : int {
    B1, B2
};

int f(B b) {
    switch(b) {
        case B::B1: return 1;
        case B::B2: return 2;
    }
}

In this instance, GCC is making an important point: the function f cannot assume that its parameter of enum type has a value that is one of the enumerator values, because this enumerator has a a fixed underlying type (the : int in the enum declaration) and the standard says that in that case, any integer of that underlying type may be casted to this enum type: https://eel.is/c++draft/expr.static.cast#9

Currently, GCC always warns regardless of having a specified underlying type in the enum declaration ; Clang never warns. If my understanding of the spec is correct then the reachability of the end of the function (and therefore, the rationale for generating a warning) is when there is no specified underlying type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerconfirmedVerified by a second partydiverges-from:gccDoes the clang frontend diverge from gcc on this issuefalse-negativeWarning doesn't fire when it should

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions