devops.cpp.ast.checks.max_anonymous_namespace_size

Limit the size of anonymous namespaces.

Everything inside an anonymous namespace has internal linkage, so it cannot be reached from a unit test. A few small helpers are fine; anything bigger belongs in a named (e.g. detail) namespace where it can be tested.

An anonymous namespace is flagged if it

  • defines any class, struct or union (including class templates), or

  • has more than max_declarations top-level declarations. Functions, function templates, variables and type aliases/typedefs count; a function declared and later defined in the same namespace counts once.

Each namespace { ... } block is checked on its own. Declarations pulled in from other files (e.g. an #include inside the block) are ignored.

Configure the limit via the project TOML file:

[cpp.ast_check_config.maxAnonymousNamespaceSize]
max_declarations = 3

Classes

MaxAnonymousNamespaceSize()

Flag anonymous namespaces holding a class or too many declarations.

class devops.cpp.ast.checks.max_anonymous_namespace_size.MaxAnonymousNamespaceSize[source]

Bases: Check

Flag anonymous namespaces holding a class or too many declarations.

id: str = 'maxAnonymousNamespaceSize'

unique id, used as the cppcheck-style [id] suffix

configure(config)[source]

Load max_declarations from the check’s TOML config block.

Parameters:

config (dict) – Expected shape: {"max_declarations": int}; the key is optional.

Raises:

ConfigError – If max_declarations is not a non-negative integer.

Return type:

None

visit(cursor, filename)[source]

Flag cursor if it is an anonymous namespace that is too large.

Parameters:
  • cursor (clang.Cursor) – The AST node currently being visited.

  • filename (str) – Path of the file being checked.

Returns:

list[Diagnostic] – A single diagnostic at the namespace’s opening location if it is too large, otherwise an empty list.

Return type:

list[Diagnostic]