Skip to content

Commit 6fda6fa

Browse files
committed
Add Banned8
1 parent ad580d3 commit 6fda6fa

File tree

8 files changed

+172
-2
lines changed

8 files changed

+172
-2
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Banned8Query = TBuiltInUnaryPlusOperatorShouldNotBeUsedQuery()
7+
8+
predicate isBanned8QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `builtInUnaryPlusOperatorShouldNotBeUsed` query
11+
Banned8Package::builtInUnaryPlusOperatorShouldNotBeUsedQuery() and
12+
queryId =
13+
// `@id` for the `builtInUnaryPlusOperatorShouldNotBeUsed` query
14+
"cpp/misra/built-in-unary-plus-operator-should-not-be-used" and
15+
ruleId = "RULE-8-3-2" and
16+
category = "advisory"
17+
}
18+
19+
module Banned8Package {
20+
Query builtInUnaryPlusOperatorShouldNotBeUsedQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `builtInUnaryPlusOperatorShouldNotBeUsed` query
24+
TQueryCPP(TBanned8PackageQuery(TBuiltInUnaryPlusOperatorShouldNotBeUsedQuery()))
25+
}
26+
}

cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import codingstandards.cpp.exclusions.RuleMetadata
44
//** Import packages for this language **/
55
import Allocations
66
import Banned1
7+
import Banned8
78
import BannedAPIs
89
import BannedFunctions
910
import BannedLibraries
@@ -95,6 +96,7 @@ import VirtualFunctions
9596
newtype TCPPQuery =
9697
TAllocationsPackageQuery(AllocationsQuery q) or
9798
TBanned1PackageQuery(Banned1Query q) or
99+
TBanned8PackageQuery(Banned8Query q) or
98100
TBannedAPIsPackageQuery(BannedAPIsQuery q) or
99101
TBannedFunctionsPackageQuery(BannedFunctionsQuery q) or
100102
TBannedLibrariesPackageQuery(BannedLibrariesQuery q) or
@@ -186,6 +188,7 @@ newtype TCPPQuery =
186188
predicate isQueryMetadata(Query query, string queryId, string ruleId, string category) {
187189
isAllocationsQueryMetadata(query, queryId, ruleId, category) or
188190
isBanned1QueryMetadata(query, queryId, ruleId, category) or
191+
isBanned8QueryMetadata(query, queryId, ruleId, category) or
189192
isBannedAPIsQueryMetadata(query, queryId, ruleId, category) or
190193
isBannedFunctionsQueryMetadata(query, queryId, ruleId, category) or
191194
isBannedLibrariesQueryMetadata(query, queryId, ruleId, category) or
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @id cpp/misra/built-in-unary-plus-operator-should-not-be-used
3+
* @name RULE-8-3-2: The built-in unary + operator should not be used
4+
* @description Using the built-in unary '+' operator may trigger unexpected implicit type
5+
* conversions.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-8-3-2
10+
* scope/single-translation-unit
11+
* correctness
12+
* external/misra/enforcement/decidable
13+
* external/misra/obligation/advisory
14+
*/
15+
16+
import cpp
17+
import codingstandards.cpp.misra
18+
19+
from UnaryPlusExpr e
20+
where not e.isFromUninstantiatedTemplate(_)
21+
select e, "Use of built-in unary + operator."
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
| test.cpp:12:12:12:14 | + ... | Use of built-in unary + operator. |
2+
| test.cpp:15:12:15:14 | + ... | Use of built-in unary + operator. |
3+
| test.cpp:18:12:18:15 | + ... | Use of built-in unary + operator. |
4+
| test.cpp:21:12:21:14 | + ... | Use of built-in unary + operator. |
5+
| test.cpp:24:12:24:14 | + ... | Use of built-in unary + operator. |
6+
| test.cpp:26:12:26:27 | + ... | Use of built-in unary + operator. |
7+
| test.cpp:29:18:29:25 | + ... | Use of built-in unary + operator. |
8+
| test.cpp:30:12:30:27 | + ... | Use of built-in unary + operator. |
9+
| test.cpp:33:12:33:13 | + ... | Use of built-in unary + operator. |
10+
| test.cpp:34:12:34:15 | + ... | Use of built-in unary + operator. |
11+
| test.cpp:35:12:35:16 | + ... | Use of built-in unary + operator. |
12+
| test.cpp:38:7:38:8 | + ... | Use of built-in unary + operator. |
13+
| test.cpp:42:12:42:15 | + ... | Use of built-in unary + operator. |
14+
| test.cpp:45:12:45:13 | + ... | Use of built-in unary + operator. |
15+
| test.cpp:48:12:48:15 | + ... | Use of built-in unary + operator. |
16+
| test.cpp:64:10:64:13 | + ... | Use of built-in unary + operator. |
17+
| test.cpp:64:10:64:13 | + ... | Use of built-in unary + operator. |
18+
| test.cpp:75:14:75:17 | + ... | Use of built-in unary + operator. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-3-2/BuiltInUnaryPlusOperatorShouldNotBeUsed.ql
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <cstdint>
2+
3+
// Helper types
4+
enum UnscopedEnum { A, B, C };
5+
enum SmallEnum : uint8_t { S1, S2 };
6+
7+
uint8_t operator+(SmallEnum e) { return static_cast<uint8_t>(e); }
8+
9+
void test_unary_plus() {
10+
// === NON-COMPLIANT: integral promotion ===
11+
uint8_t u8 = 1;
12+
auto a = +u8; // NON_COMPLIANT: promotes to int
13+
14+
int8_t i8 = 1;
15+
auto b = +i8; // NON_COMPLIANT: promotes to int
16+
17+
uint16_t u16 = 1;
18+
auto c = +u16; // NON_COMPLIANT: promotes to int
19+
20+
char ch = 'x';
21+
auto d = +ch; // NON_COMPLIANT: promotes to int
22+
23+
bool bl = true;
24+
auto e = +bl; // NON_COMPLIANT: promotes to int
25+
26+
auto f = +UnscopedEnum::A; // NON_COMPLIANT: promotes to int
27+
28+
// === NON_COMPLIANT: function/lambda decay ===
29+
void (*fp)() = +[]() {}; // NON_COMPLIANT: lambda decays to pointer
30+
auto g = +test_unary_plus; // NON_COMPLIANT: function decays to pointer
31+
32+
// === NON_COMPLIANT: literals and expressions ===
33+
auto h = +1; // NON_COMPLIANT: unary + on literal
34+
auto i = +'a'; // NON_COMPLIANT: promotes char to int
35+
auto j = +true; // NON_COMPLIANT: promotes bool to int
36+
37+
int x;
38+
x = +1; // NON_COMPLIANT: unary +, not +=
39+
40+
// === NON_COMPLIANT: larger types (still built-in +) ===
41+
int i32 = 1;
42+
auto k = +i32; // NON_COMPLIANT: built-in unary +
43+
44+
long l = 1;
45+
auto m = +l; // NON_COMPLIANT: built-in unary +
46+
47+
double dbl = 1.0;
48+
auto n = +dbl; // NON_COMPLIANT: built-in unary +
49+
50+
// === COMPLIANT: user-defined operator+ ===
51+
auto o = +SmallEnum::S1; // COMPLIANT: calls user-defined operator+
52+
auto p = operator+(SmallEnum::S2); // COMPLIANT: explicit call
53+
54+
// === COMPLIANT: not unary + ===
55+
auto q = 1 + 2; // COMPLIANT: binary +
56+
auto r = u8 + u8; // COMPLIANT: binary +
57+
58+
int y = 0;
59+
y += 1; // COMPLIANT: compound assignment
60+
}
61+
62+
// === NON_COMPLIANT: in other contexts ===
63+
template <typename T> T promote(T val) {
64+
return +val; // NON_COMPLIANT (when T is built-in type)
65+
}
66+
67+
void test_template() {
68+
promote(uint8_t{1}); // Instantiates NON_COMPLIANT case
69+
promote(1); // Instantiates NON_COMPLIANT case
70+
}
71+
72+
// === NON_COMPLIANT: array decay ===
73+
void test_array() {
74+
int arr[5];
75+
auto ptr = +arr; // NON_COMPLIANT: array decays to pointer
76+
}

rule_packages/cpp/Banned8.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"MISRA-C++-2023": {
3+
"RULE-8-3-2": {
4+
"properties": {
5+
"enforcement": "decidable",
6+
"obligation": "advisory"
7+
},
8+
"queries": [
9+
{
10+
"description": "Using the built-in unary '+' operator may trigger unexpected implicit type conversions.",
11+
"kind": "problem",
12+
"name": "The built-in unary + operator should not be used",
13+
"precision": "very-high",
14+
"severity": "error",
15+
"short_name": "BuiltInUnaryPlusOperatorShouldNotBeUsed",
16+
"tags": [
17+
"scope/single-translation-unit",
18+
"correctness"
19+
]
20+
}
21+
],
22+
"title": "The built-in unary + operator should not be used"
23+
}
24+
}
25+
}

