Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve Tuning Using-Directive Checks #267

@geoffviola

Description

@geoffviola

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

  1. build/namespaces_header_block_literals
  2. build/namespaces_header_block_nonliterals
  3. build/namespaces_header_namespace_literals
  4. build/namespaces_header_namespace_nonliterals
  5. build/namespaces_source_block_literals
  6. build/namespaces_source_block_nonliterals
  7. build/namespaces_source_namespace_literals
  8. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions