]> git.lizzy.rs Git - rust.git/blobdiff - util/update_lints.py
Merge #3353
[rust.git] / util / update_lints.py
index 0f9c34794396e26aac06f2deb4bc0ee5bb55ce9e..221069d353cb3093063b1b0eefc60b46435475e4 100755 (executable)
@@ -1,4 +1,16 @@
 #!/usr/bin/env python
+
+# Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
+# file at the top-level directory of this distribution and at
+# http://rust-lang.org/COPYRIGHT.
+#
+# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+# option. This file may not be copied, modified, or distributed
+# except according to those terms.
+
+
 # Generate a Markdown table of all lints, and put it in README.md.
 # With -n option, only print the new table to stdout.
 # With -c option, print a warning and set exit status to 1 if a file would be
@@ -7,13 +19,7 @@
 import os
 import re
 import sys
-
-declare_lint_re = re.compile(r'''
-    declare_lint! \s* [{(] \s*
-    pub \s+ (?P<name>[A-Z_][A-Z_0-9]*) \s*,\s*
-    (?P<level>Forbid|Deny|Warn|Allow) \s*,\s*
-    " (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
-''', re.VERBOSE | re.DOTALL)
+from subprocess import call
 
 declare_deprecated_lint_re = re.compile(r'''
     declare_deprecated_lint! \s* [{(] \s*
@@ -21,12 +27,6 @@ declare_deprecated_lint_re = re.compile(r'''
     " (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
 ''', re.VERBOSE | re.DOTALL)
 
-declare_restriction_lint_re = re.compile(r'''
-    declare_restriction_lint! \s* [{(] \s*
-    pub \s+ (?P<name>[A-Z_][A-Z_0-9]*) \s*,\s*
-    " (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
-''', re.VERBOSE | re.DOTALL)
-
 declare_clippy_lint_re = re.compile(r'''
     declare_clippy_lint! \s* [{(] \s*
     pub \s+ (?P<name>[A-Z_][A-Z_0-9]*) \s*,\s*
@@ -39,20 +39,13 @@ nl_escape_re = re.compile(r'\\\n\s*')
 docs_link = 'https://rust-lang-nursery.github.io/rust-clippy/master/index.html'
 
 
-def collect(lints, deprecated_lints, restriction_lints, clippy_lints, fn):
+def collect(deprecated_lints, clippy_lints, fn):
     """Collect all lints from a file.
 
     Adds entries to the lints list as `(module, name, level, desc)`.
     """
     with open(fn) as fp:
         code = fp.read()
-    for match in declare_lint_re.finditer(code):
-        # remove \-newline escapes from description string
-        desc = nl_escape_re.sub('', match.group('desc'))
-        lints.append((os.path.splitext(os.path.basename(fn))[0],
-                      match.group('name').lower(),
-                      match.group('level').lower(),
-                      desc.replace('\\"', '"')))
 
     for match in declare_deprecated_lint_re.finditer(code):
         # remove \-newline escapes from description string
@@ -61,28 +54,23 @@ def collect(lints, deprecated_lints, restriction_lints, clippy_lints, fn):
                                 match.group('name').lower(),
                                 desc.replace('\\"', '"')))
 
-    for match in declare_restriction_lint_re.finditer(code):
-        # remove \-newline escapes from description string
-        desc = nl_escape_re.sub('', match.group('desc'))
-        restriction_lints.append((os.path.splitext(os.path.basename(fn))[0],
-                                  match.group('name').lower(),
-                                  "allow",
-                                  desc.replace('\\"', '"')))
-    
     for match in declare_clippy_lint_re.finditer(code):
         # remove \-newline escapes from description string
         desc = nl_escape_re.sub('', match.group('desc'))
         cat = match.group('cat')
-        clippy_lints[cat].append((os.path.splitext(os.path.basename(fn))[0],
+        if cat in ('internal', 'internal_warn'):
+            continue
+        module_name = os.path.splitext(os.path.basename(fn))[0]
+        if module_name == 'mod':
+            module_name = os.path.basename(os.path.dirname(fn))
+        clippy_lints[cat].append((module_name,
                                   match.group('name').lower(),
                                   "allow",
                                   desc.replace('\\"', '"')))
 
 
-def gen_group(lints, levels=None):
+def gen_group(lints):
     """Write lint group (list of all lints in the form module::NAME)."""
-    if levels:
-        lints = [tup for tup in lints if tup[2] in levels]
     for (module, name, _, _) in sorted(lints):
         yield '        %s::%s,\n' % (module, name.upper())
 
@@ -116,6 +104,8 @@ def replace_region(fn, region_start, region_end, callback,
     with open(fn) as fp:
         lines = list(fp)
 
+    found = False
+
     # replace old region with new region
     new_lines = []
     in_old_region = False
@@ -130,9 +120,13 @@ def replace_region(fn, region_start, region_end, callback,
                 new_lines.append(line)
             # old region starts here
             in_old_region = True
+            found = True
         else:
             new_lines.append(line)
 
+    if not found:
+        print("regex " + region_start + " not found")
+
     # write back to file
     if write_back:
         with open(fn, 'w') as fp:
@@ -143,14 +137,16 @@ def replace_region(fn, region_start, region_end, callback,
 
 
 def main(print_only=False, check=False):
-    lints = []
     deprecated_lints = []
-    restriction_lints = []
     clippy_lints = {
         "correctness": [],
         "style": [],
         "complexity": [],
         "perf": [],
+        "restriction": [],
+        "pedantic": [],
+        "cargo": [],
+        "nursery": [],
     }
 
     # check directory
@@ -159,10 +155,11 @@ def main(print_only=False, check=False):
         return
 
     # collect all lints from source files
-    for fn in os.listdir('clippy_lints/src'):
-        if fn.endswith('.rs'):
-            collect(lints, deprecated_lints, restriction_lints, clippy_lints,
-                    os.path.join('clippy_lints', 'src', fn))
+    for root, dirs, files in os.walk('clippy_lints/src'):
+        for fn in files:
+            if fn.endswith('.rs'):
+                collect(deprecated_lints, clippy_lints,
+                        os.path.join(root, fn))
 
     # determine version
     with open('Cargo.toml') as fp:
@@ -174,25 +171,36 @@ def main(print_only=False, check=False):
             print('Error: version not found in Cargo.toml!')
             return
 
-    all_lints = lints + restriction_lints + clippy_lints['perf'] + clippy_lints['correctness'] + clippy_lints['style'] + clippy_lints['complexity']
+    all_lints = []
+    clippy_lint_groups = [
+        "correctness",
+        "style",
+        "complexity",
+        "perf",
+    ]
+    clippy_lint_list = []
+    for x in clippy_lint_groups:
+        clippy_lint_list += clippy_lints[x]
+    for _, value in clippy_lints.iteritems():
+        all_lints += value
 
     if print_only:
-        sys.stdout.writelines(gen_table(all_lints))
+        call(["./util/dev", "update_lints", "--print-only"])
         return
 
     # update the lint counter in README.md
     changed = replace_region(
         'README.md',
-        r'^\[There are \d+ lints included in this crate\]\(https://rust-lang-nursery.github.io/rust-clippy/master/index.html\)$', "",
-        lambda: ['[There are %d lints included in this crate](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)\n' %
+        r'^\[There are \d+ lints included in this crate!\]\(https://rust-lang-nursery.github.io/rust-clippy/master/index.html\)$', "",
+        lambda: ['[There are %d lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)\n' %
                  (len(all_lints))],
         write_back=not check)
 
     # update the links in the CHANGELOG
     changed |= replace_region(
         'CHANGELOG.md',
-        "<!-- begin autogenerated links to wiki -->",
-        "<!-- end autogenerated links to wiki -->",
+        "<!-- begin autogenerated links to lint list -->",
+        "<!-- end autogenerated links to lint list -->",
         lambda: ["[`{0}`]: {1}#{0}\n".format(l[1], docs_link) for l in
                  sorted(all_lints + deprecated_lints,
                         key=lambda l: l[1])],
@@ -217,55 +225,26 @@ def main(print_only=False, check=False):
         lambda: gen_mods(all_lints),
         replace_start=False, write_back=not check)
 
-    # same for "clippy" lint collection
-    changed |= replace_region(
-        'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy"', r'\]\);',
-        lambda: gen_group(lints, levels=('warn', 'deny')),
-        replace_start=False, write_back=not check)
-
-    # same for "clippy_style" lint collection
-    changed |= replace_region(
-        'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy_style"', r'\]\);',
-        lambda: gen_group(clippy_lints['style']),
-        replace_start=False, write_back=not check)
-
-    # same for "clippy_correctness" lint collection
-    changed |= replace_region(
-        'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy_correctness"', r'\]\);',
-        lambda: gen_group(clippy_lints['correctness']),
-        replace_start=False, write_back=not check)
-
-    # same for "clippy_complexity" lint collection
+    # same for "clippy::*" lint collections
     changed |= replace_region(
-        'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy_complexity"', r'\]\);',
-        lambda: gen_group(clippy_lints['complexity']),
+        'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy::all"', r'\]\);',
+        lambda: gen_group(clippy_lint_list),
         replace_start=False, write_back=not check)
 
-    # same for "clippy_perf" lint collection
-    changed |= replace_region(
-        'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy_perf"', r'\]\);',
-        lambda: gen_group(clippy_lints['perf']),
-        replace_start=False, write_back=not check)
+    for key, value in clippy_lints.iteritems():
+        # same for "clippy::*" lint collections
+        changed |= replace_region(
+            'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy::' + key + r'"', r'\]\);',
+            lambda: gen_group(value),
+            replace_start=False, write_back=not check)
 
     # same for "deprecated" lint collection
     changed |= replace_region(
-        'clippy_lints/src/lib.rs', r'let mut store', r'end deprecated lints',
+        'clippy_lints/src/lib.rs', r'begin deprecated lints', r'end deprecated lints',
         lambda: gen_deprecated(deprecated_lints),
         replace_start=False,
         write_back=not check)
 
-    # same for "clippy_pedantic" lint collection
-    changed |= replace_region(
-        'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy_pedantic"', r'\]\);',
-        lambda: gen_group(lints, levels=('allow',)),
-        replace_start=False, write_back=not check)
-
-    # same for "clippy_restrictions" lint collection
-    changed |= replace_region(
-        'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy_restrictions"',
-        r'\]\);', lambda: gen_group(restriction_lints),
-        replace_start=False, write_back=not check)
-
     if check and changed:
         print('Please run util/update_lints.py to regenerate lints lists.')
         return 1