Rule A0-1-2 (required, implementation, automated)
The value returned by a function having a non-void return type that is not an overloaded operator shall be used.
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 builtin operators.
Exception
The return value of a function call may be discarded by use of a static_cast
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.