1 # Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 # file at the top-level directory of this distribution and at
3 # http://rust-lang.org/COPYRIGHT.
5 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 # option. This file may not be copied, modified, or distributed
9 # except according to those terms.
11 # Common utils for the several housekeeping scripts.
18 log.basicConfig(level=log.INFO, format='%(levelname)s: %(message)s')
20 Lint = collections.namedtuple('Lint', 'name level doc sourcefile group')
21 Config = collections.namedtuple('Config', 'name ty doc default')
23 lintname_re = re.compile(r'''pub\s+([A-Z_][A-Z_0-9]*)''')
24 group_re = re.compile(r'''\s*([a-z_][a-z_0-9]+)''')
25 conf_re = re.compile(r'''define_Conf! {\n([^}]*)\n}''', re.MULTILINE)
26 confvar_re = re.compile(
27 r'''/// Lint: (\w+). (.*).*\n\s*\([^,]+,\s+"([^"]+)",\s+([^=\)]+)=>\s+(.*)\),''', re.MULTILINE)
30 "correctness": 'Deny',
34 "restriction": 'Allow',
41 def parse_lints(lints, filepath):
48 with open(filepath) as fp:
51 if line.startswith("/// "):
52 last_comment.append(line[4:])
53 elif line.startswith("///"):
54 last_comment.append(line[3:])
55 elif line.startswith("declare_lint!"):
57 print("don't use `declare_lint!` in Clippy, use `declare_clippy_lint!` instead")
59 elif line.startswith("declare_clippy_lint!"):
64 elif line.startswith("declare_deprecated_lint!"):
71 m = lintname_re.search(line)
74 name = m.group(1).lower()
82 g = group_re.search(line)
84 group = g.group(1).lower()
85 level = lint_levels.get(group, None)
92 log.info("found %s with level %s in %s",
93 name, level, filepath)
94 lints.append(Lint(name, level, last_comment, filepath, group))
99 log.warn("Warning: missing Lint-Name in %s", filepath)
103 def parse_configs(path):
105 with open(os.path.join(path, 'utils/conf.rs')) as fp:
108 match = re.search(conf_re, contents)
109 confvars = re.findall(confvar_re, match.group(1))
111 for (lint, doc, name, default, ty) in confvars:
112 configs[lint.lower()] = Config(name.replace("_", "-"), ty, doc, default)
117 def parse_all(path="clippy_lints/src"):
119 for root, dirs, files in os.walk(path):
121 if fn.endswith('.rs'):
122 parse_lints(lints, os.path.join(root, fn))
124 log.info("got %s lints", len(lints))
126 configs = parse_configs(path)
127 log.info("got %d configs", len(configs))
129 return lints, configs