-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathPotentiallyErroneousContainerUsage.ql
More file actions
40 lines (36 loc) · 1.48 KB
/
PotentiallyErroneousContainerUsage.ql
File metadata and controls
40 lines (36 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @id cpp/misra/potentially-erroneous-container-usage
* @name RULE-28-6-4: The result of std::remove, std::remove_if, std::unique and empty shall be used
* @description The `empty` method may be confused with `clear`, and the family of algorithms
* similar to `std::remove` require the resulting iterator to be used in order to
* properly handle the modifications performed.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-28-6-4
* scope/single-translation-unit
* correctness
* external/misra/enforcement/decidable
* external/misra/obligation/required
*/
import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.standardlibrary.Iterators
predicate isRemoveOrUniqueCall(FunctionCall fc) {
fc.getTarget().hasQualifiedName("std", ["remove", "remove_if", "unique"]) and
fc.getAnArgument().getUnderlyingType() instanceof IteratorType
}
predicate isEmptyCall(FunctionCall fc) {
fc.getTarget().getName() = "empty" and
(
fc.getTarget().hasQualifiedName("std", "empty") or
fc.getTarget().(MemberFunction).getDeclaringType().hasQualifiedName("std", _)
)
}
from FunctionCall fc, string message
where
not isExcluded(fc, DeadCode11Package::potentiallyErroneousContainerUsageQuery()) and
fc = any(ExprStmt es).getExpr() and
(isRemoveOrUniqueCall(fc) or isEmptyCall(fc)) and
message = "Result of call to '" + fc.getTarget().getName() + "' is not used."
select fc, message