Skip to content

Commit 063a5cc

Browse files
committed
Rule 7.0.6: Improve tests for templates
1 parent 3fdaa98 commit 063a5cc

File tree

2 files changed

+49
-43
lines changed

2 files changed

+49
-43
lines changed

cpp/misra/test/rules/RULE-7-0-6/NumericAssignmentTypeMismatch.expected

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,42 +49,42 @@
4949
| test.cpp:289:6:289:8 | u32 | Assignment between incompatible numeric types from 'uint32_t' to 'size_t'. |
5050
| test.cpp:294:12:294:14 | s16 | Assignment between incompatible numeric types from 'int16_t' to 'int32_t'. |
5151
| test.cpp:313:9:313:10 | 42 | Assignment between incompatible numeric types from 'int' to 'long'. |
52-
| test.cpp:342:23:342:24 | 42 | Assignment between incompatible numeric types from 'int' to 'unsigned long'. |
53-
| test.cpp:352:19:352:25 | ... + ... | Assignment between incompatible numeric types from 'int' to 'int16_t'. |
54-
| test.cpp:357:10:357:12 | u32 | Assignment between incompatible numeric types from 'uint32_t' to 'int32_t'. |
55-
| test.cpp:390:8:390:9 | l6 | Assignment between incompatible numeric types from 'uint32_t &' to 'uint8_t'. |
56-
| test.cpp:391:8:391:9 | l7 | Assignment between incompatible numeric types from 'int8_t &' to 'uint8_t'. |
57-
| test.cpp:392:9:392:10 | l8 | Assignment between incompatible numeric types from 'float &' to 'int32_t'. |
58-
| test.cpp:403:6:403:7 | l3 | Assignment between incompatible numeric types from 'uint8_t &' to 'int64_t'. |
59-
| test.cpp:404:6:404:7 | l4 | Assignment between incompatible numeric types from 'uint16_t &' to 'int32_t'. |
60-
| test.cpp:415:8:415:9 | l3 | Assignment between incompatible numeric types from 'uint8_t &' to 'int8_t'. |
61-
| test.cpp:416:8:416:9 | l4 | Assignment between incompatible numeric types from 'int8_t &' to 'uint8_t'. |
62-
| test.cpp:429:9:429:10 | l4 | Assignment between incompatible numeric types from 'float &' to 'int32_t'. |
63-
| test.cpp:430:7:430:8 | l5 | Assignment between incompatible numeric types from 'double &' to 'float'. |
64-
| test.cpp:431:7:431:8 | l6 | Assignment between incompatible numeric types from 'int32_t &' to 'float'. |
65-
| test.cpp:442:8:442:14 | ... + ... | Assignment between incompatible numeric types from 'int' to 'uint8_t'. |
66-
| test.cpp:460:7:460:8 | l3 | Assignment between incompatible numeric types from 'uint16_t &' to 'uint32_t'. |
67-
| test.cpp:463:7:463:8 | l5 | Assignment between incompatible numeric types from 'uint64_t &' to 'uint32_t'. |
68-
| test.cpp:512:12:512:13 | l1 | Assignment between incompatible numeric types from 'uint32_t' to 'uint8_t'. |
69-
| test.cpp:513:12:513:13 | l1 | Assignment between incompatible numeric types from 'uint32_t' to 'uint16_t'. |
70-
| test.cpp:515:12:515:13 | l2 | Assignment between incompatible numeric types from 'int32_t' to 'int8_t'. |
71-
| test.cpp:516:12:516:13 | l2 | Assignment between incompatible numeric types from 'int32_t' to 'int16_t'. |
72-
| test.cpp:536:12:536:14 | 300 | Assignment between incompatible numeric types from 'int' to 'uint8_t'. |
73-
| test.cpp:537:12:537:16 | 70000 | Assignment between incompatible numeric types from 'int' to 'uint16_t'. |
74-
| test.cpp:538:12:538:27 | 4294967296 | Assignment between incompatible numeric types from 'unsigned long long' to 'uint32_t'. |
75-
| test.cpp:539:12:539:14 | 200 | Assignment between incompatible numeric types from 'int' to 'int8_t'. |
76-
| test.cpp:540:12:540:16 | 40000 | Assignment between incompatible numeric types from 'int' to 'int16_t'. |
77-
| test.cpp:541:12:541:26 | 4294967296 | Assignment between incompatible numeric types from 'long long' to 'int32_t'. |
78-
| test.cpp:542:12:542:14 | 1.0 | Assignment between incompatible numeric types from 'double' to 'float'. |
79-
| test.cpp:543:12:543:15 | 1.0 | Assignment between incompatible numeric types from 'float' to 'double'. |
80-
| test.cpp:548:12:548:18 | ... + ... | Assignment between incompatible numeric types from 'int' to 'uint8_t'. |
81-
| test.cpp:549:12:549:18 | ... + ... | Assignment between incompatible numeric types from 'int' to 'uint16_t'. |
82-
| test.cpp:550:12:550:18 | ... + ... | Assignment between incompatible numeric types from 'int' to 'uint32_t'. |
83-
| test.cpp:552:12:552:13 | l1 | Assignment between incompatible numeric types from 'uint8_t' to 'int16_t'. |
84-
| test.cpp:553:12:553:13 | l1 | Assignment between incompatible numeric types from 'uint8_t' to 'int32_t'. |
85-
| test.cpp:554:12:554:13 | l1 | Assignment between incompatible numeric types from 'uint8_t' to 'float'. |
86-
| test.cpp:555:12:555:13 | l1 | Assignment between incompatible numeric types from 'uint8_t' to 'double'. |
87-
| test.cpp:585:9:585:37 | static_cast<int16_t>... | Assignment between incompatible numeric types from 'int16_t' to 'int32_t'. |
52+
| test.cpp:346:25:346:27 | u32 | Assignment between incompatible numeric types from 'uint32_t' to 'unsigned long'. |
53+
| test.cpp:358:19:358:25 | ... + ... | Assignment between incompatible numeric types from 'int' to 'int16_t'. |
54+
| test.cpp:363:10:363:12 | u32 | Assignment between incompatible numeric types from 'uint32_t' to 'int32_t'. |
55+
| test.cpp:396:8:396:9 | l6 | Assignment between incompatible numeric types from 'uint32_t &' to 'uint8_t'. |
56+
| test.cpp:397:8:397:9 | l7 | Assignment between incompatible numeric types from 'int8_t &' to 'uint8_t'. |
57+
| test.cpp:398:9:398:10 | l8 | Assignment between incompatible numeric types from 'float &' to 'int32_t'. |
58+
| test.cpp:409:6:409:7 | l3 | Assignment between incompatible numeric types from 'uint8_t &' to 'int64_t'. |
59+
| test.cpp:410:6:410:7 | l4 | Assignment between incompatible numeric types from 'uint16_t &' to 'int32_t'. |
60+
| test.cpp:421:8:421:9 | l3 | Assignment between incompatible numeric types from 'uint8_t &' to 'int8_t'. |
61+
| test.cpp:422:8:422:9 | l4 | Assignment between incompatible numeric types from 'int8_t &' to 'uint8_t'. |
62+
| test.cpp:435:9:435:10 | l4 | Assignment between incompatible numeric types from 'float &' to 'int32_t'. |
63+
| test.cpp:436:7:436:8 | l5 | Assignment between incompatible numeric types from 'double &' to 'float'. |
64+
| test.cpp:437:7:437:8 | l6 | Assignment between incompatible numeric types from 'int32_t &' to 'float'. |
65+
| test.cpp:448:8:448:14 | ... + ... | Assignment between incompatible numeric types from 'int' to 'uint8_t'. |
66+
| test.cpp:466:7:466:8 | l3 | Assignment between incompatible numeric types from 'uint16_t &' to 'uint32_t'. |
67+
| test.cpp:469:7:469:8 | l5 | Assignment between incompatible numeric types from 'uint64_t &' to 'uint32_t'. |
68+
| test.cpp:518:12:518:13 | l1 | Assignment between incompatible numeric types from 'uint32_t' to 'uint8_t'. |
69+
| test.cpp:519:12:519:13 | l1 | Assignment between incompatible numeric types from 'uint32_t' to 'uint16_t'. |
70+
| test.cpp:521:12:521:13 | l2 | Assignment between incompatible numeric types from 'int32_t' to 'int8_t'. |
71+
| test.cpp:522:12:522:13 | l2 | Assignment between incompatible numeric types from 'int32_t' to 'int16_t'. |
72+
| test.cpp:542:12:542:14 | 300 | Assignment between incompatible numeric types from 'int' to 'uint8_t'. |
73+
| test.cpp:543:12:543:16 | 70000 | Assignment between incompatible numeric types from 'int' to 'uint16_t'. |
74+
| test.cpp:544:12:544:27 | 4294967296 | Assignment between incompatible numeric types from 'unsigned long long' to 'uint32_t'. |
75+
| test.cpp:545:12:545:14 | 200 | Assignment between incompatible numeric types from 'int' to 'int8_t'. |
76+
| test.cpp:546:12:546:16 | 40000 | Assignment between incompatible numeric types from 'int' to 'int16_t'. |
77+
| test.cpp:547:12:547:26 | 4294967296 | Assignment between incompatible numeric types from 'long long' to 'int32_t'. |
78+
| test.cpp:548:12:548:14 | 1.0 | Assignment between incompatible numeric types from 'double' to 'float'. |
79+
| test.cpp:549:12:549:15 | 1.0 | Assignment between incompatible numeric types from 'float' to 'double'. |
80+
| test.cpp:554:12:554:18 | ... + ... | Assignment between incompatible numeric types from 'int' to 'uint8_t'. |
81+
| test.cpp:555:12:555:18 | ... + ... | Assignment between incompatible numeric types from 'int' to 'uint16_t'. |
82+
| test.cpp:556:12:556:18 | ... + ... | Assignment between incompatible numeric types from 'int' to 'uint32_t'. |
83+
| test.cpp:558:12:558:13 | l1 | Assignment between incompatible numeric types from 'uint8_t' to 'int16_t'. |
84+
| test.cpp:559:12:559:13 | l1 | Assignment between incompatible numeric types from 'uint8_t' to 'int32_t'. |
85+
| test.cpp:560:12:560:13 | l1 | Assignment between incompatible numeric types from 'uint8_t' to 'float'. |
86+
| test.cpp:561:12:561:13 | l1 | Assignment between incompatible numeric types from 'uint8_t' to 'double'. |
87+
| test.cpp:591:9:591:37 | static_cast<int16_t>... | Assignment between incompatible numeric types from 'int16_t' to 'int32_t'. |
8888
| test_aggregate.cpp:29:22:29:24 | u16 | Assignment between incompatible numeric types from 'uint16_t' to 'uint8_t'. |
8989
| test_aggregate.cpp:31:26:31:28 | u32 | Assignment between incompatible numeric types from 'uint32_t' to 'uint16_t'. |
9090
| test_aggregate.cpp:33:31:33:33 | u32 | Assignment between incompatible numeric types from 'uint32_t' to 'int32_t'. |

