Main Content

AUTOSAR C++14 Rule A0-1-3

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

Since R2020b

Description

Rule Definition

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

Rationale

Functions defined in an anonymous namespace and static functions with internal linkage are callable only inside the compilation unit in which they are defined. Similarly, private member functions are callable only inside the class implementation that they belong to. In both these cases, such functions are intended to be used exclusively in the current source code and not in external code that is integrated later on into the project. Not using such functions indicates poor software design or missing logic in the current code base.

Note

An explicit function call in the source code is sufficient to satisfy this rule, even if the call is not reachable at run time. A separate rule, M0-1-1, checks for all unreachable code occurrences.

Polyspace Implementation

If a function defined in your source code is not called explicitly and belongs to one of these categories, the checker flags the function definition:

  • Functions defined in anonymous namespace

  • Static functions with internal linkage

  • Private member functions that are defined outside the class definition

The checker does not flag an uncalled private member function that is defined inside the class definition.

The checker does not flag private member functions that are defined outside the class definition in a default Polyspace® as You Code analysis. See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access).

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 <cstdint>

namespace
{
  void F1()   // Compliant, function in anonymous namespace used
  {
  }
  
  void F2()   // Noncompliant, function in anonymous namespace not used
  {
  }
}

int main()
{
  F1();
  return 0;
}

The static function F2 is defined in an anonymous namespace but is not called from the main function, thus violating this coding rule.

#include <cstdint>
static void F1() // Compliant, static function called from main
{
}

static void F2() // Noncompliant, static function not called from main
{
}

int main()
{
  F1();
  return 0;
}

The static function F2 has internal linkage but is not called from the main function, thus violating this coding rule.

#include <cstdint>

class C
{
  public:
    C() : x(0) {}
    void M1(std::int32_t);                
    void M2(std::int32_t, std::int32_t);  
  private:
    std::int32_t x;
    void M1PrivateImpl(std::int32_t j);    
};

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

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

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

int main()
{
  C c;
  c.M1(1);
  return 0;
}

The private member function M1PrivateImpl is not called from any member of the class C, thus violating this coding rule.

Check Information

Group: Language independent issues
Category: Required, Automated

Version History

Introduced in R2020b