Rule A0-1-3 (required, implementation, automated)

Every function defined in an anonymous namespace, or static function with internal linkage, or private member function shall be used.

Rationale

Functions which are not callable from outside the compilation unit in which they are defined, or from outside the class implementation to which they pertain, and which are not used may be symptomatic of serious problems, such as poor software design or missing paths in flow control. This rule enforces developers to statically and explicitly use every such function in the source code. A function does not necessarily need to be called at run-time. Rule M0-1-1 detects all unreachable code occurrences. Note that this rule applies equally to static and non-static private member functions.

Example

//% $Id: A0-1-3.cpp 291350 2017-10-17 14:31:34Z jan.babst $
#include <cstdint>
static void F1() // Compliant
{
}

namespace
{
void F2() // Non-compliant, defined function never used
{
}
}

class C
{
public:
C() : x(0) {}
void M1(std::int32_t i) // Compliant, member function is used
{
x = i;
}

void M2(std::int32_t i,
std::int32_t j) // Compliant, never used but declared
// as public
{
x = (i > j) ? i : j;
}

protected:
void M1ProtectedImpl(std::int32_t j) // Compliant, never used but declared
// as protected
{
x = j;
}

private:
std::int32_t x;
void M1PrivateImpl(
std::int32_t j) // Non-compliant, private member function never used
{
x = j;
}

};

int main(int, char**)

{
F1();
C c;
c.M1(1);
return 0;

}

See also

MISRA C++ 2008 [7]: Rule 0-1-10 Every defined function shall be called at least once. HIC++ v4.0 [9]: 1.2.2 Ensure that no expression or sub-expression is redundant.