cpp/misra/test/rules/RULE-7-0-6/test.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,22 +326,28 @@ void test_constructor_exception() {
326326
MyInt l1{s8}; // COMPLIANT
327327
}
328328

329-
// Test template functions - not overload-independent
330329
template <typename T> struct D {
330+
// Overload-independent - f10 parameters are always the same type
331331
void f10(T l1, int l2) {}
332332
void f10(T l1, std::string l2) {}
333+
// Not overload-independent
333334
template <typename S1> void f11(S1 l1, int l2) {}
334335
template <typename S2> void f11(S2 l1, std::string l2) {}
335336
void f11(std::int32_t l1, float f) {}
336337
};
337338

338339
void test_template_functions() {
339-
D<std::size_t> l1;
340-
l1.f10(42, "X"); // COMPLIANT
341-
l1.f10(42, 1); // COMPLIANT
342-
l1.f11<std::size_t>(42, "X"); // NON_COMPLIANT - int not size_t
343-
l1.f11<int>(42, 1); // COMPLIANT - same as specialized type
344-
l1.f11(42, 0.0f); // COMPLIANT - same as specialized type
340+
D<std::uint64_t> l1;
341+
l1.f10(u32, "X"); // COMPLIANT - can widen, because always same type
342+
l1.f10(u32, 1); // COMPLIANT - can widen, because always same type
343+
D<std::uint32_t> l2;
344+
l2.f10(u16, "X"); // COMPLIANT - can widen, because always same type
345+
l2.f10(u16, 1); // COMPLIANT - can widen, because always same type
346+
l1.f11<std::uint64_t>(u32, "X"); // NON_COMPLIANT - not overload-independent
347+
// and not the same type as the parameter
348+
// so cannot widen - must be the same type
349+
l1.f11<std::int32_t>(s32, 1); // COMPLIANT - same as specialized type
350+
l1.f11(s32, 0.0f); // COMPLIANT - matches parameter type
345351
}
346352

347353
// Test initialization forms

0 commit comments

Comments
 (0)