Repository navigation
house-unary-minus-unsigned: unary minus only on a signed operand - #114
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A project's
make tidyreports 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:-countof auint8_t,-1u, and an unscoped enum of an unsigned type. The new CodingStyle T-25 states the rule;Misra.mdnames the check instead of review.bool, a character type and an overloadedoperator-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:
-countof auint8_tis a negativeint, of auint32_ta 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.pypasses, with the cases of the new check:unsigned char,unsigned, a literal, an operand in parentheses, an unscoped enum, a template instantiated withintandunsigned, and what passes (signed, cast,float,char16_t, an overloaded operator, a template only instantiated withint). The cases found that clang'sisAnyCharacterType()andisCharType()countunsigned char, whichuint8_tis; the check tells plaincharby its builtin kind.clang -Weverything, GCC with-Wall -Wextra -Wconversion -Wsign-conversionand all of clang-tidy's checks report none of-cof auint8_t,-cof auint32_tand-1u.tests/check.sh lint tidypasses for the Arduino library,--no-arduino,--firmwareand--platformio, andtests/check.sh --targets tidyfor the Arduino library,--no-arduinoand--firmware.tests/doc_indent.pypasses.