]> git.lizzy.rs Git - rust.git/blobdiff - util/update_lints.py
Fix deploy.sh III
[rust.git] / util / update_lints.py
index bf9728d4f58cc56be07ea06e95e09ba5a0825ce7..e81f71617ffa2b870cd06dbccaeca04fdc3dfc97 100755 (executable)
@@ -21,12 +21,18 @@ 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)
+
 nl_escape_re = re.compile(r'\\\n\s*')
 
 wiki_link = 'https://github.com/Manishearth/rust-clippy/wiki'
 
 
-def collect(lints, deprecated_lints, fn):
+def collect(lints, deprecated_lints, restriction_lints, fn):
     """Collect all lints from a file.
 
     Adds entries to the lints list as `(module, name, level, desc)`.
@@ -48,6 +54,14 @@ def collect(lints, deprecated_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('\\"', '"')))
+
 
 def gen_table(lints, link=None):
     """Write lint table in Markdown format."""
@@ -128,32 +142,44 @@ def replace_region(fn, region_start, region_end, callback,
 def main(print_only=False, check=False):
     lints = []
     deprecated_lints = []
+    restriction_lints = []
 
     # check directory
-    if not os.path.isfile('src/lib.rs'):
+    if not os.path.isfile('clippy_lints/src/lib.rs'):
         print('Error: call this script from clippy checkout directory!')
         return
 
     # collect all lints from source files
-    for root, _, files in os.walk('src'):
-        for fn in files:
-            if fn.endswith('.rs'):
-                collect(lints, deprecated_lints, os.path.join(root, fn))
+    for fn in os.listdir('clippy_lints/src'):
+        if fn.endswith('.rs'):
+            collect(lints, deprecated_lints, restriction_lints,
+                    os.path.join('clippy_lints', 'src', fn))
+
+    # determine version
+    with open('Cargo.toml') as fp:
+        for line in fp:
+            if line.startswith('version ='):
+                clippy_version = line.split()[2].strip('"')
+                break
+        else:
+            print('Error: version not found in Cargo.toml!')
+            return
 
     if print_only:
-        sys.stdout.writelines(gen_table(lints))
+        sys.stdout.writelines(gen_table(lints + restriction_lints))
         return
 
     # replace table in README.md
     changed = replace_region(
         'README.md', r'^name +\|', '^$',
-        lambda: gen_table(lints, link=wiki_link),
+        lambda: gen_table(lints + restriction_lints, link=wiki_link),
         write_back=not check)
 
     changed |= replace_region(
         'README.md',
         r'^There are \d+ lints included in this crate:', "",
-        lambda: ['There are %d lints included in this crate:\n' % len(lints)],
+        lambda: ['There are %d lints included in this crate:\n' %
+                 (len(lints) + len(restriction_lints))],
         write_back=not check)
 
     # update the links in the CHANGELOG
@@ -162,34 +188,54 @@ def main(print_only=False, check=False):
         "<!-- begin autogenerated links to wiki -->",
         "<!-- end autogenerated links to wiki -->",
         lambda: ["[`{0}`]: {1}#{0}\n".format(l[1], wiki_link) for l in
-                 sorted(lints + deprecated_lints, key=lambda l: l[1])],
+                 sorted(lints + restriction_lints + deprecated_lints,
+                        key=lambda l: l[1])],
+        replace_start=False, write_back=not check)
+
+    # update version of clippy_lints in Cargo.toml
+    changed |= replace_region(
+        'Cargo.toml', r'# begin automatic update', '# end automatic update',
+        lambda: ['clippy_lints = { version = "%s", path = "clippy_lints" }\n' %
+                 clippy_version],
+        replace_start=False, write_back=not check)
+
+    # update version of clippy_lints in Cargo.toml
+    changed |= replace_region(
+        'clippy_lints/Cargo.toml', r'# begin automatic update', '# end automatic update',
+        lambda: ['version = "%s"\n' % clippy_version],
         replace_start=False, write_back=not check)
 
     # update the `pub mod` list
     changed |= replace_region(
-        'src/lib.rs', r'begin lints modules', r'end lints modules',
-        lambda: gen_mods(lints),
+        'clippy_lints/src/lib.rs', r'begin lints modules', r'end lints modules',
+        lambda: gen_mods(lints + restriction_lints),
         replace_start=False, write_back=not check)
 
     # same for "clippy" lint collection
     changed |= replace_region(
-        'src/lib.rs', r'reg.register_lint_group\("clippy"', r'\]\);',
+        '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 "deprecated" lint collection
     changed |= replace_region(
-            'src/lib.rs', r'let mut store', r'end deprecated lints',
-            lambda: gen_deprecated(deprecated_lints),
-            replace_start=False,
-            write_back=not check)
+        'clippy_lints/src/lib.rs', r'let mut store', 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(
-        'src/lib.rs', r'reg.register_lint_group\("clippy_pedantic"', r'\]\);',
+        '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