llvm-prefer-static-over-anonymous-namespace¶
Finds function and variable declarations inside anonymous namespace and
suggests replacing them with static
declarations.
The LLVM Coding Standards
recommend keeping anonymous namespaces as small as possible and only use them
for class declarations. For functions and variables the static
specifier
should be preferred for restricting visibility.
For example non-compliant code:
namespace {
class StringSort {
public:
StringSort(...)
bool operator<(const char *RHS) const;
};
// warning: place method definition outside of an anonymous namespace
bool StringSort::operator<(const char *RHS) const {}
// warning: prefer using 'static' for restricting visibility
void runHelper() {}
// warning: prefer using 'static' for restricting visibility
int myVariable = 42;
}
Should become:
// Small anonymous namespace for class declaration
namespace {
class StringSort {
public:
StringSort(...)
bool operator<(const char *RHS) const;
};
}
// placed method definition outside of the anonymous namespace
bool StringSort::operator<(const char *RHS) const {}
// used 'static' instead of an anonymous namespace
static void runHelper() {}
// used 'static' instead of an anonymous namespace
static int myVariable = 42;
Options¶
- AllowVariableDeclarations¶
When true, allow variable declarations to be in anonymous namespace. Default value is true.
- AllowMemberFunctionsInClass¶
When true, only methods defined in anonymous namespace outside of the corresponding class will be warned. Default value is true.