@@ -1407,9 +1407,9 @@ def AddFilters(self, filters):
1407
1407
if clean_filt :
1408
1408
self .filters .append (clean_filt )
1409
1409
for filt in self .filters :
1410
- if not ( filt .startswith ('+' ) or filt . startswith ( '-' )):
1411
- raise ValueError ( 'Every filter in --filters must start with + or -'
1412
- f' ( { filt } does not)' )
1410
+ if not filt .startswith (( '+' , '-' )):
1411
+ msg = f 'Every filter in --filters must start with + or - ( { filt } does not) '
1412
+ raise ValueError ( msg )
1413
1413
1414
1414
def BackupFilters (self ):
1415
1415
""" Saves the current filter list to backup storage."""
@@ -1618,7 +1618,7 @@ def Check(self, error, filename, linenum):
1618
1618
trigger = base_trigger * 2 ** _VerboseLevel ()
1619
1619
1620
1620
if self .lines_in_function > trigger :
1621
- error_level = int (math .log (self .lines_in_function / base_trigger , 2 ))
1621
+ error_level = int (math .log2 (self .lines_in_function / base_trigger ))
1622
1622
# 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ...
1623
1623
if error_level > 5 :
1624
1624
error_level = 5
@@ -1769,7 +1769,9 @@ def _ShouldPrintError(category, confidence, filename, linenum):
1769
1769
if category_match and file_match and line_match :
1770
1770
is_filtered = False
1771
1771
else :
1772
- assert False # should have been checked for in SetFilter.
1772
+ # should have been checked for in SetFilter.
1773
+ msg = f'Invalid filter: { one_filter } '
1774
+ raise ValueError (msg )
1773
1775
if is_filtered :
1774
1776
return False
1775
1777
@@ -2357,7 +2359,8 @@ def CheckForCopyright(filename, lines, error):
2357
2359
# We'll say it should occur by line 10. Don't forget there's a
2358
2360
# placeholder line at the front.
2359
2361
for line in range (1 , min (len (lines ), 11 )):
2360
- if re .search (r'Copyright' , lines [line ], re .I ): break
2362
+ if re .search (r'Copyright' , lines [line ], re .IGNORECASE ):
2363
+ break
2361
2364
else : # means no copyright line was found
2362
2365
error (filename , 0 , 'legal/copyright' , 5 ,
2363
2366
'No copyright message found. '
@@ -2376,8 +2379,7 @@ def GetIndentLevel(line):
2376
2379
indent = re .match (r'^( *)\S' , line )
2377
2380
if indent :
2378
2381
return len (indent .group (1 ))
2379
- else :
2380
- return 0
2382
+ return 0
2381
2383
2382
2384
def PathSplitToList (path ):
2383
2385
"""Returns the path split into a list by the separator.
@@ -3108,14 +3110,16 @@ def InTemplateArgumentList(self, clean_lines, linenum, pos):
3108
3110
# These things do not look like template argument list:
3109
3111
# class Suspect {
3110
3112
# class Suspect x; }
3111
- if token in ('{' , '}' , ';' ): return False
3113
+ if token in ('{' , '}' , ';' ):
3114
+ return False
3112
3115
3113
3116
# These things look like template argument list:
3114
3117
# template <class Suspect>
3115
3118
# template <class Suspect = default_value>
3116
3119
# template <class Suspect[]>
3117
3120
# template <class Suspect...>
3118
- if token in ('>' , '=' , '[' , ']' , '.' ): return True
3121
+ if token in ('>' , '=' , '[' , ']' , '.' ):
3122
+ return True
3119
3123
3120
3124
# Check if token is an unmatched '<'.
3121
3125
# If not, move on to the next character.
@@ -4912,8 +4916,7 @@ def GetLineWidth(line):
4912
4916
4913
4917
width += 1
4914
4918
return width
4915
- else :
4916
- return len (line )
4919
+ return len (line )
4917
4920
4918
4921
4919
4922
def CheckStyle (filename , clean_lines , linenum , file_extension , nesting_state ,
@@ -4985,9 +4988,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
4985
4988
# Check if the line is a header guard.
4986
4989
is_header_guard = False
4987
4990
if IsHeaderExtension (file_extension ):
4988
- if (line .startswith (f'#ifndef { cppvar } ' ) or
4989
- line .startswith (f'#define { cppvar } ' ) or
4990
- line .startswith (f'#endif // { cppvar } ' )):
4991
+ if line .startswith ((f'#ifndef { cppvar } ' , f'#define { cppvar } ' , f'#endif // { cppvar } ' )):
4991
4992
is_header_guard = True
4992
4993
# #include lines and header guards can be long, since there's no clean way to
4993
4994
# split them.
@@ -5126,8 +5127,7 @@ def _ClassifyInclude(fileinfo, include, used_angle_brackets, include_order="defa
5126
5127
return _CPP_SYS_HEADER
5127
5128
if is_std_c_header :
5128
5129
return _C_SYS_HEADER
5129
- else :
5130
- return _OTHER_SYS_HEADER
5130
+ return _OTHER_SYS_HEADER
5131
5131
5132
5132
# If the target file and the include we're checking share a
5133
5133
# basename when we drop common extensions, and the include
@@ -5277,7 +5277,7 @@ def _GetTextInside(text, start_pattern):
5277
5277
closing_punctuation = set (dict .values (matching_punctuation ))
5278
5278
5279
5279
# Find the position to start extracting text.
5280
- match = re .search (start_pattern , text , re .M )
5280
+ match = re .search (start_pattern , text , re .MULTILINE )
5281
5281
if not match : # start_pattern not found in text.
5282
5282
return None
5283
5283
start_position = match .end (0 )
@@ -5422,7 +5422,7 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
5422
5422
match = re .match (r'([\w.\->()]+)$' , printf_args )
5423
5423
if match and match .group (1 ) != '__VA_ARGS__' :
5424
5424
function_name = re .search (r'\b((?:string)?printf)\s*\(' ,
5425
- line , re .I ).group (1 )
5425
+ line , re .IGNORECASE ).group (1 )
5426
5426
error (filename , linenum , 'runtime/printf' , 4 ,
5427
5427
'Potential format string bug. Do'
5428
5428
f' { function_name } ("%s", { match .group (1 )} ) instead.' )
@@ -5458,17 +5458,25 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
5458
5458
skip_next = False
5459
5459
continue
5460
5460
5461
- if re .search (r'sizeof\(.+\)' , tok ): continue
5462
- if re .search (r'arraysize\(\w+\)' , tok ): continue
5461
+ if re .search (r'sizeof\(.+\)' , tok ):
5462
+ continue
5463
+ if re .search (r'arraysize\(\w+\)' , tok ):
5464
+ continue
5463
5465
5464
5466
tok = tok .lstrip ('(' )
5465
5467
tok = tok .rstrip (')' )
5466
- if not tok : continue
5467
- if re .match (r'\d+' , tok ): continue
5468
- if re .match (r'0[xX][0-9a-fA-F]+' , tok ): continue
5469
- if re .match (r'k[A-Z0-9]\w*' , tok ): continue
5470
- if re .match (r'(.+::)?k[A-Z0-9]\w*' , tok ): continue
5471
- if re .match (r'(.+::)?[A-Z][A-Z0-9_]*' , tok ): continue
5468
+ if not tok :
5469
+ continue
5470
+ if re .match (r'\d+' , tok ):
5471
+ continue
5472
+ if re .match (r'0[xX][0-9a-fA-F]+' , tok ):
5473
+ continue
5474
+ if re .match (r'k[A-Z0-9]\w*' , tok ):
5475
+ continue
5476
+ if re .match (r'(.+::)?k[A-Z0-9]\w*' , tok ):
5477
+ continue
5478
+ if re .match (r'(.+::)?[A-Z][A-Z0-9_]*' , tok ):
5479
+ continue
5472
5480
# A catch all for tricky sizeof cases, including 'sizeof expression',
5473
5481
# 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)'
5474
5482
# requires skipping the next token because we split on ' ' and '*'.
@@ -5783,7 +5791,7 @@ def CheckForNonConstReference(filename, clean_lines, linenum,
5783
5791
r')\s*\(' )
5784
5792
if re .search (allowed_functions , line ):
5785
5793
return
5786
- elif not re .search (r'\S+\([^)]*$' , line ):
5794
+ if not re .search (r'\S+\([^)]*$' , line ):
5787
5795
# Don't see an allowed function on this line. Actually we
5788
5796
# didn't see any function name on this line, so this is likely a
5789
5797
# multi-line parameter list. Try a bit harder to catch this case.
@@ -5955,8 +5963,7 @@ def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
5955
5963
return False
5956
5964
5957
5965
# operator++(int) and operator--(int)
5958
- if (context .endswith (' operator++' ) or context .endswith (' operator--' ) or
5959
- context .endswith ('::operator++' ) or context .endswith ('::operator--' )):
5966
+ if context .endswith ((' operator++' , ' operator--' , '::operator++' , '::operator--' )):
5960
5967
return False
5961
5968
5962
5969
# A single unnamed argument for a function tends to look like old style cast.
@@ -6273,7 +6280,8 @@ def CheckRedundantVirtual(filename, clean_lines, linenum, error):
6273
6280
# Look for "virtual" on current line.
6274
6281
line = clean_lines .elided [linenum ]
6275
6282
virtual = re .match (r'^(.*)(\bvirtual\b)(.*)$' , line )
6276
- if not virtual : return
6283
+ if not virtual :
6284
+ return
6277
6285
6278
6286
# Ignore "virtual" keywords that are near access-specifiers. These
6279
6287
# are only used in class base-specifier and do not apply to member
@@ -6285,7 +6293,8 @@ def CheckRedundantVirtual(filename, clean_lines, linenum, error):
6285
6293
# Ignore the "virtual" keyword from virtual base classes. Usually
6286
6294
# there is a column on the same line in these cases (virtual base
6287
6295
# classes are rare in google3 because multiple inheritance is rare).
6288
- if re .match (r'^.*[^:]:[^:].*$' , line ): return
6296
+ if re .match (r'^.*[^:]:[^:].*$' , line ):
6297
+ return
6289
6298
6290
6299
# Look for the next opening parenthesis. This is the start of the
6291
6300
# parameter list (possibly on the next line shortly after virtual).
@@ -6374,7 +6383,7 @@ def IsBlockInNameSpace(nesting_state, is_forward_declaration):
6374
6383
if len (nesting_state .stack ) >= 1 :
6375
6384
if isinstance (nesting_state .stack [- 1 ], _NamespaceInfo ):
6376
6385
return True
6377
- elif (len (nesting_state .stack ) > 1 and
6386
+ if (len (nesting_state .stack ) > 1 and
6378
6387
isinstance (nesting_state .previous_stack_top , _NamespaceInfo ) and
6379
6388
isinstance (nesting_state .stack [- 2 ], _NamespaceInfo )):
6380
6389
return True
@@ -6453,7 +6462,8 @@ def ProcessLine(filename, file_extension, clean_lines, line,
6453
6462
nesting_state .Update (filename , clean_lines , line , error )
6454
6463
CheckForNamespaceIndentation (filename , nesting_state , clean_lines , line ,
6455
6464
error )
6456
- if nesting_state .InAsmBlock (): return
6465
+ if nesting_state .InAsmBlock ():
6466
+ return
6457
6467
CheckForFunctionLengths (filename , clean_lines , line , function_state , error )
6458
6468
CheckForMultilineCommentsAndStrings (filename , clean_lines , line , error )
6459
6469
CheckStyle (filename , clean_lines , line , file_extension , nesting_state , error , cppvar )
@@ -6895,9 +6905,8 @@ def _ParseFilterSelector(parameter):
6895
6905
second_colon_pos = parameter .find (":" , colon_pos + 1 )
6896
6906
if second_colon_pos == - 1 :
6897
6907
return category , parameter [colon_pos + 1 :], - 1
6898
- else :
6899
- return category , parameter [colon_pos + 1 : second_colon_pos ], \
6900
- int (parameter [second_colon_pos + 1 :])
6908
+ return category , parameter [colon_pos + 1 : second_colon_pos ], \
6909
+ int (parameter [second_colon_pos + 1 :])
6901
6910
6902
6911
def _ExpandDirectories (filenames ):
6903
6912
"""Searches a list of filenames and replaces directories in the list with
0 commit comments