Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions change_notes/2026-02-10-share-autosar-0-1-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- `A0-1-2` - `UnusedReturnValueAutosar.ql`:
- Refactors the rule implementation into a shared library for usage in MISRA C++ ruleset.
- Moves old query file from `UnusedReturnValue.ql` to `UnusedReturnValueAutosar.ql` to distinguish from the MISRA C++ version of the rule.
50 changes: 0 additions & 50 deletions cpp/autosar/src/rules/A0-1-2/UnusedReturnValue.ql

This file was deleted.

25 changes: 25 additions & 0 deletions cpp/autosar/src/rules/A0-1-2/UnusedReturnValueAutosar.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @id cpp/autosar/unused-return-value-autosar
* @name A0-1-2: Unused return value
* @description The value returned by a function having a non-void return type that is not an
* overloaded operator shall be used.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/autosar/id/a0-1-2
* readability
* maintainability
* external/autosar/allocated-target/implementation
* external/autosar/enforcement/automated
* external/autosar/obligation/required
*/

import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.rules.unusedreturnvalue.UnusedReturnValue

module UnusedReturnValueAutosarConfig implements UnusedReturnValueConfigSig {
Query getQuery() { result = DeadCodePackage::unusedReturnValueAutosarQuery() }
}

import UnusedReturnValue<UnusedReturnValueAutosarConfig>
1 change: 0 additions & 1 deletion cpp/autosar/test/rules/A0-1-2/UnusedReturnValue.expected

This file was deleted.

1 change: 0 additions & 1 deletion cpp/autosar/test/rules/A0-1-2/UnusedReturnValue.qlref

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp/common/test/rules/unusedreturnvalue/UnusedReturnValue.ql
16 changes: 8 additions & 8 deletions cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode.qll
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import codingstandards.cpp.exclusions.RuleMetadata

