-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathPotentiallyErroneousContainerUsage.ql
More file actions
50 lines (46 loc) · 1.65 KB
/
PotentiallyErroneousContainerUsage.ql
File metadata and controls
50 lines (46 loc) · 1.65 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
41
42
43
44
45
46
47
48
49
50
/**
* @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.Iterators
predicate isRemoveOrUniqueCall(FunctionCall fc) {
exists(string name | name = fc.getTarget().getName() |
name = "remove" or
name = "remove_if" or
name = "unique"
) and
fc.getTarget().hasQualifiedName("std", _) 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
exists(ExprStmt es | es.getExpr() = fc) and
(
isRemoveOrUniqueCall(fc) and
message = "Result of call to '" + fc.getTarget().getName() + "' is not used."
or
isEmptyCall(fc) and
message = "Result of call to 'empty' is not used."
)
select fc, message