Skip to content

Commit b9d8b1e

Browse files
Implement DeadCode6, RULE-0-1-2, shared with A0-1-2.
Uses new shared library format templates.
1 parent e140430 commit b9d8b1e

File tree

18 files changed

+183
-62
lines changed

18 files changed

+183
-62
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- `A0-1-2` - `UnusedReturnValue.ql`:
2+
- Rule implementation has refactored into a shared library for usage in MISRA C++ ruleset.
3+
- Query file renamed to `UnusedReturnValueAutosar.ql` to distinguish from the MISRA C++ version of the rule.

cpp/autosar/src/rules/A0-1-2/UnusedReturnValue.ql

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @id cpp/autosar/unused-return-value-autosar
3+
* @name A0-1-2: Unused return value
4+
* @description The value returned by a function having a non-void return type that is not an
5+
* overloaded operator shall be used.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity warning
9+
* @tags external/autosar/id/a0-1-2
10+
* readability
11+
* maintainability
12+
* external/autosar/allocated-target/implementation
13+
* external/autosar/enforcement/automated
14+
* external/autosar/obligation/required
15+
*/
16+
17+
import cpp
18+
import codingstandards.cpp.autosar
19+
import codingstandards.cpp.rules.unusedreturnvalue.UnusedReturnValue
20+
21+
module UnusedReturnValueAutosarConfig implements UnusedReturnValueConfigSig {
22+
Query getQuery() {
23+
result = DeadCodePackage::unusedReturnValueAutosarQuery()
24+
}
25+
}
26+
27+
import UnusedReturnValue<UnusedReturnValueAutosarConfig>

cpp/autosar/test/rules/A0-1-2/UnusedReturnValue.expected

Lines changed: 0 additions & 1 deletion
This file was deleted.

cpp/autosar/test/rules/A0-1-2/UnusedReturnValue.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/unusedreturnvalue/UnusedReturnValue.ql

cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode.qll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import codingstandards.cpp.exclusions.RuleMetadata
55

