Skip to content

Commit 54418e4

Browse files
Copilot feedback
1 parent 5d7db89 commit 54418e4

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

cpp/misra/src/rules/RULE-28-6-4/PotentiallyErroneousContainerUsage.ql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,23 @@
1616

1717
import cpp
1818
import codingstandards.cpp.misra
19+
import codingstandards.cpp.Iterators
1920

2021
predicate isRemoveOrUniqueCall(FunctionCall fc) {
2122
exists(string name | name = fc.getTarget().getName() |
2223
name = "remove" or
2324
name = "remove_if" or
2425
name = "unique"
2526
) and
26-
fc.getTarget().hasQualifiedName("std", _)
27+
fc.getTarget().hasQualifiedName("std", _) and
28+
fc.getAnArgument().getUnderlyingType() instanceof IteratorType
2729
}
2830

2931
predicate isEmptyCall(FunctionCall fc) {
3032
fc.getTarget().getName() = "empty" and
3133
(
3234
fc.getTarget().hasQualifiedName("std", "empty") or
33-
fc.getTarget() instanceof MemberFunction
35+
fc.getTarget().(MemberFunction).getDeclaringType().hasQualifiedName("std", _)
3436
)
3537
}
3638

cpp/misra/test/rules/RULE-28-6-4/PotentiallyErroneousContainerUsage.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
| test.cpp:100:3:100:12 | call to empty | Result of call to 'empty' is not used. |
77
| test.cpp:117:3:117:13 | call to remove | Result of call to 'remove' is not used. |
88
| test.cpp:122:6:122:10 | call to empty | Result of call to 'empty' is not used. |
9+
| test.cpp:148:21:148:31 | call to remove | Result of call to 'remove' is not used. |
10+
| test.cpp:149:9:149:19 | call to remove | Result of call to 'remove' is not used. |

cpp/misra/test/rules/RULE-28-6-4/test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,29 @@ void test_remove_chained_operations() {
141141
std::vector<std::int32_t> v2 = {5, 6, 7};
142142
v1.erase(std::remove(v1.begin(), v1.end(), 2), v1.end()); // COMPLIANT
143143
v2.erase(std::remove(v2.begin(), v2.end(), 6), v2.end()); // COMPLIANT
144+
}
145+
146+
void test_cast_to_void() {
147+
std::vector<std::int32_t> v1 = {1, 2, 3, 2, 4};
148+
static_cast<void>(std::remove(v1.begin(), v1.end(), 2)); // NON_COMPLIANT
149+
(void)std::remove(v1.begin(), v1.end(), 2); // NON_COMPLIANT
150+
}
151+
152+
void test_unrelated_empty() {
153+
class MyClass {
154+
std::vector<int> v1;
155+
156+
public:
157+
void empty() { v1.clear(); }
158+
};
159+
160+
MyClass c1;
161+
c1.empty(); // COMPLIANT - empty is not a member function of a standard
162+
}
163+
164+
#include <cstdio>
165+
void test_cstdio_remove() {
166+
std::remove("filename.txt"); // COMPLIANT - std::remove from <cstdio>
167+
remove("filename.txt"); // COMPLIANT - std::remove from <cstdio> with using
168+
// directive
144169
}

0 commit comments

Comments
 (0)