Skip to content

house-unary-minus-unsigned: unary minus only on a signed operand - #114

Merged
theAndreas merged 2 commits into
mainfrom
house-unary-minus-unsigned
Nov 17, 2026
Merged

theAndreas merged 2 commits into
mainfrom
house-unary-minus-unsigned

Conversation

@theAndreas

Copy link
Copy Markdown
Owner

What

A project's make tidy reports the built-in unary minus on an unsigned operand, as MISRA C++:2023 Rule 8.3.1 asks, by the operand's type before the promotion: -count of a uint8_t, -1u, and an unscoped enum of an unsigned type. The new CodingStyle T-25 states the rule; Misra.md names the check instead of review.

bool, a character type and an overloaded operator- pass. In a template, the check reports the instantiations whose operand is unsigned. It has no fix-it: whether the operand becomes signed by its type or by a cast, and to which width, only the author knows.

Why

On an unsigned operand, the result depends on the width of its type: -count of a uint8_t is a negative int, of a uint32_t a large positive number, so the same line means another thing when a type changes. Neither clang-tidy nor GCC or clang with all their warnings report it. On the update, a project fixes each place by hand, as -static_cast<int16_t>(count).

How it was checked

  • tests/tidy_module.py passes, with the cases of the new check: unsigned char, unsigned, a literal, an operand in parentheses, an unscoped enum, a template instantiated with int and unsigned, and what passes (signed, cast, float, char16_t, an overloaded operator, a template only instantiated with int). The cases found that clang's isAnyCharacterType() and isCharType() count unsigned char, which uint8_t is; the check tells plain char by its builtin kind.
  • clang -Weverything, GCC with -Wall -Wextra -Wconversion -Wsign-conversion and all of clang-tidy's checks report none of -c of a uint8_t, -c of a uint32_t and -1u.
  • tests/check.sh lint tidy passes for the Arduino library, --no-arduino, --firmware and --platformio, and tests/check.sh --targets tidy for the Arduino library, --no-arduino and --firmware.
  • tests/doc_indent.py passes.

MISRA C++:2023 Rule 8.3.1 asks for the built-in unary minus only on an
operand of signed type, by its type before the promotion: on an unsigned
one the result is unsigned, or signed after the promotion, by the width of
the type. Neither clang-tidy nor GCC or clang with all their warnings
report it, so Misra.md left the rule to review.

The new check reports the built-in unary minus on an unsigned integer, an
unscoped enum of one as well, but not on bool or a character type, nor an
overloaded operator-. In a template it reports the instantiations whose
operand is unsigned. It has no fix-it, as only the author knows which
signed type the operand takes. clang's isAnyCharacterType() and
isCharType() count unsigned char, which uint8_t is, so plain char is
told by its builtin kind.

CodingStyle T-25 states the rule; ROADMAP C-16 records the check.
@theAndreas
theAndreas merged commit abaca7a into main Nov 17, 2026
1 check passed
@theAndreas
theAndreas deleted the house-unary-minus-unsigned branch November 17, 2026 12:58
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