66
newtype DeadCodeQuery =
77
TUselessAssignmentQuery() or
8-
TUnusedReturnValueQuery() or
8+
TUnusedReturnValueAutosarQuery() or
99
TUnusedLocalFunctionQuery() or
1010
TUnusedParameterQuery() or
1111
TUnusedVirtualParameterQuery() or
@@ -33,11 +33,11 @@ predicate isDeadCodeQueryMetadata(Query query, string queryId, string ruleId, st
3333
category = "required"
3434
or
3535
query =
36-
// `Query` instance for the `unusedReturnValue` query
37-
DeadCodePackage::unusedReturnValueQuery() and
36+
// `Query` instance for the `unusedReturnValueAutosar` query
37+
DeadCodePackage::unusedReturnValueAutosarQuery() and
3838
queryId =
39-
// `@id` for the `unusedReturnValue` query
40-
"cpp/autosar/unused-return-value" and
39+
// `@id` for the `unusedReturnValueAutosar` query
40+
"cpp/autosar/unused-return-value-autosar" and
4141
ruleId = "A0-1-2" and
4242
category = "required"
4343
or
@@ -185,11 +185,11 @@ module DeadCodePackage {
185185
TQueryCPP(TDeadCodePackageQuery(TUselessAssignmentQuery()))
186186
}
187187

188-
Query unusedReturnValueQuery() {
188+
Query unusedReturnValueAutosarQuery() {
189189
//autogenerate `Query` type
190190
result =
191-
// `Query` type for `unusedReturnValue` query
192-
TQueryCPP(TDeadCodePackageQuery(TUnusedReturnValueQuery()))
191+
// `Query` type for `unusedReturnValueAutosar` query
192+
TQueryCPP(TDeadCodePackageQuery(TUnusedReturnValueAutosarQuery()))
193193
}
194194

195195
Query unusedLocalFunctionQuery() {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype DeadCode6Query = TUnusedReturnValueMisraCppQuery()
7+
8+
predicate isDeadCode6QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `unusedReturnValueMisraCpp` query
11+
DeadCode6Package::unusedReturnValueMisraCppQuery() and
12+
queryId =
13+
// `@id` for the `unusedReturnValueMisraCpp` query
14+
"cpp/misra/unused-return-value-misra-cpp" and
15+
ruleId = "RULE-0-1-2" and
16+
category = "required"
17+
}
18+
19+
module DeadCode6Package {
20+
Query unusedReturnValueMisraCppQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `unusedReturnValueMisraCpp` query
24+
TQueryCPP(TDeadCode6PackageQuery(TUnusedReturnValueMisraCppQuery()))
25+
}
26+
}

cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Conversions2
1919
import DeadCode
2020
import DeadCode3
2121
import DeadCode4
22+
import DeadCode6
2223
import Declarations
2324
import ExceptionSafety
2425
import Exceptions1
@@ -91,6 +92,7 @@ newtype TCPPQuery =
9192
TDeadCodePackageQuery(DeadCodeQuery q) or
9293
TDeadCode3PackageQuery(DeadCode3Query q) or
9394
TDeadCode4PackageQuery(DeadCode4Query q) or
95+
TDeadCode6PackageQuery(DeadCode6Query q) or
9496
TDeclarationsPackageQuery(DeclarationsQuery q) or
9597
TExceptionSafetyPackageQuery(ExceptionSafetyQuery q) or
9698
TExceptions1PackageQuery(Exceptions1Query q) or
@@ -163,6 +165,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
163165
isDeadCodeQueryMetadata(query, queryId, ruleId, category) or
164166
isDeadCode3QueryMetadata(query, queryId, ruleId, category) or
165167
isDeadCode4QueryMetadata(query, queryId, ruleId, category) or
168+
isDeadCode6QueryMetadata(query, queryId, ruleId, category) or
166169
isDeclarationsQueryMetadata(query, queryId, ruleId, category) or
167170
isExceptionSafetyQueryMetadata(query, queryId, ruleId, category) or
168171
isExceptions1QueryMetadata(query, queryId, ruleId, category) or
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Provides a configurable module UnusedReturnValue with a `problems` predicate
3+
* for the following issue:
4+
* The value returned by a function having a non-void return type that is not an
5+
* overloaded operator shall be used.
6+
*/
7+
8+
import cpp
9+
import codingstandards.cpp.Customizations
10+
import codingstandards.cpp.Exclusions
11+
12+
signature module UnusedReturnValueConfigSig {
13+
Query getQuery();
14+
}
15+
16+
module UnusedReturnValue<UnusedReturnValueConfigSig Config> {
17+
/*
18+
* This query performs a simple syntactic check to ensure that the return value of the function is
19+
* not completely ignored. This matches the examples given in the rule, although the text itself is
20+
* not entirely clear. This means it will not find cases where something is done with the return
21+
* value, but it is not meaningfully read. For example: `int ret_val = f();`, with no subsequent
22+
* access of `ret_val`. However, such a case _would_ be flagged by A0-1-1 - Useless assignment.
23+
*/
24+
25+
query predicate problems(FunctionCall fc, string message, Function f, string funcName) {
26+
not isExcluded(fc, Config::getQuery()) and
27+
message = "Return value from call to $@ is unused." and
28+
funcName = f.getName() and
29+
// Find function calls in `ExprStmt`s, which indicate the return value is ignored
30+
fc.getParent() instanceof ExprStmt and
31+
// Ignore calls to void functions, which don't return values
32+
not fc.getUnderlyingType() instanceof VoidType and
33+
// Get the function target
34+
f = fc.getTarget() and
35+
// Overloaded (i.e. user defined) operators should behave in the same way as built-in operators,
36+
// so the rule does not require the use of the return value
37+
not f instanceof Operator and
38+
// Exclude cases where the function call is generated within a macro, as the user of the macro is
39+
// not necessarily able to address those results
40+
not fc.isAffectedByMacro() and
41+
// Rule allows disabling this rule where a static_cast<void> or a C-style cast to void is applied
42+
not exists(Cast cast | cast instanceof StaticCast or cast instanceof CStyleCast |
43+
fc.getExplicitlyConverted() = cast and
44+
cast.getActualType() instanceof VoidType
45+
)
46+
}
47+
}

0 commit comments

Comments
 (0)