Skip to content

Commit 4ca4133

Browse files
committed
Fix false positives in RULE-6-9-2/A3-9-1 for auto-deduced types
Variables declared with 'auto' or 'decltype(auto)' should not be flagged when the deduced type resolves through fixed-width typedefs (e.g. uint32_t) to a built-in integer type. The programmer never explicitly wrote a variable-width type name in these cases. This is analogous to the existing template instantiation exclusion (#540). Fixes: #1145
1 parent 883a46f commit 4ca4133

4 files changed

Lines changed: 20 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `RULE-6-9-2`, `A3-9-1` - `VariableWidthIntegerTypesUsed.qll`:
2+
- Fixed false positives for variables declared with `auto` or `decltype(auto)` where the deduced type resolves through fixed-width typedefs (e.g., `std::uint32_t`) to a built-in integer type. The programmer never wrote a variable-width type name in these cases.

cpp/common/src/codingstandards/cpp/rules/variablewidthintegertypesused/VariableWidthIntegerTypesUsed.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ query predicate problems(Element e, string message) {
3333
// Fixed Width Types are recorded after stripping their typedef'd type,
3434
// thereby, causing false positives (#540).
3535
not v.isFromTemplateInstantiation(_) and
36+
// Dont consider variables declared with `auto` or `decltype(auto)` because
37+
// the deduced type may resolve through fixed-width typedefs (e.g. uint32_t)
38+
// to a built-in type, even though the programmer never wrote that type name.
39+
not v.declaredUsingAutoType() and
3640
//post-increment/post-decrement operators are required by the standard to have a dummy int parameter
3741
not v.(Parameter).getFunction() instanceof PostIncrementOperator and
3842
not v.(Parameter).getFunction() instanceof PostDecrementOperator and

cpp/common/test/rules/variablewidthintegertypesused/VariableWidthIntegerTypesUsed.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@
5050
| test.cpp:123:6:123:21 | test_long_return | Function 'test_long_return' has variable-width return type. |
5151
| test.cpp:126:15:126:39 | test_unsigned_long_return | Function 'test_unsigned_long_return' has variable-width return type. |
5252
| test.cpp:129:13:129:35 | test_signed_long_return | Function 'test_signed_long_return' has variable-width return type. |
53+
| test.cpp:160:5:160:11 | get_int | Function 'get_int' has variable-width return type. |
54+
| test.cpp:166:7:166:18 | explicit_int | Variable 'explicit_int' has variable-width type. |

cpp/common/test/rules/variablewidthintegertypesused/test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,15 @@ std::uint32_t test_uint32_t_return() { // COMPLIANT
153153
std::uint64_t test_uint64_t_return() { // COMPLIANT
154154
return 60;
155155
}
156+
157+
// Regression test: auto-deduced types should not be flagged even when
158+
// the deduced type resolves through fixed-width typedefs to a built-in type.
159+
std::uint32_t get_uint32() { return 0; }
160+
int get_int() { return 0; }
161+
162+
void test_auto_deduced_types() {
163+
auto a1 = get_uint32(); // COMPLIANT - auto deduces through uint32_t
164+
auto a2 = get_int(); // COMPLIANT - auto, programmer didn't write 'int'
165+
const auto a3 = 42U; // COMPLIANT - auto
166+
int explicit_int = 0; // NON_COMPLIANT - explicit variable-width type
167+
}

0 commit comments

Comments
 (0)