From 71d7dd6f5f118e3da8591bbb2b4d563071ad0eea Mon Sep 17 00:00:00 2001 From: virskl Date: Tue, 17 Nov 2026 13:05:17 +0100 Subject: [PATCH 1/2] house-else-if-without-else: an if ... else if chain ends with else MISRA C++:2023 Rule 9.4.1 asks for an else at the end of every chain of else if, as 9.4.2 asks for a default in a switch. Misra.md left it to review, as hicpp-multiway-paths-covered, which checked it, is gone in clang-tidy 23. The new check reports the if of the last else if of a chain without else; once in a template, not in the body of a macro. It has no fix-it, as only the author knows what the else does. CodingStyle T-24 states the rule, and that an empty else says in a comment why nothing is to do. ROADMAP C-15 records the check, and C-3 drops it from its candidates. --- ROADMAP.md | 3 +- template/docs/CodingStyle.md.jinja | 1 + template/docs/Misra.md.jinja | 2 +- .../else_if_without_else_check.cpp | 43 +++++++ .../tidy_module/else_if_without_else_check.h | 39 ++++++ .../tools/lib/tidy_module/house_module.cpp | 9 +- tests/tidy_module/else_if_without_else.cpp | 118 ++++++++++++++++++ 7 files changed, 210 insertions(+), 5 deletions(-) create mode 100644 template/tools/lib/tidy_module/else_if_without_else_check.cpp create mode 100644 template/tools/lib/tidy_module/else_if_without_else_check.h create mode 100644 tests/tidy_module/else_if_without_else.cpp diff --git a/ROADMAP.md b/ROADMAP.md index 7b13315..3b2cfef 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -73,7 +73,7 @@ gh, uv, clangd. Every project builds the same image again, and every machine doe |---|---|---| | C-1 | Find out whether the clang-tidy pinned from PyPI loads a plugin (`-load`); if not, clang-tidy and the LLVM headers from apt, pinned in the Dockerfile | done: it loads one and exports the symbols a module needs; only the headers come from apt.llvm.org, at the major version | | C-2 | A clang-tidy module of our own, with the first check: braced initialization in a member initializer list, with a fix-it (Utl's `function_ref.h:106` slipped through) | done: `house-braced-member-init` | -| C-3 | Further candidates: a NOLINT names its check and its reason; an `if … else if` ends with `else` (MISRA 9.4.1, `hicpp-multiway-paths-covered` is gone in clang-tidy 23); a template parameter is not a single letter (CodingStyle.md); an expression of too many steps, to split (CodingStyle.md T-18); a cast to `void` of a result `_NODISCARD` marks, which no compiler reports where the macro is `[[nodiscard]]` (CodingStyle.md FN-10) | idea | +| C-3 | Further candidates: a NOLINT names its check and its reason; a template parameter is not a single letter (CodingStyle.md); an expression of too many steps, to split (CodingStyle.md T-18); a cast to `void` of a result `_NODISCARD` marks, which no compiler reports where the macro is `[[nodiscard]]` (CodingStyle.md FN-10) | idea | | C-4 | Rule of six: a class that declares one special member declares all six, the default constructor too; `cppcoreguidelines-special-member-functions` checks the five, nothing the sixth | idea | | C-5 | Fixed-width integer types (MISRA C:2012 Dir 4.6, AUTOSAR A3-9-1): no `int`, `unsigned` in `src/`, but `char` for text, `bool`, `size_t`, and what the Arduino core's signatures give, as the `int` of `analogRead()`. `google-runtime-int`, which is on, reports only `short` and `long`. Utl keeps to it already | idea | | C-6 | Braced initialization outside the member initializer list (CodingStyle.md T-3): a variable and a data member's default, `auto` as well, with a fix-it where braces keep the meaning | done: `house-braced-var-init` | @@ -85,6 +85,7 @@ gh, uv, clangd. Every project builds the same image again, and every machine doe | C-12 | No `friend` but a hidden friend (CodingStyle C-12): `house-no-friend` | done | | C-13 | One form per operator (CodingStyle C-15): a member where it changes its operand or is unary, a hidden friend where it is binary; `house-operator-form` | done | | C-14 | Parentheses that make the precedence explicit (CodingStyle T-23, MISRA C++:2023 8.0.1): `house-operator-precedence`, with a fix-it, for a comparison in a logical operator and the operands of `?:` as well, which `readability-math-missing-parentheses` left out; it holds the rule in full, so that check is off | done | +| C-15 | An `if … else if` chain ends with `else` (CodingStyle T-24, MISRA C++:2023 9.4.1): `house-else-if-without-else`, as `hicpp-multiway-paths-covered`, which checked it, is gone in clang-tidy 23 | done | ### M5 - A generic embedded library on the ETL diff --git a/template/docs/CodingStyle.md.jinja b/template/docs/CodingStyle.md.jinja index f631bfa..da48b62 100644 --- a/template/docs/CodingStyle.md.jinja +++ b/template/docs/CodingStyle.md.jinja @@ -109,6 +109,7 @@ it: | T-21 | A `char` is widened to a number through `unsigned char`: whether `char` has a sign depends on the target, and where it has one, a character above 127 turns negative. | `bugprone-signed-char-misuse` | | T-22 | A literal's suffix with the `u` in lower case and every `L` in upper case: `192u`, `1000uL`, `1LL`, `1.0L`. An `l` is read as a 1; a `U` reads as one more digit of a hexadecimal value, as in `0x04U`, where a `u` stands apart. A float's `f` is written either way. | `readability-uppercase-literal-suffix` | | T-23 | An operand that is itself an operation of higher precedence stands in parentheses: `(a == b) && ready`, `(x + 1) < limit`, `ready ? (a + b) : c`, `sizeof(value)`. The reader need not know the table of precedence, and an operand that moves keeps what it binds (MISRA C++:2023 8.0.1). An operand of the same precedence, `a + b + c`, needs none, nor one that is a call, a cast, a unary operator or a name, nor the operands of an assignment. | `house-operator-precedence`, `-Wparentheses`; an `#if` by review | +| T-24 | An `if … else if` chain ends with `else`, as a `switch` has its `default` (T-15): it says what happens when no condition holds, and an empty one says in a comment why nothing does. An `if` alone needs none (MISRA C++:2023 9.4.1). | `house-else-if-without-else`; the comment by review | T-18 in an example: diff --git a/template/docs/Misra.md.jinja b/template/docs/Misra.md.jinja index 883ff61..c6d7c32 100644 --- a/template/docs/Misra.md.jinja +++ b/template/docs/Misra.md.jinja @@ -148,7 +148,7 @@ PVS-Studio or Cppcheck Premium, fills the rows marked *Review* (ROADMAP I-4). |---|---|---|---|---| | Rule 9.2.1 | Required | No explicit conversion as a statement | `bugprone-unused-raii`, in part | | | Rule 9.3.1 | Required | Bodies are compound statements | `readability-braces-around-statements` | | -| Rule 9.4.1 | Required | `if … else if` ends with `else` | Review (ROADMAP C-3) | | +| Rule 9.4.1 | Required | `if … else if` ends with `else` | `house-else-if-without-else`, CodingStyle T-24 | | | Rule 9.4.2 | Required | Structure of a `switch` | `bugprone-switch-missing-default-case`, `-Wswitch`, `-Wimplicit-fallthrough`, CodingStyle T-15, in part: the rest of the structure, as where the labels stand and how many branches there are, by review | A `switch` over an `enum class` names every value and has no `default` (T-15): `-Wswitch` then reports a value added to the `enum` later, which a `default` would take in unnoticed. A value the `enum` does not name is handled after the `switch`, so that a way out stays | | Rule 9.5.1 | Advisory | Simple counting `for` loops | Review | | | Rule 9.5.2 | Required | Range-for initializer with one call at most | Review | | diff --git a/template/tools/lib/tidy_module/else_if_without_else_check.cpp b/template/tools/lib/tidy_module/else_if_without_else_check.cpp new file mode 100644 index 0000000..cc27564 --- /dev/null +++ b/template/tools/lib/tidy_module/else_if_without_else_check.cpp @@ -0,0 +1,43 @@ +// Copyright 2026 Andreas Burnickl +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "else_if_without_else_check.h" + +#include "clang/AST/Stmt.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" + +namespace house { + +void ElseIfWithoutElseCheck::registerMatchers( + clang::ast_matchers::MatchFinder* finder) { + using clang::ast_matchers::hasElse; + using clang::ast_matchers::hasParent; + using clang::ast_matchers::ifStmt; + using clang::ast_matchers::isInTemplateInstantiation; + using clang::ast_matchers::stmt; + using clang::ast_matchers::unless; + // An if without else in an if; check() keeps the one that is the else of + // its parent, not its then. + finder->addMatcher( + ifStmt(unless(hasElse(stmt())), hasParent(ifStmt().bind("parent")), + unless(isInTemplateInstantiation())) + .bind("last"), + this); +} + +void ElseIfWithoutElseCheck::check( + clang::ast_matchers::MatchFinder::MatchResult const& result) { + auto const* const last = result.Nodes.getNodeAs("last"); + auto const* const parent = result.Nodes.getNodeAs("parent"); + bool const is_else_if{parent->getElse() == last}; + if (!is_else_if || last->getIfLoc().isMacroID()) { + return; + } + diag(last->getIfLoc(), + "the if ... else if chain ends without else; add one, with what is left " + "to do or why there is nothing (CodingStyle T-24, MISRA C++:2023 " + "9.4.1)"); +} + +} // namespace house diff --git a/template/tools/lib/tidy_module/else_if_without_else_check.h b/template/tools/lib/tidy_module/else_if_without_else_check.h new file mode 100644 index 0000000..f6befb6 --- /dev/null +++ b/template/tools/lib/tidy_module/else_if_without_else_check.h @@ -0,0 +1,39 @@ +// Copyright 2026 Andreas Burnickl +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef TOOLS_TIDY_MODULE_ELSE_IF_WITHOUT_ELSE_CHECK_H_ +#define TOOLS_TIDY_MODULE_ELSE_IF_WITHOUT_ELSE_CHECK_H_ + +#include "clang-tidy/ClangTidyCheck.h" + +namespace house { + +/*! + * \brief house-else-if-without-else: an `if … else if` chain ends with + * `else`, CodingStyle T-24 and MISRA C++:2023 Rule 9.4.1. + * + * Reported: the `if` of the last `else if` of a chain that has no `else`, an + * `if constexpr` as well. Not reported: an `if` alone, with or without `else`; + * an `if` nested in the branch of another, which starts a chain of its own. + * Reported where it is written: in a template, not again in its + * instantiations; not in the body of a macro, which every expansion shares. + * + * No fix-it: what the `else` does, or why it does nothing, only the author + * knows. + */ +class ElseIfWithoutElseCheck : public clang::tidy::ClangTidyCheck { + public: + using ClangTidyCheck::ClangTidyCheck; + + bool isLanguageVersionSupported( + clang::LangOptions const& lang_opts) const override { + return lang_opts.CPlusPlus; + } + void registerMatchers(clang::ast_matchers::MatchFinder* finder) override; + void check( + clang::ast_matchers::MatchFinder::MatchResult const& result) override; +}; + +} // namespace house + +#endif // TOOLS_TIDY_MODULE_ELSE_IF_WITHOUT_ELSE_CHECK_H_ diff --git a/template/tools/lib/tidy_module/house_module.cpp b/template/tools/lib/tidy_module/house_module.cpp index 9a18c65..0c8c045 100644 --- a/template/tools/lib/tidy_module/house_module.cpp +++ b/template/tools/lib/tidy_module/house_module.cpp @@ -2,14 +2,15 @@ // SPDX-License-Identifier: GPL-3.0-or-later // // The module house: checks of the rules every project made from the template -// keeps, which clang-tidy's own checks do not. tools/lib/tidy_module.sh builds it, -// and tools/tidy.sh loads it with --load; .clang-tidy turns its checks on as -// house-*. +// keeps, which clang-tidy's own checks do not. tools/lib/tidy_module.sh builds +// it, and tools/tidy.sh loads it with --load; .clang-tidy turns its checks on +// as house-*. #include "bitwise_in_bit_class_check.h" #include "braced_member_init_check.h" #include "braced_var_init_check.h" #include "clang-tidy/ClangTidyModule.h" +#include "else_if_without_else_check.h" #include "no_friend_check.h" #include "operator_form_check.h" #include "operator_precedence_check.h" @@ -25,6 +26,8 @@ class HouseModule : public clang::tidy::ClangTidyModule { "house-bitwise-in-bit-class"); factories.registerCheck("house-braced-member-init"); factories.registerCheck("house-braced-var-init"); + factories.registerCheck( + "house-else-if-without-else"); factories.registerCheck("house-no-friend"); factories.registerCheck("house-operator-form"); factories.registerCheck( diff --git a/tests/tidy_module/else_if_without_else.cpp b/tests/tidy_module/else_if_without_else.cpp new file mode 100644 index 0000000..c8e7c0f --- /dev/null +++ b/tests/tidy_module/else_if_without_else.cpp @@ -0,0 +1,118 @@ +// The cases of house-else-if-without-else. A line that the check reports ends +// in a comment `house: chain`; any other line must not be reported. +// tests/tidy_module.py runs the check on this file and compiles it as C++11. + +namespace reported { + +// The last else if of a chain without else, of two links and of three. +int Two(bool p, bool q) { + int r{0}; + if (p) { + r = 1; + } else if (q) { // house: chain + r = 2; + } + return r; +} + +int Three(bool p, bool q, bool s) { + int r{0}; + if (p) { + r = 1; + } else if (q) { + r = 2; + } else if (s) { // house: chain + r = 3; + } + return r; +} + +// A chain in the branch of another if is a chain of its own. +int Nested(bool p, bool q, bool s) { + int r{0}; + if (p) { + if (q) { + r = 1; + } else if (s) { // house: chain + r = 2; + } + } else { + r = 3; + } + return r; +} + +// In a template: once, as written. +template +Number InTemplate(Number a, bool p, bool q) { + Number r{a}; + if (p) { + r = a; + } else if (q) { // house: chain + r = Number{}; + } + return r; +} + +int Instantiated() { + return InTemplate(1, true, false) + + static_cast(InTemplate(1L, false, true)); +} + +} // namespace reported + +namespace passed { + +// An if alone, with else or without. +int Alone(bool p) { + int r{0}; + if (p) { + r = 1; + } + if (p) { + r = 2; + } else { + r = 3; + } + return r; +} + +// A chain that ends with else, an empty one with its reason as well. +int Ended(bool p, bool q) { + int r{0}; + if (p) { + r = 1; + } else if (q) { + r = 2; + } else { + // Neither: r keeps its start. + } + return r; +} + +// An if in the then branch of another, without else: not a chain. +int InThen(bool p, bool q) { + int r{0}; + if (p) { + if (q) { + r = 1; + } + } + return r; +} + +// In the body of a macro: every expansion shares it. +#define PICK(p, q, r) \ + if (p) { \ + (r) = 1; \ + } else if (q) { \ + (r) = 2; \ + } + +int InBody(bool p, bool q) { + int r{0}; + PICK(p, q, r) + return r; +} + +} // namespace passed From 08d982206a6e1ed56106cc17a725adad74351a4b Mon Sep 17 00:00:00 2001 From: virskl Date: Tue, 17 Nov 2026 13:06:50 +0100 Subject: [PATCH 2/2] CHANGELOG: house-else-if-without-else --- CHANGELOG.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c86733a..fe912fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,9 +22,14 @@ brings it. added later, but the row named T-15 as if it covered the rule. It now says why the template departs from it there, and that the rest of the structure of a `switch`, as where its labels stand and how many branches it has, is left to review. - - -On the update: nothing to do; a project that keeps its own `Misra.md` takes the rows over. +- **`house-else-if-without-else`: an `if … else if` chain ends with `else` (CodingStyle T-24, + MISRA C++:2023 9.4.1).** As a `switch` has its `default`, the chain says what happens when no + condition holds; an empty `else` says in a comment why nothing does. `Misra.md` left the rule + to review, as `hicpp-multiway-paths-covered`, which checked it, is gone in clang-tidy 23. + +On the update: `make tidy` reports the `if` of every chain that lacks its `else`, without a +fix-it, as only the author knows what the `else` does. A project that keeps its own `Misra.md` +takes the rows over. ## v0.13.0