AutosarC++18_10-A0.1.2

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

Required inputs: IR

Rationale

A called function may provide essential information about its process status and result through return statement. Calling a function without using the return value should be a warning that incorrect assumptions about the process were made.

Overloaded operators are excluded, as they should behave in the same way as built-in operators.

Exceptions

The return value of a function call may be discarded by use of a static_cast<void> cast, so intentions of a programmer are explicitly stated.

Example
// $Id: A0-1-2.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
    #include <algorithm>
    #include <cstdint>
    #include <vector>
    std::uint8_t Fn1() noexcept
    {
        return 0U;
    }
    void Fn2() noexcept
    {
        std::uint8_t x = Fn1();    // Compliant
        Fn1();                     // Non-compliant
        static_cast<void>(Fn1()); // Compliant by exception
    }
    void Fn3()
    {
        std::vector<std::int8_t> v{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5};
        std::unique(v.begin(), v.end());                    // Non-compliant
        v.erase(std::unique(v.begin(), v.end()), v.end()); // Compliant
    }
See Also
  • MISRA C++ 2008 [7]: Rule 0-1-7 The value returned by a function having a non- void return type that is not an overloaded operator shall always be used.
  • HIC++ v4.0 [9]: 17.5.1 Do not ignore the result of std::remove, std::remove_if or std::unique.

Possible Messages

Key

Text

Severity

Disabled

discarded_return_with_entity

Return value of function discarded.

None

False

Options

allowed_functions

allowed_functions : set[bauhaus.analysis.config.FunctionName] = {'memcpy', 'memmove', 'memset', 'strcat', 'strcpy', 'strncat', 'strncpy'}

Calls to these functions are ignored.
 

check_operators

check_operators : bool = False

Also check operator calls. Unused return values of assignment operators are only reported if given in function style syntax.
 

report_references

report_references : bool = False

Report returned references. For operators, unused returned references are only reported if given in function style syntax.