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

Skip to content

enhance_using_directives_267 #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

geoffviola
Copy link
Contributor

@geoffviola geoffviola commented Jul 6, 2025

Description

Enhance the using directives check by adding more granularity to the warnings.
The warnings are split into multiple categories

  1. block/namespace scope
  2. source/header file
  3. literal/nonliteral namespace

Possible filter combinations would look like the following

  1. Google: None
  2. CppCoreGuidelines
    1. -build/namespaces/header/block
    2. -build/namespaces/source
  3. Like CppCoreGuidlines, but disallow non-literal namespaces in source files at namespace scope
    1. -build/namespaces/header/block
    2. -build/namespaces/source/block
    3. -build/namespaces/source/namespace/literal

Tests

Unit Tests

python3 cpplint_unittest.py
python3 cpplint_clitest.py

Configuration tests

my_test.h

// Copyright 2025 Geoffrey Viola

#ifndef MY_TEST_H_
#define MY_TEST_H_

using namespace std;
using namespace std::chrono::literals;

void foo() {
  using namespace std;
  using namespace std::chrono::literals;
}

#endif  // MY_TEST_H_

my_test.cc

// Copyright 2025 Geoffrey Viola

using namespace std;
using namespace std::chrono::literals;

void foo() {
  using namespace std;
  using namespace std::chrono::literals;
}

Tests

python3 cpplint.py my_test.cc
my_test.cc:3:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/source/namespace/nonliterals] [5]
my_test.cc:4:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/source/namespace/literals] [5]
my_test.cc:7:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/source/block/nonliterals] [5]
my_test.cc:8:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/source/block/literals] [5]
Done processing my_test.cc
Total errors found: 4

python3 cpplint.py my_test.h
my_test.h:6:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/namespace/nonliterals] [5]
my_test.h:7:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/namespace/literals] [5]
my_test.h:10:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/block/nonliterals] [5]
my_test.h:11:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/block/literals] [5]
Done processing my_test.h
Total errors found: 4

python3 cpplint.py --filter=-build/namespaces my_test.h
Done processing my_test.h
python3 cpplint.py --filter=-build/namespaces my_test.cc
Done processing my_test.cc

python3 cpplint.py --filter=-build/namespaces/source my_test.h
my_test.h:6:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/namespace/nonliterals] [5]
my_test.h:7:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/namespace/literals] [5]
my_test.h:10:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/block/nonliterals] [5]
my_test.h:11:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/block/literals] [5]
Done processing my_test.h
Total errors found: 4
python3 cpplint.py --filter=-build/namespaces/source my_test.cc
Done processing my_test.cc

python3 cpplint.py --filter=-build/namespaces/source/block my_test.h
my_test.h:6:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/namespace/nonliterals] [5]
my_test.h:7:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/namespace/literals] [5]
my_test.h:10:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/block/nonliterals] [5]
my_test.h:11:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/block/literals] [5]
Done processing my_test.h
Total errors found: 4
python3 cpplint.py --filter=-build/namespaces/source/block my_test.cc
my_test.cc:3:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/source/namespace/nonliterals] [5]
my_test.cc:4:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/source/namespace/literals] [5]
Done processing my_test.cc
Total errors found: 2

python3 cpplint.py --filter=-build/namespaces/source/block/literals my_test.h
my_test.h:6:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/namespace/nonliterals] [5]
my_test.h:7:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/namespace/literals] [5]
my_test.h:10:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/block/nonliterals] [5]
my_test.h:11:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/header/block/literals] [5]
Done processing my_test.h
Total errors found: 4
python3 cpplint.py --filter=-build/namespaces/source/block/literals my_test.cc
my_test.cc:3:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/source/namespace/nonliterals] [5]
my_test.cc:4:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/source/namespace/literals] [5]
my_test.cc:7:  Do not use namespace using-directives.  Use using-declarations instead.  [build/namespaces/source/block/nonliterals] [5]
Done processing my_test.cc
Total errors found: 3

Issues

#267

@geoffviola geoffviola force-pushed the geoffviola/enhance_using_directives_267 branch 4 times, most recently from 9c5534b to a4f110c Compare July 6, 2025 19:10
@aaronliu0130 aaronliu0130 linked an issue Jul 7, 2025 that may be closed by this pull request
Copy link
Member

@aaronliu0130 aaronliu0130 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For backwards compatibility, could we recognize existing filters against build/namespaces and build/namespaces_literals?

@geoffviola geoffviola force-pushed the geoffviola/enhance_using_directives_267 branch from b21510b to f1c7c95 Compare July 7, 2025 20:14
@geoffviola geoffviola force-pushed the geoffviola/enhance_using_directives_267 branch from b2d438e to 68c551e Compare July 7, 2025 20:18
@geoffviola
Copy link
Contributor Author

For backwards compatibility, could we recognize existing filters against build/namespaces and build/namespaces_literals?

Filtering by -build/namespaces will stop all using directive warnings including the literals one. See the test above.

build/namespaces_literals has been split up. It's now the following

  1. build/namespaces/header/block/literals
  2. build/namespaces/header/namespace/literals
  3. build/namespaces/source/block/literals
  4. build/namespaces/source/namespace/literals
    The categorization is broken up like this to enable specifying the most common cases first. We could intercept the build/namespaces_literals and replace it with those 4 items, but that seems complicated and users will probably want to specify it differently anyway.

@geoffviola geoffviola requested a review from aaronliu0130 July 7, 2025 20:24
@aaronliu0130
Copy link
Member

aaronliu0130 commented Jul 8, 2025

We should only break backwards compatibility on major releases (when I have time—not the near future—we should reorganize the error types anyways), so I think we should keep compatibility with namespace_literals for now. I feel like there's probably a reason that was split out in the first place, probably someone wanting to only filter that out.

Also, you don't need to force-push if it squashes into only one commit! GitHub has a "Squash and merge" option.

@geoffviola
Copy link
Contributor Author

We should only break backwards compatibility on major releases (when I have time—not the near future—we should reorganize the error types anyways), so I think we should keep compatibility with namespace_literals for now. I feel like there's probably a reason that was split out in the first place, probably someone wanting to only filter that out.

The goal of this PR is to figure out how cpplint could support google, cppcoreguidelines, and my org's using-directive policies. The develop branch has one category literals build/namespaces_literals and everything else build/namespaces. Those warnings have been around for around a decade: #15. This PR has three categories: source, scope, and literals.

Because of this different categorization, backwards compatibility is difficult. The meaning of build/namespaces is slightly different. build/namespace now means all using-directives not just non-literals. This new meaning is clearer to me. In practice, I could see users either enabling both build/namespaces_literals and build/namespaces, only build/namespaces, or neither.

For backwards compatibility, I made build/namespaces_literals a shortcut for build/namespaces/header/block/literals, build/namespaces/header/namespace/literals, build/namespaces/source/block/literals, and build/namespaces/source/namespace/literals. Let me know if that works for you.

Copy link
Member

@aaronliu0130 aaronliu0130 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the meaning of build/namespace is not ideal but I'm satisfied with the namespace_literal shortcuts.

@aaronliu0130 aaronliu0130 merged commit 26af971 into cpplint:develop Jul 9, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Tuning Using-Directive Checks
2 participants