Skip to content

False positive Wreturn-type warning when a function either returns or throws #144952

@TiborGY

Description

@TiborGY

Clang does not appear to be able to deduce that a function that always throws is effectively [[noreturn]], not even in seemingly trivial cases. Consider this:

void throwError(const std::string& msg){
    throw std::runtime_error(msg);
}

int ensureZero(const int i){
    if (i == 0) return 0;
    throwError("ERROR");
}

Compiling with -Wall -Wextra -O3 -std=c++2c yields a false positive warning:
warning: non-void function does not return a value in all control paths [-Wreturn-type]

PS. https://godbolt.org/z/hdTb6bGcr

Metadata

Metadata

Assignees

Labels

clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerfalse-positiveWarning fires when it should not

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions