[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
llvm-ci wrote: LLVM Buildbot has detected a new failure on builder `clang-ppc64le-linux-test-suite` running on `ppc64le-clang-test-suite` while building `clang-tools-extra` at step 6 "test-build-unified-tree-check-all". Full details are available at: https://lab.llvm.org/buildbot/#/builders/95/builds/21447 Here is the relevant piece of the build log for the reference ``` Step 6 (test-build-unified-tree-check-all) failure: 1200 seconds without output running [b'ninja', b'check-all'], attempting to kill ... PASS: LLVM :: CodeGen/AMDGPU/directive-amdgcn-target.ll (112116 of 112126) PASS: LLVM :: tools/llvm-objcopy/ELF/cross-arch-headers.test (112117 of 112126) PASS: LLVM :: CodeGen/AMDGPU/elf-header-flags-mach.ll (112118 of 112126) PASS: Clang :: Driver/x86-target-features.c (112119 of 112126) PASS: Clang :: Driver/arm-cortex-cpus-1.c (112120 of 112126) PASS: LLVM :: tools/llvm-readobj/ELF/AMDGPU/elf-headers.test (112121 of 112126) PASS: Clang :: Driver/arm-cortex-cpus-2.c (112122 of 112126) PASS: ScudoStandalone-Unit :: ./ScudoUnitTest-powerpc64le-Test/101/387 (112123 of 112126) PASS: ScudoStandalone-Unit :: ./ScudoCxxUnitTest-powerpc64le-Test/0/3 (112124 of 112126) PASS: ScudoStandalone-Unit :: ./ScudoUnitTest-powerpc64le-Test/110/387 (112125 of 112126) command timed out: 1200 seconds without output running [b'ninja', b'check-all'], attempting to kill process killed by signal 9 program finished with exit code -1 elapsedTime=9712.483735 ``` https://github.com/llvm/llvm-project/pull/198237 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
https://github.com/zwuis closed https://github.com/llvm/llvm-project/pull/198237 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
https://github.com/zwuis auto_merge_enabled https://github.com/llvm/llvm-project/pull/198237 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
https://github.com/zwuis updated
https://github.com/llvm/llvm-project/pull/198237
>From 0445e94537811e0943838124bf810e2f22c647d4 Mon Sep 17 00:00:00 2001
From: Yanzuo Liu
Date: Mon, 18 May 2026 14:15:05 +0800
Subject: [PATCH 1/2] Move part of bugprone-unhandled-code-paths to a new check
readability-trivial-switch
---
.../bugprone/UnhandledCodePathsCheck.cpp | 43 ++-
.../bugprone/UnhandledCodePathsCheck.h| 1 -
.../clang-tidy/readability/CMakeLists.txt | 1 +
.../readability/ReadabilityTidyModule.cpp | 3 ++
.../readability/TrivialSwitchCheck.cpp| 48 +
.../readability/TrivialSwitchCheck.h | 33
clang-tools-extra/docs/ReleaseNotes.rst | 6 ++-
.../checks/bugprone/unhandled-code-paths.rst | 33
.../docs/clang-tidy/checks/list.rst | 1 +
.../checks/readability/trivial-switch.rst | 36 +
.../bugprone/unhandled-code-paths.cpp | 52 ++-
.../checkers/readability/trivial-switch.cpp | 44
12 files changed, 178 insertions(+), 123 deletions(-)
create mode 100644
clang-tools-extra/clang-tidy/readability/TrivialSwitchCheck.cpp
create mode 100644
clang-tools-extra/clang-tidy/readability/TrivialSwitchCheck.h
create mode 100644
clang-tools-extra/docs/clang-tidy/checks/readability/trivial-switch.rst
create mode 100644
clang-tools-extra/test/clang-tidy/checkers/readability/trivial-switch.cpp
diff --git a/clang-tools-extra/clang-tidy/bugprone/UnhandledCodePathsCheck.cpp
b/clang-tools-extra/clang-tidy/bugprone/UnhandledCodePathsCheck.cpp
index 1a7d907bac6ec..c02dcd50f10f3 100644
--- a/clang-tools-extra/clang-tidy/bugprone/UnhandledCodePathsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/UnhandledCodePathsCheck.cpp
@@ -98,39 +98,10 @@ void UnhandledCodePathsCheck::check(const
MatchFinder::MatchResult &Result) {
bool SwitchHasDefault = false;
std::tie(SwitchCaseCount, SwitchHasDefault) = countCaseLabels(Switch);
- // Checks the sanity of 'switch' statements that actually do define
- // a default branch but might be degenerated by having no or only one case.
- if (SwitchHasDefault) {
-handleSwitchWithDefault(Switch, SwitchCaseCount);
+ if (SwitchHasDefault || SwitchCaseCount <= 1)
return;
- }
- // Checks all 'switch' statements that do not define a default label.
- // Here the heavy lifting happens.
- if (!SwitchHasDefault && SwitchCaseCount > 0) {
-handleSwitchWithoutDefault(Switch, SwitchCaseCount, Result);
-return;
- }
- // Warns for degenerated 'switch' statements that neither define a case nor
- // a default label.
- // FIXME: Evaluate, if emitting a fix-it to simplify that statement is
- // reasonable.
- if (!SwitchHasDefault && SwitchCaseCount == 0) {
-diag(Switch->getBeginLoc(),
- "switch statement without labels has no effect");
-return;
- }
- llvm_unreachable("matched a case, that was not explicitly handled");
-}
-void UnhandledCodePathsCheck::handleSwitchWithDefault(const SwitchStmt *Switch,
- std::size_t CaseCount) {
- assert(CaseCount > 0 && "Switch statement with supposedly one default "
- "branch did not contain any case labels");
- if (CaseCount == 1 || CaseCount == 2)
-diag(Switch->getBeginLoc(),
- CaseCount == 1
- ? "degenerated switch with default label only"
- : "switch could be better written as an if/else statement");
+ handleSwitchWithoutDefault(Switch, SwitchCaseCount, Result);
}
void UnhandledCodePathsCheck::handleSwitchWithoutDefault(
@@ -146,11 +117,7 @@ void UnhandledCodePathsCheck::handleSwitchWithoutDefault(
// and duplicating case labels is not allowed this number represents
// the number of codepaths. It can be directly compared to 'MaxPathsPossible'
// to see if some cases are missing.
- // CaseCount == 0 is caught in DegenerateSwitch. Necessary because the
- // matcher used for here does not match on degenerate 'switch'.
- assert(CaseCount > 0 && "Switch statement without any case found. This case "
- "should be excluded by the matcher and is handled "
- "separately.");
+ assert(CaseCount > 1 && "Switch statement with fewer than two cases found.");
const std::size_t MaxPathsPossible = [&]() {
if (const auto *GeneralCondition =
Result.Nodes.getNodeAs("non-enum-condition")) {
@@ -165,10 +132,8 @@ void UnhandledCodePathsCheck::handleSwitchWithoutDefault(
return static_cast(0);
}();
- // FIXME: Transform the 'switch' into an 'if' for CaseCount == 1.
if (CaseCount < MaxPathsPossible)
diag(Switch->getBeginLoc(),
- CaseCount == 1 ? "switch with only one case; use an if statement"
-: "potential uncovered code path; add a default
label");
+ "potential uncovered code path; add a default label"
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
https://github.com/vbvictor approved this pull request. LGTM https://github.com/llvm/llvm-project/pull/198237 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
@@ -83,8 +83,10 @@ Potentially Breaking Changes ` ``hicpp-move-const-arg`` :doc:`performance-move-const-arg ` - ``hicpp-multiway-paths-covered`` :doc:`bugprone-unhandled-code-paths - ` + ``hicpp-multiway-paths-covered`` | :doc:`bugprone-unhandled-code-paths zwuis wrote: ```txt | They are line blocks, which preserve line breaks. | So that 'bugprone-unhandled-code-path' and 'readability-trivial-switch' are rendered in different lines. ``` Rendered as: ```txt They are line blocks, which preserve line breaks. So that 'bugprone-unhandled-code-path' and 'readability-trivial-switch' are rendered in different lines. ``` https://github.com/llvm/llvm-project/pull/198237 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
https://github.com/vbvictor edited https://github.com/llvm/llvm-project/pull/198237 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
@@ -0,0 +1,36 @@ +.. title:: clang-tidy - readability-trivial-switch + +readability-trivial-switch +== + +Finds trivial ``switch`` statements that can be written more clearly. + +Every ``switch`` statement should have at least two ``case`` labels other than a ``default`` label. vbvictor wrote: @EugeneZelenko, We are not enforcing 80-column limit anymore according to this https://discourse.llvm.org/t/rfc-remove-80-column-limit-in-documentation-files/89678 https://llvm.org/docs/CodingStandards.html#source-code-width > However, documentation files are not source code files, and instead of > fitting into 80 columns, they must be formatted to one sentence per line. > This way a change in the middle of a paragraph doesn’t cause unnecessary > changes in subsequent lines, making it easier for reviewers to see what has > changed when documentation is updated. https://github.com/llvm/llvm-project/pull/198237 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
@@ -83,8 +83,10 @@ Potentially Breaking Changes ` ``hicpp-move-const-arg`` :doc:`performance-move-const-arg ` - ``hicpp-multiway-paths-covered`` :doc:`bugprone-unhandled-code-paths - ` + ``hicpp-multiway-paths-covered`` | :doc:`bugprone-unhandled-code-paths vbvictor wrote: Formatting seems off, and highligh is different in github https://github.com/llvm/llvm-project/pull/198237 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
https://github.com/vbvictor commented: lgtm overall https://github.com/llvm/llvm-project/pull/198237 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
@@ -0,0 +1,36 @@ +.. title:: clang-tidy - readability-trivial-switch + +readability-trivial-switch +== + +Finds trivial ``switch`` statements that can be written more clearly. + +Every ``switch`` statement should have at least two ``case`` labels other than a ``default`` label. EugeneZelenko wrote: Format. https://github.com/llvm/llvm-project/pull/198237 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
https://github.com/zeyi2 approved this pull request. Code and tests LGTM, but please wait for other reviewers :) https://github.com/llvm/llvm-project/pull/198237 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
@@ -0,0 +1,44 @@
+// RUN: %check_clang_tidy -std=c++98-or-later %s readability-trivial-switch %t
+
+void bad(int I) {
+ switch (I) {
+// CHECK-MESSAGES: [[@LINE-1]]:3: warning: switch statement without labels
has no effect [readability-trivial-switch]
zeyi2 wrote:
```suggestion
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: switch statement without labels
has no effect
```
Same as other places :)
https://github.com/llvm/llvm-project/pull/198237
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
@@ -83,8 +83,10 @@ Potentially Breaking Changes ` ``hicpp-move-const-arg`` :doc:`performance-move-const-arg ` - ``hicpp-multiway-paths-covered`` :doc:`bugprone-unhandled-code-paths - ` + ``hicpp-multiway-paths-covered`` | :doc:`bugprone-unhandled-code-paths zeyi2 wrote: Oops, I forgot that. No need to add them in ReleaseNotes then :) https://github.com/llvm/llvm-project/pull/198237 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
github-actions[bot] wrote:
:warning: C/C++ code linter, clang-tidy found issues in your code. :warning:
You can test this locally with the following command:
```bash
git diff -U0 origin/main...HEAD --
clang-tools-extra/clang-tidy/readability/TrivialSwitchCheck.cpp
clang-tools-extra/clang-tidy/readability/TrivialSwitchCheck.h
clang-tools-extra/clang-tidy/bugprone/UnhandledCodePathsCheck.cpp
clang-tools-extra/clang-tidy/bugprone/UnhandledCodePathsCheck.h
clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp |
python3 clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py -path build -p1
-quiet
```
View the output from clang-tidy here.
```
clang-tools-extra/clang-tidy/readability/TrivialSwitchCheck.cpp:32:3: warning:
switching on non-enum value without default case may not cover all cases
[bugprone-switch-missing-default-case]
32 | switch (SourceLocation Loc = Switch->getBeginLoc(); CaseCount) {
| ^
clang-tools-extra/clang-tidy/readability/TrivialSwitchCheck.cpp:32:11: warning:
variable 'Loc' of type 'SourceLocation' can be declared 'const'
[misc-const-correctness]
32 | switch (SourceLocation Loc = Switch->getBeginLoc(); CaseCount) {
| ^
| const
```
https://github.com/llvm/llvm-project/pull/198237
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Move part of bugprone-unhandled-code-paths to a new check readability-trivial-switch (PR #198237)
@@ -83,8 +83,10 @@ Potentially Breaking Changes ` ``hicpp-move-const-arg`` :doc:`performance-move-const-arg ` - ``hicpp-multiway-paths-covered`` :doc:`bugprone-unhandled-code-paths - ` + ``hicpp-multiway-paths-covered`` | :doc:`bugprone-unhandled-code-paths zeyi2 wrote: Would you please add a new entry in the New Check section? https://github.com/llvm/llvm-project/pull/198237 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
