-
Notifications
You must be signed in to change notification settings - Fork 293
Description
Problem
The Google style guide states
Do not use using-directives (e.g., using namespace foo)
cpplint rightly checks for that. There is good reasoning on why to forbid using-directives. But it may be too restrictive for some projects. Instead, it would be beneficial to have targeted checks. For example, only allow declarations in source files or in block scope. This checking might allow for restricted literal usage for some projects.
CppCoreGuidelines only recommends
SF.7: Don’t write using namespace at global scope in a header file
It might be nice to follow that guidance instead.
Proposal
Have configurations to check against {header, source}, {block, namespace}, and {literals, nonliterals}. So the options would look like
- build/namespaces_header_block_literals
- build/namespaces_header_block_nonliterals
- build/namespaces_header_namespace_literals
- build/namespaces_header_namespace_nonliterals
- build/namespaces_source_block_literals
- build/namespaces_source_block_nonliterals
- build/namespaces_source_namespace_literals
- build/namespaces_source_namespace_nonliterals
Then, offer configuration styles like clang-format. For example, some might want Google or CppCoreGuidelines. Some places might want to forbid any using-directive in just header files. Others, might want to restrict header file and namespace scope as well as source file, namespace scope, and nonliterals.
Backwards compatibility is an issue. There are already checks for build/namespaces_literals
and build/namespaces
. There are two different checks for build/namespaces
. One checks against using namespace
for nonliterals. The other checks for finding a complete declaration of a namespace. The latter could be renamed. Then, suppression could work by assigning a style, then the current suppressions, then the more specific suppressions.
There's already a function for _isSourceExtension
. Checking for namespace scope could be done by checking for the start of the line (i.e. no whitespace). The literals
check could be kept.
Comments
This expands the scope of cpplint outside of the original intent. Also, C++ modules would hopefully deprecate this feature.