Skip to content

Fix #15277 FP missingReturn: std::unreachable() is not configured as noreturn - #9081

Open
autoantwort wants to merge 1 commit into
cppcheck-opensource:mainfrom
autoantwort:fix-std-unreachable-noreturn
Open

autoantwort wants to merge 1 commit into
cppcheck-opensource:mainfrom
autoantwort:fix-std-unreachable-noreturn

Conversation

@autoantwort

@autoantwort autoantwort commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Trac: https://trac.cppcheck.net/ticket/15277

#include <utility>

int f(int x) {
    if (x == 0)
        return 1;
    std::unreachable();
}
test.cpp:5:17: error: Found an exit path from function with non-void return type that has missing return statement [missingReturn]

Cause: cfg/std.cfg has no entry for std::unreachable() (C++23, [[noreturn]] void unreachable();), so it is treated as a function that returns. gnu.cfg already configures __builtin_unreachable as noreturn.

Fix: configure std::unreachable as noreturn in std.cfg. The new tests in test/cfg/std.cpp (missingReturn and unreachableCode) fail without it.

Verification: make checkcfg and testrunner pass. On the real code file the missingReturn FP is gone.

This PR was created by Claude Code (an AI coding agent) on behalf of @autoantwort.

🤖 Generated with Claude Code

…noreturn

std::unreachable() (C++23) is declared [[noreturn]], but std.cfg had no
entry for it, so a function ending in std::unreachable() was reported
as missing a return statement.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

I found no problems. The new std::unreachable noreturn entry in std.cfg works and matches how std::terminate and std::abort are configured. The new tests in test/cfg/std.cpp are inside #ifdef __cpp_lib_unreachable, but cppcheck still checks them because the cfg tests run with --force. The tests fail without the cfg change and pass with it.

Testing

  • I built cppcheck in pr/ and in base/. Both builds succeeded.
  • cfg test: I ran test/cfg/std.cpp with the same options that test/cfg/runtests.sh uses (--check-library --force --inline-suppr --check-level=exhaustive ...).
    • With the PR's std.cfg there was no output.
    • With the base std.cfg it reports missingReturn at line 120, an unmatched unreachableCode suppression at line 114, and checkLibraryNoReturn/checkLibraryFunction for std::unreachable(). So the new tests do catch a regression.
    • g++ 13 with -std=c++2a does not define __cpp_lib_unreachable, so the compiler syntax check skips the new block. That is harmless.
  • Own test (scratch/t.cpp), using the cppcheck of the PR and the base build:
    #include <utility>
    int f1(int x) { switch (x) { case 0: return 1; default: std::unreachable(); } }
    int f2(int x) { if (x) return 1; ::std::unreachable(); }
    using namespace std;
    int f3(int x) { if (x) return 1; unreachable(); }
    int f4(int x) { if (x) return 1; else std::unreachable(); }
    int f5(int *p) { if (!p) std::unreachable(); return *p; }
    • Base reports missingReturn for f2 and f4, and an inconclusive nullPointerRedundantCheck for f5.
    • The PR reports none of these, and adds no new warnings for this file.
    • --check-library reports nothing for std::unreachable any more.
  • cfg/std.cfg is still well-formed XML. I could not validate it against the RelaxNG schema because xmllint is not installed. I did not run testrunner, because the PR only changes a cfg file and a cfg test.

Automated review of 0ec2f28 by Claude. The pull request was built and tested locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants