Skip to content

Commit cd26ae8

Browse files
cclaussaaronliu0130
authored andcommitted
Ruff rules for comprehensions and performance
1 parent 03d56ce commit cd26ae8

File tree

3 files changed

+44
-52
lines changed

3 files changed

+44
-52
lines changed

cpplint.py

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
import xml.etree.ElementTree
5858

5959
# if empty, use defaults
60-
_valid_extensions = set([])
60+
_valid_extensions = set()
6161

6262
__VERSION__ = '2.0.1'
6363

@@ -826,7 +826,7 @@
826826
]
827827

828828
# Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE
829-
_CHECK_REPLACEMENT = dict([(macro_var, {}) for macro_var in _CHECK_MACROS])
829+
_CHECK_REPLACEMENT = {macro_var: {} for macro_var in _CHECK_MACROS}
830830

831831
for op, replacement in [('==', 'EQ'), ('!=', 'NE'),
832832
('>=', 'GE'), ('>', 'GT'),
@@ -945,7 +945,7 @@
945945

946946
# Treat all headers starting with 'h' equally: .h, .hpp, .hxx etc.
947947
# This is set by --headers flag.
948-
_hpp_headers = set([])
948+
_hpp_headers = set()
949949

950950
class ErrorSuppressions:
951951
"""Class to track all error suppressions for cpplint"""
@@ -1038,13 +1038,12 @@ def GetHeaderExtensions():
10381038
return _hpp_headers
10391039
if _valid_extensions:
10401040
return {h for h in _valid_extensions if 'h' in h}
1041-
return set(['h', 'hh', 'hpp', 'hxx', 'h++', 'cuh'])
1041+
return {'h', 'hh', 'hpp', 'hxx', 'h++', 'cuh'}
10421042

10431043
# The allowed extensions for file names
10441044
# This is set by --extensions flag
10451045
def GetAllExtensions():
1046-
return GetHeaderExtensions().union(_valid_extensions or set(
1047-
['c', 'cc', 'cpp', 'cxx', 'c++', 'cu']))
1046+
return GetHeaderExtensions().union(_valid_extensions or {'c', 'cc', 'cpp', 'cxx', 'c++', 'cu'})
10481047

10491048
def ProcessExtensionsOption(val):
10501049
global _valid_extensions
@@ -1101,7 +1100,7 @@ def ProcessCategory(category):
11011100
if categories in (None, '(*)'): # => "suppress all"
11021101
ProcessCategory(None)
11031102
elif categories.startswith('(') and categories.endswith(')'):
1104-
for category in set(map(lambda c: c.strip(), categories[1:-1].split(','))):
1103+
for category in {c.strip() for c in categories[1:-1].split(',')}:
11051104
if category in _ERROR_CATEGORIES:
11061105
ProcessCategory(category)
11071106
elif any(c for c in _OTHER_NOLINT_CATEGORY_PREFIXES if category.startswith(c)):
@@ -6073,13 +6072,12 @@ def ExpectingFunctionArgs(clean_lines, linenum):
60736072

60746073
_re_pattern_headers_maybe_templates = []
60756074
for _header, _templates in _HEADERS_MAYBE_TEMPLATES:
6076-
for _template in _templates:
6077-
# Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
6078-
# 'type::max()'.
6079-
_re_pattern_headers_maybe_templates.append(
6080-
(re.compile(r'((\bstd::)|[^>.:])\b' + _template + r'(<.*?>)?\([^\)]'),
6081-
_template,
6082-
_header))
6075+
# Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
6076+
# 'type::max()'.
6077+
_re_pattern_headers_maybe_templates.extend(
6078+
(re.compile(r'((\bstd::)|[^>.:])\b' + _template + r'(<.*?>)?\([^\)]'),
6079+
_template,
6080+
_header) for _template in _templates)
60836081

60846082
# Map is often overloaded. Only check, if it is fully qualified.
60856083
# Match 'std::map<type>(...)', but not 'map<type>(...)''
@@ -6091,29 +6089,26 @@ def ExpectingFunctionArgs(clean_lines, linenum):
60916089
# Other scripts may reach in and modify this pattern.
60926090
_re_pattern_templates = []
60936091
for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
6094-
for _template in _templates:
6095-
_re_pattern_templates.append(
6096-
(re.compile(r'((^|(^|\s|((^|\W)::))std::)|[^>.:]\b)' + _template + r'\s*\<'),
6097-
_template + '<>',
6098-
_header))
6092+
_re_pattern_templates.extend(
6093+
(re.compile(r'((^|(^|\s|((^|\W)::))std::)|[^>.:]\b)' + _template + r'\s*\<'),
6094+
_template + '<>',
6095+
_header) for _template in _templates)
60996096

61006097
_re_pattern_types_or_objs = []
61016098
for _header, _types_or_objs in _HEADERS_TYPES_OR_OBJS:
6102-
for _type_or_obj in _types_or_objs:
6103-
_re_pattern_types_or_objs.append(
6104-
(re.compile(r'\b' + _type_or_obj + r'\b'),
6105-
_type_or_obj,
6106-
_header))
6099+
_re_pattern_types_or_objs.extend(
6100+
(re.compile(r'\b' + _type_or_obj + r'\b'),
6101+
_type_or_obj,
6102+
_header) for _type_or_obj in _types_or_objs)
61076103

61086104
_re_pattern_functions = []
61096105
for _header, _functions in _HEADERS_FUNCTIONS:
6110-
for _function in _functions:
6111-
# Match printf(..., ...), but not foo->printf, foo.printf or
6112-
# 'type::printf()'.
6113-
_re_pattern_functions.append(
6114-
(re.compile(r'([^>.]|^)\b' + _function + r'\([^\)]'),
6115-
_function,
6116-
_header))
6106+
# Match printf(..., ...), but not foo->printf, foo.printf or
6107+
# 'type::printf()'.
6108+
_re_pattern_functions.extend(
6109+
(re.compile(r'([^>.]|^)\b' + _function + r'\([^\)]'),
6110+
_function,
6111+
_header) for _function in _functions)
61176112

61186113
def FilesBelongToSameModule(filename_cc, filename_h):
61196114
"""Check if these two filenames belong to the same module.
@@ -6750,8 +6745,8 @@ def PrintUsage(message):
67506745
Args:
67516746
message: The optional error message.
67526747
"""
6753-
sys.stderr.write(_USAGE % (sorted(list(GetAllExtensions())),
6754-
','.join(sorted(list(GetAllExtensions()))),
6748+
sys.stderr.write(_USAGE % (sorted(GetAllExtensions()),
6749+
','.join(sorted(GetAllExtensions())),
67556750
sorted(GetHeaderExtensions()),
67566751
','.join(sorted(GetHeaderExtensions()))))
67576752

@@ -6933,11 +6928,8 @@ def _ExpandDirectories(filenames):
69336928
fullname = fullname[len('.' + os.path.sep):]
69346929
expanded.add(fullname)
69356930

6936-
filtered = []
6937-
for filename in expanded:
6938-
if os.path.splitext(filename)[1][1:] in GetAllExtensions():
6939-
filtered.append(filename)
6940-
return filtered
6931+
return [filename for filename in expanded
6932+
if os.path.splitext(filename)[1][1:] in GetAllExtensions()]
69416933

69426934
def _FilterExcludedFiles(fnames):
69436935
"""Filters out files listed in the --exclude command line switch. File paths

cpplint_unittest.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4260,27 +4260,27 @@ def testParseArguments(self):
42604260
self.assertEqual(['foo.h'],
42614261
cpplint.ParseArguments(['--linelength=120', 'foo.h']))
42624262
self.assertEqual(120, cpplint._line_length)
4263-
self.assertEqual(set(['h', 'hh', 'hpp', 'hxx', 'h++', 'cuh']), cpplint.GetHeaderExtensions()) # Default value
4263+
self.assertEqual({'h', 'hh', 'hpp', 'hxx', 'h++', 'cuh'}, cpplint.GetHeaderExtensions()) # Default value
42644264

42654265
cpplint._hpp_headers = old_headers
42664266
cpplint._valid_extensions = old_valid_extensions
42674267
self.assertEqual(['foo.h'],
42684268
cpplint.ParseArguments(['--headers=h', 'foo.h']))
4269-
self.assertEqual(set(['h', 'c', 'cc', 'cpp', 'cxx', 'c++', 'cu']), cpplint.GetAllExtensions())
4269+
self.assertEqual({'h', 'c', 'cc', 'cpp', 'cxx', 'c++', 'cu'}, cpplint.GetAllExtensions())
42704270

42714271
cpplint._hpp_headers = old_headers
42724272
cpplint._valid_extensions = old_valid_extensions
42734273
self.assertEqual(['foo.h'],
42744274
cpplint.ParseArguments(['--extensions=hpp,cpp,cpp', 'foo.h']))
4275-
self.assertEqual(set(['hpp', 'cpp']), cpplint.GetAllExtensions())
4276-
self.assertEqual(set(['hpp']), cpplint.GetHeaderExtensions())
4275+
self.assertEqual({'hpp', 'cpp'}, cpplint.GetAllExtensions())
4276+
self.assertEqual({'hpp'}, cpplint.GetHeaderExtensions())
42774277

42784278
cpplint._hpp_headers = old_headers
42794279
cpplint._valid_extensions = old_valid_extensions
42804280
self.assertEqual(['foo.h'],
42814281
cpplint.ParseArguments(['--extensions=cpp,cpp', '--headers=hpp,h', 'foo.h']))
4282-
self.assertEqual(set(['hpp', 'h']), cpplint.GetHeaderExtensions())
4283-
self.assertEqual(set(['hpp', 'h', 'cpp']), cpplint.GetAllExtensions())
4282+
self.assertEqual({'hpp', 'h'}, cpplint.GetHeaderExtensions())
4283+
self.assertEqual({'hpp', 'h', 'cpp'}, cpplint.GetAllExtensions())
42844284

42854285
finally:
42864286
sys.stdout = sys.__stdout__
@@ -4330,8 +4330,8 @@ def testRecursiveExcludeInvalidFileExtension(self):
43304330
finally:
43314331
os.chdir(working_dir)
43324332
shutil.rmtree(temp_dir)
4333-
cpplint._hpp_headers = set([])
4334-
cpplint._valid_extensions = set([])
4333+
cpplint._hpp_headers = set()
4334+
cpplint._valid_extensions = set()
43354335

43364336
def testRecursiveExclude(self):
43374337
working_dir = os.getcwd()
@@ -4356,7 +4356,7 @@ def testRecursiveExclude(self):
43564356
]
43574357
cpplint._excludes = None
43584358
actual = cpplint.ParseArguments(['src'])
4359-
self.assertEqual(set(['src']), set(actual))
4359+
self.assertEqual({'src'}, set(actual))
43604360

43614361
cpplint._excludes = None
43624362
actual = cpplint.ParseArguments(['--recursive', 'src'])
@@ -4374,11 +4374,11 @@ def testRecursiveExclude(self):
43744374
'--exclude=src/two.cc', '--exclude=src/three.cc', 'src'])
43754375
self.assertEqual(set(expected), set(actual))
43764376

4377-
expected = set([
4377+
expected = {
43784378
os.path.join('src2', 'one.cc'),
43794379
os.path.join('src2', 'two.cc'),
43804380
os.path.join('src2', 'three.cc')
4381-
])
4381+
}
43824382
cpplint._excludes = None
43834383
actual = cpplint.ParseArguments(['--recursive',
43844384
'--exclude=src', '.'])
@@ -5629,8 +5629,8 @@ def testClassifyInclude(self):
56295629
False))
56305630

56315631
def testTryDropCommonSuffixes(self):
5632-
cpplint._hpp_headers = set([])
5633-
cpplint._valid_extensions = set([])
5632+
cpplint._hpp_headers = set()
5633+
cpplint._valid_extensions = set()
56345634
self.assertEqual('foo/foo', cpplint._DropCommonSuffixes('foo/foo-inl.h'))
56355635
self.assertEqual('foo/foo', cpplint._DropCommonSuffixes('foo/foo-inl.hxx'))
56365636
self.assertEqual('foo/foo', cpplint._DropCommonSuffixes('foo/foo-inl.h++'))

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ lint.select = [
7474
"ASYNC", # flake8-async
7575
"B", # flake8-bugbear
7676
"BLE", # flake8-blind-except
77+
"C4", # flake8-comprehensions
7778
"C90", # McCabe cyclomatic complexity
7879
"DJ", # flake8-django
7980
"DTZ", # flake8-datetimez
@@ -113,7 +114,6 @@ lint.select = [
113114
"YTT", # flake8-2020
114115
# "ANN", # flake8-annotations
115116
# "ARG", # flake8-unused-arguments
116-
# "C4", # flake8-comprehensions
117117
# "COM", # flake8-commas
118118
# "CPY", # flake8-copyright
119119
# "D", # pydocstyle

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy