Skip to content
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

cpplint: allow using-directive for user-defined literals namespaces #204

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cpplint/cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4708,7 +4708,15 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
'Did you mean "memset(%s, 0, %s)"?'
% (match.group(1), match.group(2)))

if Search(r'\busing namespace\b', line):
# Check for 'using namespace' which pollutes namespaces.
# An exception is made for namespaces with the word 'literals',
# such namespaces are likely to only contain user-defined literals
# which by design are supposed to be imported by using-directives,
# e.g. 'using namespace std::chrono_literals;'.
# Headers do not take part of this 'literals' exception.
match = Search(r'\busing namespace\s+((\w|::)+)', line)
if (match and
(IsHeaderExtension(file_extension) or 'literals' not in match.group(1))):
error(filename, linenum, 'build/namespaces', 5,
'Do not use namespace using-directives. '
'Use using-declarations instead.')
Expand Down
16 changes: 10 additions & 6 deletions cpplint/cpplint_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3023,19 +3023,23 @@ def testSpaceAfterCommentMarker(self):
# precise, the '// marker so line numbers and indices both start at
# 1' line was also causing the issue.
def testLinePrecededByEmptyOrCommentLines(self):
def DoTest(self, lines):
def DoTest(self, filetype, lines, error_count):
error_collector = ErrorCollector(self.assert_)
cpplint.ProcessFileData('foo.cc', 'cc', lines, error_collector)
cpplint.ProcessFileData('foo.' + filetype, filetype, lines,
error_collector)
# The warning appears only once.
self.assertEquals(
1,
error_count,
error_collector.Results().count(
'Do not use namespace using-directives. '
'Use using-declarations instead.'
' [build/namespaces] [5]'))
DoTest(self, ['using namespace foo;'])
DoTest(self, ['', '', '', 'using namespace foo;'])
DoTest(self, ['// hello', 'using namespace foo;'])
DoTest(self, 'cc', ['using namespace foo;'], 1)
DoTest(self, 'h', ['using namespace foo;'], 1)
DoTest(self, 'cc', ['', '', '', 'using namespace foo;'], 1)
DoTest(self, 'cc', ['// hello', 'using namespace foo;'], 1)
DoTest(self, 'cc', ['using namespace std::chrono_literals;'], 0)
DoTest(self, 'h', ['using namespace std::chrono_literals;'], 1)

def testNewlineAtEOF(self):
def DoTest(self, data, is_missing_eof):
Expand Down