Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A0-1-1: Fix useless assignment false positive on constexpr array size #732

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions change_notes/2024-10-04-fix-constexpr-arr-size-fp-a0-1-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `A0-1-1` - `UselessAssignments.qll`:
- Remove (dead code) useless assignment false positive when integer constant expression is used to define the size of an array.
2 changes: 2 additions & 0 deletions cpp/autosar/test/rules/A0-1-1/UselessAssignment.expected
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
| test.cpp:94:11:94:17 | new | Definition of $@ is unused. | test.cpp:94:6:94:7 | b4 | b4 |
| test.cpp:95:11:95:17 | 0 | Definition of $@ is unused. | test.cpp:95:6:95:7 | b5 | b5 |
| test.cpp:103:11:103:17 | 0 | Definition of $@ is unused. | test.cpp:103:6:103:7 | c5 | c5 |
| test.cpp:132:43:132:45 | {...} | Definition of $@ is unused. | test.cpp:132:7:132:18 | unused_array | unused_array |
| test.cpp:134:29:134:31 | 0 | Definition of $@ is unused. | test.cpp:134:17:134:26 | unused_int | unused_int |
14 changes: 13 additions & 1 deletion cpp/autosar/test/rules/A0-1-1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,16 @@ template <typename T> void test_range_based_for_loop_template() {
// template
elem;
}
}
}

#include <cstdint>

std::int32_t test_constexpr_array_size() {
constexpr int constexpr_array_size = 7; // COMPLIANT
int unused_array[constexpr_array_size] = {}; // NON_COMPLIANT

constexpr int unused_int = {}; // NON_COMPLIANT

std::int32_t used_array[] = {-1, 0, 1}; // COMPLIANT
return used_array[1];
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import cpp
import codingstandards.cpp.deadcode.UnusedVariables
import codingstandards.cpp.enhancements.ControlFlowGraphEnhancements

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

Expand Down
Loading