newtype DeadCodeQuery =
TUselessAssignmentQuery() or
TUnusedReturnValueQuery() or
TUnusedReturnValueAutosarQuery() or
TUnusedLocalFunctionQuery() or
TUnusedParameterQuery() or
TUnusedVirtualParameterQuery() or
Expand Down Expand Up @@ -33,11 +33,11 @@ predicate isDeadCodeQueryMetadata(Query query, string queryId, string ruleId, st
category = "required"
or
query =
// `Query` instance for the `unusedReturnValue` query
DeadCodePackage::unusedReturnValueQuery() and
// `Query` instance for the `unusedReturnValueAutosar` query
DeadCodePackage::unusedReturnValueAutosarQuery() and
queryId =
// `@id` for the `unusedReturnValue` query
"cpp/autosar/unused-return-value" and
// `@id` for the `unusedReturnValueAutosar` query
"cpp/autosar/unused-return-value-autosar" and
ruleId = "A0-1-2" and
category = "required"
or
Expand Down Expand Up @@ -185,11 +185,11 @@ module DeadCodePackage {
TQueryCPP(TDeadCodePackageQuery(TUselessAssignmentQuery()))
}

Query unusedReturnValueQuery() {
Query unusedReturnValueAutosarQuery() {
//autogenerate `Query` type
result =
// `Query` type for `unusedReturnValue` query
TQueryCPP(TDeadCodePackageQuery(TUnusedReturnValueQuery()))
// `Query` type for `unusedReturnValueAutosar` query
TQueryCPP(TDeadCodePackageQuery(TUnusedReturnValueAutosarQuery()))
}

Query unusedLocalFunctionQuery() {
Expand Down
26 changes: 26 additions & 0 deletions cpp/common/src/codingstandards/cpp/exclusions/cpp/DeadCode6.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
import cpp
import RuleMetadata
import codingstandards.cpp.exclusions.RuleMetadata

newtype DeadCode6Query = TUnusedReturnValueMisraCppQuery()

predicate isDeadCode6QueryMetadata(Query query, string queryId, string ruleId, string category) {
query =
// `Query` instance for the `unusedReturnValueMisraCpp` query
DeadCode6Package::unusedReturnValueMisraCppQuery() and
queryId =
// `@id` for the `unusedReturnValueMisraCpp` query
"cpp/misra/unused-return-value-misra-cpp" and
ruleId = "RULE-0-1-2" and
category = "required"
}

module DeadCode6Package {
Query unusedReturnValueMisraCppQuery() {
//autogenerate `Query` type
result =
// `Query` type for `unusedReturnValueMisraCpp` query
TQueryCPP(TDeadCode6PackageQuery(TUnusedReturnValueMisraCppQuery()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Conversions2
import DeadCode
import DeadCode3
import DeadCode4
import DeadCode6
import Declarations
import ExceptionSafety
import Exceptions1
Expand Down Expand Up @@ -91,6 +92,7 @@ newtype TCPPQuery =
TDeadCodePackageQuery(DeadCodeQuery q) or
TDeadCode3PackageQuery(DeadCode3Query q) or
TDeadCode4PackageQuery(DeadCode4Query q) or
TDeadCode6PackageQuery(DeadCode6Query q) or
TDeclarationsPackageQuery(DeclarationsQuery q) or
TExceptionSafetyPackageQuery(ExceptionSafetyQuery q) or
TExceptions1PackageQuery(Exceptions1Query q) or
Expand Down Expand Up @@ -163,6 +165,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
isDeadCodeQueryMetadata(query, queryId, ruleId, category) or
isDeadCode3QueryMetadata(query, queryId, ruleId, category) or
isDeadCode4QueryMetadata(query, queryId, ruleId, category) or
isDeadCode6QueryMetadata(query, queryId, ruleId, category) or
isDeclarationsQueryMetadata(query, queryId, ruleId, category) or
isExceptionSafetyQueryMetadata(query, queryId, ruleId, category) or
isExceptions1QueryMetadata(query, queryId, ruleId, category) or
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Provides a configurable module UnusedReturnValue with a `problems` predicate
* for the following issue:
* The value returned by a function having a non-void return type that is not an
* overloaded operator shall be used.
*/

import cpp
import codingstandards.cpp.Customizations
import codingstandards.cpp.Exclusions

signature module UnusedReturnValueConfigSig {
Query getQuery();
}

module UnusedReturnValue<UnusedReturnValueConfigSig Config> {
/*
* This query performs a simple syntactic check to ensure that the return value of the function is
* not completely ignored. This matches the examples given in the rule, although the text itself is
* not entirely clear. This means it will not find cases where something is done with the return
* value, but it is not meaningfully read. For example: `int ret_val = f();`, with no subsequent
* access of `ret_val`. However, such a case _would_ be flagged by A0-1-1 - Useless assignment.
*/

query predicate problems(FunctionCall fc, string message, Function f, string funcName) {
not isExcluded(fc, Config::getQuery()) and
message = "Return value from call to $@ is unused." and
funcName = f.getName() and
// Find function calls in `ExprStmt`s, which indicate the return value is ignored
fc.getParent() instanceof ExprStmt and
// Ignore calls to void functions, which don't return values
not fc.getUnderlyingType() instanceof VoidType and
// Get the function target
f = fc.getTarget() and
// Overloaded (i.e. user defined) operators should behave in the same way as built-in operators,
// so the rule does not require the use of the return value
not f instanceof Operator and
// Exclude cases where the function call is generated within a macro, as the user of the macro is
// not necessarily able to address those results
not fc.isAffectedByMacro() and
// Rule allows disabling this rule where a static_cast<void> or a C-style cast to void is applied
not exists(Cast cast | cast instanceof StaticCast or cast instanceof CStyleCast |
fc.getExplicitlyConverted() = cast and
cast.getActualType() instanceof VoidType
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| test.cpp:15:3:15:3 | call to f | Return value from call to $@ is unused. | test.cpp:3:5:3:5 | f | f |
8 changes: 8 additions & 0 deletions cpp/common/test/rules/unusedreturnvalue/UnusedReturnValue.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// GENERATED FILE - DO NOT MODIFY
import codingstandards.cpp.rules.unusedreturnvalue.UnusedReturnValue

module TestFileConfig implements UnusedReturnValueConfigSig {
Query getQuery() { result instanceof TestQuery }
}

import UnusedReturnValue<TestFileConfig>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ void g(int x);

class A {
public:
A() {
g(2); // Make constructor non-trivial so its call is always extracted.
}
A operator+(const A &other);
};

Expand All @@ -21,6 +24,7 @@ void test_return_val() {
a1 + a2; // COMPLIANT - `+` is a call to operator+, but is permitted by the
// rule

A a3{}; // COMPLIANT - not function call syntax.
(void)f(); // COMPLIANT - explicitly ignoring the return value by C-style cast
// to void.
std::ignore = f(); // COMPLIANT - explicitly ignoring the return value by
Expand Down
23 changes: 23 additions & 0 deletions cpp/misra/src/rules/RULE-0-1-2/UnusedReturnValueMisraCpp.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @id cpp/misra/unused-return-value-misra-cpp
* @name RULE-0-1-2: The value returned by a function shall be used
* @description The result of a non-void function shall be used if called with function call syntax.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-0-1-2
* scope/single-translation-unit
* correctness
* external/misra/enforcement/decidable
* external/misra/obligation/required
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.unusedreturnvalue.UnusedReturnValue

module UnusedReturnValueMisraCppConfig implements UnusedReturnValueConfigSig {
Query getQuery() { result = DeadCode6Package::unusedReturnValueMisraCppQuery() }
}

import UnusedReturnValue<UnusedReturnValueMisraCppConfig>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp/common/test/rules/unusedreturnvalue/UnusedReturnValue.ql
3 changes: 2 additions & 1 deletion rule_packages/cpp/DeadCode.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"name": "Unused return value",
"precision": "very-high",
"severity": "warning",
"short_name": "UnusedReturnValue",
"short_name": "UnusedReturnValueAutosar",
"shared_implementation_short_name": "UnusedReturnValue",
"tags": [
"readability",
"maintainability"
Expand Down
26 changes: 26 additions & 0 deletions rule_packages/cpp/DeadCode6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"MISRA-C++-2023": {
"RULE-0-1-2": {
"properties": {
"enforcement": "decidable",
"obligation": "required"
},
"queries": [
{
"description": "The result of a non-void function shall be used if called with function call syntax.",
"kind": "problem",
"name": "The value returned by a function shall be used",
"precision": "very-high",
"severity": "error",
"short_name": "UnusedReturnValueMisraCpp",
"shared_implementation_short_name": "UnusedReturnValue",
"tags": [
"scope/single-translation-unit",
"correctness"
]
}
],
"title": "The value returned by a function shall be used"
}
}
}
2 changes: 1 addition & 1 deletion rules.csv
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ c,MISRA-C-2012,RULE-23-8,Yes,Required,,,A default association shall appear as ei
cpp,MISRA-C++-2023,RULE-0-0-1,Yes,Required,Decidable,Single Translation Unit,A function shall not contain unreachable statements,M0-1-1,DeadCode3,Medium,
cpp,MISRA-C++-2023,RULE-0-0-2,Yes,Advisory,Undecidable,System,Controlling expressions should not be invariant,M0-1-2,DeadCode4,Easy,
cpp,MISRA-C++-2023,RULE-0-1-1,Yes,Advisory,Undecidable,System,A value should not be unnecessarily written to a local object,A0-1-1,DeadCode2,Medium,
cpp,MISRA-C++-2023,RULE-0-1-2,Yes,Required,Decidable,Single Translation Unit,The value returned by a function shall be used,A0-1-2,DeadCode2,Easy,
cpp,MISRA-C++-2023,RULE-0-1-2,Yes,Required,Decidable,Single Translation Unit,The value returned by a function shall be used,A0-1-2,DeadCode6,Easy,
cpp,MISRA-C++-2023,RULE-0-2-1,Yes,Advisory,Decidable,Single Translation Unit,Variables with limited visibility should be used at least once,M0-1-3,DeadCode2,Easy,
cpp,MISRA-C++-2023,RULE-0-2-2,Yes,Required,Decidable,Single Translation Unit,A named function parameter shall be used at least once,"A0-1-4, A0-1-5",DeadCode2,Easy,
cpp,MISRA-C++-2023,RULE-0-2-3,Yes,Advisory,Decidable,Single Translation Unit,Types with limited visibility should be used at least once,A0-1-6,DeadCode2,Easy,
Expand Down
Loading