rules.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Language,Standard,ID,Supportable,Obligation level,Enforcement level,Allocated target,Description,Similar,Package,Difficulty,Justification for querability level
2-
cpp,AUTOSAR,A0-1-1,Yes,Required,Automated,Implementation,A project shall not contain instances of non-volatile variables being given values that are not subsequently used.,M0-1-6,DeadCode,Medium,
2+
cpp,AUTOSAR,-1,Yes,Required,Automated,Implementation,A project shall not contain instances of non-volatile variables being given values that are not subsequently used.,M0-1-6,DeadCode,Medium,
33
cpp,AUTOSAR,A0-1-2,Yes,Required,Automated,Implementation,The value returned by a function having a non-void return type that is not an overloaded operator shall be used.,M0-1-7,DeadCode,Easy,
44
cpp,AUTOSAR,A0-1-3,Yes,Required,Automated,Implementation,"Every function defined in an anonymous namespace, or static function with internal linkage, or private member function shall be used.",M0-1-10,DeadCode,Easy,
55
cpp,AUTOSAR,A0-1-4,Yes,Required,Automated,Implementation,There shall be no unused named parameters in non-virtual functions.,,DeadCode,Easy,
@@ -894,7 +894,7 @@ cpp,MISRA-C++-2023,RULE-8-2-9,Yes,Required,Decidable,Single Translation Unit,The
894894
cpp,MISRA-C++-2023,RULE-8-2-10,Yes,Required,Undecidable,System,"Functions shall not call themselves, either directly or indirectly",A7-5-2,ImportMisra23,Import,
895895
cpp,MISRA-C++-2023,RULE-8-2-11,Yes,Required,Decidable,Single Translation Unit,An argument passed via ellipsis shall have an appropriate type,,Preconditions,Easy,
896896
cpp,MISRA-C++-2023,RULE-8-3-1,Yes,Advisory,Decidable,Single Translation Unit,The built-in unary - operator should not be applied to an expression of unsigned type,M5-3-2,ImportMisra23,Import,
897-
cpp,MISRA-C++-2023,RULE-8-3-2,Yes,Advisory,Decidable,Single Translation Unit,The built-in unary + operator should not be used,,Banned,Easy,
897+
cpp,MISRA-C++-2023,RULE-8-3-2,Yes,Advisory,Decidable,Single Translation Unit,The built-in unary + operator should not be used,,Banned8,Easy,
898898
cpp,MISRA-C++-2023,RULE-8-7-1,Yes,Required,Undecidable,System,Pointer arithmetic shall not form an invalid pointer,ARR30-C,Memory1,Easy,
899899
cpp,MISRA-C++-2023,RULE-8-7-2,Yes,Required,Undecidable,System,Subtraction between pointers shall only be applied to pointers that address elements of the same array,ARR36-C,Memory2,Easy,
900900
cpp,MISRA-C++-2023,RULE-8-9-1,Yes,Required,Undecidable,System,"The built-in relational operators >, >=, < and <= shall not be applied to objects of pointer type, except where they point to elements of the same array",ARR36-C,Memory3,Easy,

0 commit comments

Comments
 (0)