contrib/check-code.py
changeset 29278 5eda83fb09fc
parent 29277 79a11506803f
child 29279 438caf194160
equal deleted inserted replaced
29277:79a11506803f 29278:5eda83fb09fc
    49         except re2.error:
    49         except re2.error:
    50             pass
    50             pass
    51     return re.compile(pat)
    51     return re.compile(pat)
    52 
    52 
    53 def repquote(m):
    53 def repquote(m):
       
    54     # check "rules depending on implementation of repquote()" in each
       
    55     # patterns (especially pypats), before changing this function
    54     fixedmap = {' ': ' ', '\n': '\n', '.': 'p', ':': 'q'}
    56     fixedmap = {' ': ' ', '\n': '\n', '.': 'p', ':': 'q'}
    55     def encodechr(i):
    57     def encodechr(i):
    56         if i > 255:
    58         if i > 255:
    57             return 'u'
    59             return 'u'
    58         c = chr(i)
    60         c = chr(i)
   242     (r'(\w|\)),\w', "missing whitespace after ,"),
   244     (r'(\w|\)),\w', "missing whitespace after ,"),
   243     (r'(\w|\))[+/*\-<>]\w', "missing whitespace in expression"),
   245     (r'(\w|\))[+/*\-<>]\w', "missing whitespace in expression"),
   244     (r'^\s+(\w|\.)+=\w[^,()\n]*$', "missing whitespace in assignment"),
   246     (r'^\s+(\w|\.)+=\w[^,()\n]*$', "missing whitespace in assignment"),
   245     (r'\w\s=\s\s+\w', "gratuitous whitespace after ="),
   247     (r'\w\s=\s\s+\w', "gratuitous whitespace after ="),
   246     (r'.{81}', "line too long"),
   248     (r'.{81}', "line too long"),
   247     (r' x+[xpqo][\'"]\n\s+[\'"]x', 'string join across lines with no space'),
       
   248     (r'[^\n]\Z', "no trailing newline"),
   249     (r'[^\n]\Z', "no trailing newline"),
   249     (r'(\S[ \t]+|^[ \t]+)\n', "trailing whitespace"),
   250     (r'(\S[ \t]+|^[ \t]+)\n', "trailing whitespace"),
   250 #    (r'^\s+[^_ \n][^_. \n]+_[^_\n]+\s*=',
   251 #    (r'^\s+[^_ \n][^_. \n]+_[^_\n]+\s*=',
   251 #     "don't use underbars in identifiers"),
   252 #     "don't use underbars in identifiers"),
   252     (r'^\s+(self\.)?[A-za-z][a-z0-9]+[A-Z]\w* = ',
   253     (r'^\s+(self\.)?[A-za-z][a-z0-9]+[A-Z]\w* = ',
   309     (r'\.strip\(\)\.split\(\)', "no need to strip before splitting"),
   310     (r'\.strip\(\)\.split\(\)', "no need to strip before splitting"),
   310     (r'^\s*except\s*:', "naked except clause", r'#.*re-raises'),
   311     (r'^\s*except\s*:', "naked except clause", r'#.*re-raises'),
   311     (r'^\s*except\s([^\(,]+|\([^\)]+\))\s*,',
   312     (r'^\s*except\s([^\(,]+|\([^\)]+\))\s*,',
   312      'legacy exception syntax; use "as" instead of ","'),
   313      'legacy exception syntax; use "as" instead of ","'),
   313     (r':\n(    )*( ){1,3}[^ ]', "must indent 4 spaces"),
   314     (r':\n(    )*( ){1,3}[^ ]', "must indent 4 spaces"),
   314     (r'ui\.(status|progress|write|note|warn)\([\'\"]x',
       
   315      "missing _() in ui message (use () to hide false-positives)"),
       
   316     (r'release\(.*wlock, .*lock\)', "wrong lock release order"),
   315     (r'release\(.*wlock, .*lock\)', "wrong lock release order"),
   317     (r'\b__bool__\b', "__bool__ should be __nonzero__ in Python 2"),
   316     (r'\b__bool__\b', "__bool__ should be __nonzero__ in Python 2"),
   318     (r'os\.path\.join\(.*, *(""|\'\')\)',
   317     (r'os\.path\.join\(.*, *(""|\'\')\)',
   319      "use pathutil.normasprefix(path) instead of os.path.join(path, '')"),
   318      "use pathutil.normasprefix(path) instead of os.path.join(path, '')"),
   320     (r'\s0[0-7]+\b', 'legacy octal syntax; use "0o" prefix instead of "0"'),
   319     (r'\s0[0-7]+\b', 'legacy octal syntax; use "0o" prefix instead of "0"'),
   323     (r'\butil\.Abort\b', "directly use error.Abort"),
   322     (r'\butil\.Abort\b', "directly use error.Abort"),
   324     (r'^import Queue', "don't use Queue, use util.queue + util.empty"),
   323     (r'^import Queue', "don't use Queue, use util.queue + util.empty"),
   325     (r'^import cStringIO', "don't use cStringIO.StringIO, use util.stringio"),
   324     (r'^import cStringIO', "don't use cStringIO.StringIO, use util.stringio"),
   326     (r'^import urllib', "don't use urllib, use util.urlreq/util.urlerr"),
   325     (r'^import urllib', "don't use urllib, use util.urlreq/util.urlerr"),
   327     (r'\.next\(\)', "don't use .next(), use next(...)"),
   326     (r'\.next\(\)', "don't use .next(), use next(...)"),
       
   327 
       
   328     # rules depending on implementation of repquote()
       
   329     (r' x+[xpqo][\'"]\n\s+[\'"]x', 'string join across lines with no space'),
       
   330     (r'ui\.(status|progress|write|note|warn)\([\'\"]x',
       
   331      "missing _() in ui message (use () to hide false-positives)"),
   328   ],
   332   ],
   329   # warnings
   333   # warnings
   330   [
   334   [
       
   335     # rules depending on implementation of repquote()
   331     (r'(^| )pp +xxxxqq[ \n][^\n]', "add two newlines after '.. note::'"),
   336     (r'(^| )pp +xxxxqq[ \n][^\n]', "add two newlines after '.. note::'"),
   332   ]
   337   ]
   333 ]
   338 ]
   334 
   339 
   335 pyfilters = [
   340 pyfilters = [
   370     (r'^#\s+\w', "use #foo, not # foo"),
   375     (r'^#\s+\w', "use #foo, not # foo"),
   371     (r'[^\n]\Z', "no trailing newline"),
   376     (r'[^\n]\Z', "no trailing newline"),
   372     (r'^\s*#import\b', "use only #include in standard C code"),
   377     (r'^\s*#import\b', "use only #include in standard C code"),
   373     (r'strcpy\(', "don't use strcpy, use strlcpy or memcpy"),
   378     (r'strcpy\(', "don't use strcpy, use strlcpy or memcpy"),
   374     (r'strcat\(', "don't use strcat"),
   379     (r'strcat\(', "don't use strcat"),
       
   380 
       
   381     # rules depending on implementation of repquote()
   375   ],
   382   ],
   376   # warnings
   383   # warnings
   377   []
   384   [
       
   385     # rules depending on implementation of repquote()
       
   386   ]
   378 ]
   387 ]
   379 
   388 
   380 cfilters = [
   389 cfilters = [
   381     (r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment),
   390     (r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment),
   382     (r'''(?P<quote>(?<!")")(?P<text>([^"]|\\")+)"(?!")''', repquote),
   391     (r'''(?P<quote>(?<!")")(?P<text>([^"]|\\")+)"(?!")''', repquote),