Main Content

AUTOSAR C++14 Rule A0-1-2

The value returned by a function having a non-void return type that is not an overloaded operator shall be used

Description

Rule Definition

The value returned by a function having a non-void return type that is not an overloaded operator shall be used.

Rationale

The unused return value can indicate a coding error or oversight.

Overloaded operators are excluded from this rule because their usage must emulate built-in operators that might not use their return value.

Polyspace Implementation

The rule checker reports a violation on functions with a non-void return type if the return value is not used or not explicitly cast to a void type.

The checker does not flag the functions memcpy, memset, memmove, strcpy, strncpy, strcat, strncat because these functions simply return a pointer to their first arguments.

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <iostream>
#include <new>
#include <algorithm>
#include <cstdint>
#include <vector>

int assignMemory(int * ptr){
     int res = 1;
     ptr = new (std::nothrow) int;
     if(ptr==NULL) {
         res = 0;
     }
     return res;
}
void foo()
{


}

void main() {
	int val;
	int status;
	std::vector<std::int8_t> numVec{10,10,20,20,30,40,50,50,60};

	assignMemory(&val);   //Noncompliant
	status = assignMemory(&val); //Compliant
	(void)assignMemory(&val); //Compliant

	numVec.erase(std::unique(numVec.begin(), numVec.end()), numVec.end());// Noncompliant
}

In this example, Polyspace flags calls to functions with unused return value:

  • Because main does not use the return value of the first call to the function assignMemory, Polyspace reports a violation.

  • Because the return value of the second call to assignMemory is assigned to a local variable, it is compliant with this rule.

  • Because the third call to assignMemory is cast to void, it is compliant with this rule

  • Because main does not use the object returned by the function std::vector::erase(), Polyspace reports a violation.

Check Information

Group: Language Independent Issues
Category: Required, Automated

Version History

Introduced in R2019a

expand all