Skip to content

Commit 7736c34

Browse files
authored
Merge pull request #732 from fjatWbyT/fix-constexpr-array-size-a0-1-1
A0-1-1: Fix useless assignment false positive on constexpr array size
2 parents 2e8a503 + 7dfb343 commit 7736c34

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `A0-1-1` - `UselessAssignments.qll`:
2+
- Remove (dead code) useless assignment false positive when integer constant expression is used to define the size of an array.

cpp/autosar/test/rules/A0-1-1/UselessAssignment.expected

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@
1212
| test.cpp:94:11:94:17 | new | Definition of $@ is unused. | test.cpp:94:6:94:7 | b4 | b4 |
1313
| test.cpp:95:11:95:17 | 0 | Definition of $@ is unused. | test.cpp:95:6:95:7 | b5 | b5 |
1414
| test.cpp:103:11:103:17 | 0 | Definition of $@ is unused. | test.cpp:103:6:103:7 | c5 | c5 |
15+
| test.cpp:132:43:132:45 | {...} | Definition of $@ is unused. | test.cpp:132:7:132:18 | unused_array | unused_array |
16+
| test.cpp:134:29:134:31 | 0 | Definition of $@ is unused. | test.cpp:134:17:134:26 | unused_int | unused_int |

cpp/autosar/test/rules/A0-1-1/test.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,16 @@ template <typename T> void test_range_based_for_loop_template() {
123123
// template
124124
elem;
125125
}
126-
}
126+
}
127+
128+
#include <cstdint>
129+
130+
std::int32_t test_constexpr_array_size() {
131+
constexpr int constexpr_array_size = 7; // COMPLIANT
132+
int unused_array[constexpr_array_size] = {}; // NON_COMPLIANT
133+
134+
constexpr int unused_int = {}; // NON_COMPLIANT
135+
136+
std::int32_t used_array[] = {-1, 0, 1}; // COMPLIANT
137+
return used_array[1];
138+
}

cpp/common/src/codingstandards/cpp/deadcode/UselessAssignments.qll

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import cpp
6+
import codingstandards.cpp.deadcode.UnusedVariables
67
import codingstandards.cpp.enhancements.ControlFlowGraphEnhancements
78

89
/** If a variable may escape from the local context */
@@ -47,7 +48,9 @@ class InterestingStackVariable extends StackVariable {
4748
// Ignore variables in uninstantiated templates
4849
not this.isFromUninstantiatedTemplate(_) and
4950
// Ignore compiler generated variables, such as those generated for range based for loops
50-
not this.isCompilerGenerated()
51+
not this.isCompilerGenerated() and
52+
// Explicitly ignore (propagated) constants that may be used to define sizes of local arrays
53+
not countUsesInLocalArraySize(this) > 0
5154
}
5255
}
5356

0 commit comments

Comments
 (0)