Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions template/docs/CodingStyle.md.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion template/docs/Misra.md.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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 | |
Expand Down
43 changes: 43 additions & 0 deletions template/tools/lib/tidy_module/else_if_without_else_check.cpp
Original file line number Diff line number Diff line change
@@ -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<clang::IfStmt>("last");
auto const* const parent = result.Nodes.getNodeAs<clang::IfStmt>("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
39 changes: 39 additions & 0 deletions template/tools/lib/tidy_module/else_if_without_else_check.h
Original file line number Diff line number Diff line change
@@ -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_
9 changes: 6 additions & 3 deletions template/tools/lib/tidy_module/house_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,6 +26,8 @@ class HouseModule : public clang::tidy::ClangTidyModule {
"house-bitwise-in-bit-class");
factories.registerCheck<BracedMemberInitCheck>("house-braced-member-init");
factories.registerCheck<BracedVarInitCheck>("house-braced-var-init");
factories.registerCheck<ElseIfWithoutElseCheck>(
"house-else-if-without-else");
factories.registerCheck<NoFriendCheck>("house-no-friend");
factories.registerCheck<OperatorFormCheck>("house-operator-form");
factories.registerCheck<OperatorPrecedenceCheck>(
Expand Down
118 changes: 118 additions & 0 deletions tests/tidy_module/else_if_without_else.cpp
Original file line number Diff line number Diff line change
@@ -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 <typename Number>
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<int>(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
Loading