mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-26 22:50:56 +00:00
Implementation + move one lint
This commit is contained in:
parent
9b10c4be8c
commit
ef9fdbb8a9
3 changed files with 84 additions and 9 deletions
|
@ -26,9 +26,9 @@ use utils::span_lint;
|
|||
/// ```rust
|
||||
/// let x = 3.14;
|
||||
/// ```
|
||||
declare_lint! {
|
||||
declare_clippy_lint! {
|
||||
pub APPROX_CONSTANT,
|
||||
Warn,
|
||||
correctness,
|
||||
"the approximate of a known float constant (in `std::fXX::consts`)"
|
||||
}
|
||||
|
||||
|
|
|
@ -66,6 +66,21 @@ macro_rules! declare_restriction_lint {
|
|||
};
|
||||
}
|
||||
|
||||
macro_rules! declare_clippy_lint {
|
||||
{ pub $name:tt, style, $description:tt } => {
|
||||
declare_lint! { pub $name, Warn, $description }
|
||||
};
|
||||
{ pub $name:tt, correctness, $description:tt } => {
|
||||
declare_lint! { pub $name, Deny, $description }
|
||||
};
|
||||
{ pub $name:tt, complexity, $description:tt } => {
|
||||
declare_lint! { pub $name, Warn, $description }
|
||||
};
|
||||
{ pub $name:tt, perf, $description:tt } => {
|
||||
declare_lint! { pub $name, Warn, $description }
|
||||
};
|
||||
}
|
||||
|
||||
pub mod consts;
|
||||
#[macro_use]
|
||||
pub mod utils;
|
||||
|
@ -442,7 +457,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
|
|||
]);
|
||||
|
||||
reg.register_lint_group("clippy", vec![
|
||||
approx_const::APPROX_CONSTANT,
|
||||
array_indexing::OUT_OF_BOUNDS_INDEXING,
|
||||
assign_ops::ASSIGN_OP_PATTERN,
|
||||
assign_ops::MISREFACTORED_ASSIGN_OP,
|
||||
|
@ -641,6 +655,19 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
|
|||
vec::USELESS_VEC,
|
||||
zero_div_zero::ZERO_DIVIDED_BY_ZERO,
|
||||
]);
|
||||
|
||||
reg.register_lint_group("clippy_style", vec![
|
||||
]);
|
||||
|
||||
reg.register_lint_group("clippy_complexity", vec![
|
||||
]);
|
||||
|
||||
reg.register_lint_group("clippy_correctness", vec![
|
||||
approx_const::APPROX_CONSTANT,
|
||||
]);
|
||||
|
||||
reg.register_lint_group("clippy_perf", vec![
|
||||
]);
|
||||
}
|
||||
|
||||
// only exists to let the dogfood integration test works.
|
||||
|
|
|
@ -27,12 +27,19 @@ declare_restriction_lint_re = re.compile(r'''
|
|||
" (?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*
|
||||
(?P<cat>[a-z_]+) \s*,\s*
|
||||
" (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
|
||||
''', re.VERBOSE | re.DOTALL)
|
||||
|
||||
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, fn):
|
||||
def collect(lints, deprecated_lints, restriction_lints, clippy_lints, fn):
|
||||
"""Collect all lints from a file.
|
||||
|
||||
Adds entries to the lints list as `(module, name, level, desc)`.
|
||||
|
@ -62,6 +69,15 @@ def collect(lints, deprecated_lints, restriction_lints, fn):
|
|||
"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],
|
||||
match.group('name').lower(),
|
||||
"allow",
|
||||
desc.replace('\\"', '"')))
|
||||
|
||||
|
||||
def gen_group(lints, levels=None):
|
||||
"""Write lint group (list of all lints in the form module::NAME)."""
|
||||
|
@ -130,6 +146,12 @@ def main(print_only=False, check=False):
|
|||
lints = []
|
||||
deprecated_lints = []
|
||||
restriction_lints = []
|
||||
clippy_lints = {
|
||||
"correctness": [],
|
||||
"style": [],
|
||||
"complexity": [],
|
||||
"perf": [],
|
||||
}
|
||||
|
||||
# check directory
|
||||
if not os.path.isfile('clippy_lints/src/lib.rs'):
|
||||
|
@ -139,7 +161,7 @@ def main(print_only=False, check=False):
|
|||
# collect all lints from source files
|
||||
for fn in os.listdir('clippy_lints/src'):
|
||||
if fn.endswith('.rs'):
|
||||
collect(lints, deprecated_lints, restriction_lints,
|
||||
collect(lints, deprecated_lints, restriction_lints, clippy_lints,
|
||||
os.path.join('clippy_lints', 'src', fn))
|
||||
|
||||
# determine version
|
||||
|
@ -152,8 +174,10 @@ 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']
|
||||
|
||||
if print_only:
|
||||
sys.stdout.writelines(gen_table(lints + restriction_lints))
|
||||
sys.stdout.writelines(gen_table(all_lints))
|
||||
return
|
||||
|
||||
# update the lint counter in README.md
|
||||
|
@ -161,7 +185,7 @@ def main(print_only=False, check=False):
|
|||
'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' %
|
||||
(len(lints) + len(restriction_lints))],
|
||||
(len(all_lints))],
|
||||
write_back=not check)
|
||||
|
||||
# update the links in the CHANGELOG
|
||||
|
@ -170,7 +194,7 @@ 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], docs_link) for l in
|
||||
sorted(lints + restriction_lints + deprecated_lints,
|
||||
sorted(all_lints + deprecated_lints,
|
||||
key=lambda l: l[1])],
|
||||
replace_start=False, write_back=not check)
|
||||
|
||||
|
@ -190,7 +214,7 @@ def main(print_only=False, check=False):
|
|||
# update the `pub mod` list
|
||||
changed |= replace_region(
|
||||
'clippy_lints/src/lib.rs', r'begin lints modules', r'end lints modules',
|
||||
lambda: gen_mods(lints + restriction_lints),
|
||||
lambda: gen_mods(all_lints),
|
||||
replace_start=False, write_back=not check)
|
||||
|
||||
# same for "clippy" lint collection
|
||||
|
@ -199,6 +223,30 @@ def main(print_only=False, check=False):
|
|||
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
|
||||
changed |= replace_region(
|
||||
'clippy_lints/src/lib.rs', r'reg.register_lint_group\("clippy_complexity"', r'\]\);',
|
||||
lambda: gen_group(clippy_lints['complexity']),
|
||||
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)
|
||||
|
||||
# same for "deprecated" lint collection
|
||||
changed |= replace_region(
|
||||
'clippy_lints/src/lib.rs', r'let mut store', r'end deprecated lints',
|
||||
|
|
Loading…
Reference in a new issue