fixed tests, added clippy_restrictions lint group
This commit is contained in:
parent
a967440186
commit
0b40ae178a
@ -15,9 +15,8 @@ use utils::span_lint;
|
|||||||
/// ```
|
/// ```
|
||||||
/// a + 1
|
/// a + 1
|
||||||
/// ```
|
/// ```
|
||||||
declare_lint! {
|
declare_restriction_lint! {
|
||||||
pub INTEGER_ARITHMETIC,
|
pub INTEGER_ARITHMETIC,
|
||||||
Allow,
|
|
||||||
"Any integer arithmetic statement"
|
"Any integer arithmetic statement"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,9 +31,8 @@ declare_lint! {
|
|||||||
/// ```
|
/// ```
|
||||||
/// a + 1.0
|
/// a + 1.0
|
||||||
/// ```
|
/// ```
|
||||||
declare_lint! {
|
declare_restriction_lint! {
|
||||||
pub FLOAT_ARITHMETIC,
|
pub FLOAT_ARITHMETIC,
|
||||||
Allow,
|
|
||||||
"Any floating-point arithmetic statement"
|
"Any floating-point arithmetic statement"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,32 +53,32 @@ impl LateLintPass for Arithmetic {
|
|||||||
match expr.node {
|
match expr.node {
|
||||||
hir::ExprBinary(ref op, ref l, ref r) => {
|
hir::ExprBinary(ref op, ref l, ref r) => {
|
||||||
match op.node {
|
match op.node {
|
||||||
hir::BiAnd | hir::BiOr | hir::BiBitAnd |
|
hir::BiAnd | hir::BiOr | hir::BiBitAnd |
|
||||||
hir::BiBitOr | hir::BiBitXor | hir::BiShl | hir::BiShr |
|
hir::BiBitOr | hir::BiBitXor | hir::BiShl | hir::BiShr |
|
||||||
hir::BiEq | hir::BiLt | hir::BiLe | hir::BiNe | hir::BiGe |
|
hir::BiEq | hir::BiLt | hir::BiLe | hir::BiNe | hir::BiGe |
|
||||||
hir::BiGt => return,
|
hir::BiGt => return,
|
||||||
_ => ()
|
_ => ()
|
||||||
}
|
}
|
||||||
let (l_ty, r_ty) = (cx.tcx.expr_ty(l), cx.tcx.expr_ty(r));
|
let (l_ty, r_ty) = (cx.tcx.expr_ty(l), cx.tcx.expr_ty(r));
|
||||||
if l_ty.is_integral() && r_ty.is_integral() {
|
if l_ty.is_integral() && r_ty.is_integral() {
|
||||||
span_lint(cx,
|
span_lint(cx,
|
||||||
INTEGER_ARITHMETIC,
|
INTEGER_ARITHMETIC,
|
||||||
expr.span,
|
expr.span,
|
||||||
"integer arithmetic detected");
|
"integer arithmetic detected");
|
||||||
self.span = Some(expr.span);
|
self.span = Some(expr.span);
|
||||||
} else if l_ty.is_floating_point() && r_ty.is_floating_point() {
|
} else if l_ty.is_floating_point() && r_ty.is_floating_point() {
|
||||||
span_lint(cx,
|
span_lint(cx,
|
||||||
FLOAT_ARITHMETIC,
|
FLOAT_ARITHMETIC,
|
||||||
expr.span,
|
expr.span,
|
||||||
"floating-point arithmetic detected");
|
"floating-point arithmetic detected");
|
||||||
self.span = Some(expr.span);
|
self.span = Some(expr.span);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
hir::ExprUnary(hir::UnOp::UnNeg, ref arg) => {
|
hir::ExprUnary(hir::UnOp::UnNeg, ref arg) => {
|
||||||
let ty = cx.tcx.expr_ty(arg);
|
let ty = cx.tcx.expr_ty(arg);
|
||||||
if ty.is_integral() {
|
if ty.is_integral() {
|
||||||
span_lint(cx,
|
span_lint(cx,
|
||||||
INTEGER_ARITHMETIC,
|
INTEGER_ARITHMETIC,
|
||||||
expr.span,
|
expr.span,
|
||||||
"integer arithmetic detected");
|
"integer arithmetic detected");
|
||||||
self.span = Some(expr.span);
|
self.span = Some(expr.span);
|
||||||
@ -95,7 +93,7 @@ impl LateLintPass for Arithmetic {
|
|||||||
_ => ()
|
_ => ()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_expr_post(&mut self, _: &LateContext, expr: &hir::Expr) {
|
fn check_expr_post(&mut self, _: &LateContext, expr: &hir::Expr) {
|
||||||
if Some(expr.span) == self.span {
|
if Some(expr.span) == self.span {
|
||||||
self.span = None;
|
self.span = None;
|
||||||
|
11
src/lib.rs
11
src/lib.rs
@ -44,6 +44,12 @@ extern crate rustc_const_eval;
|
|||||||
extern crate rustc_const_math;
|
extern crate rustc_const_math;
|
||||||
use rustc_plugin::Registry;
|
use rustc_plugin::Registry;
|
||||||
|
|
||||||
|
macro_rules! declare_restriction_lint {
|
||||||
|
{ pub $name:tt, $description:tt } => {
|
||||||
|
declare_lint! { pub $name, Allow, $description }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub mod consts;
|
pub mod consts;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
@ -242,9 +248,12 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
|||||||
reg.register_late_lint_pass(box mem_forget::MemForget);
|
reg.register_late_lint_pass(box mem_forget::MemForget);
|
||||||
reg.register_late_lint_pass(box arithmetic::Arithmetic::default());
|
reg.register_late_lint_pass(box arithmetic::Arithmetic::default());
|
||||||
|
|
||||||
reg.register_lint_group("clippy_pedantic", vec![
|
reg.register_lint_group("clippy_restrictions", vec![
|
||||||
arithmetic::FLOAT_ARITHMETIC,
|
arithmetic::FLOAT_ARITHMETIC,
|
||||||
arithmetic::INTEGER_ARITHMETIC,
|
arithmetic::INTEGER_ARITHMETIC,
|
||||||
|
]);
|
||||||
|
|
||||||
|
reg.register_lint_group("clippy_pedantic", vec![
|
||||||
array_indexing::INDEXING_SLICING,
|
array_indexing::INDEXING_SLICING,
|
||||||
booleans::NONMINIMAL_BOOL,
|
booleans::NONMINIMAL_BOOL,
|
||||||
enum_glob_use::ENUM_GLOB_USE,
|
enum_glob_use::ENUM_GLOB_USE,
|
||||||
|
@ -15,7 +15,6 @@ fn main() {
|
|||||||
i & 1; // no wrapping
|
i & 1; // no wrapping
|
||||||
i | 1;
|
i | 1;
|
||||||
i ^ 1;
|
i ^ 1;
|
||||||
i % 7;
|
|
||||||
i >> 1;
|
i >> 1;
|
||||||
i << 1;
|
i << 1;
|
||||||
|
|
||||||
|
@ -21,12 +21,18 @@ declare_deprecated_lint_re = re.compile(r'''
|
|||||||
" (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
|
" (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
|
||||||
''', re.VERBOSE | re.DOTALL)
|
''', 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*')
|
nl_escape_re = re.compile(r'\\\n\s*')
|
||||||
|
|
||||||
wiki_link = 'https://github.com/Manishearth/rust-clippy/wiki'
|
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.
|
"""Collect all lints from a file.
|
||||||
|
|
||||||
Adds entries to the lints list as `(module, name, level, desc)`.
|
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(),
|
match.group('name').lower(),
|
||||||
desc.replace('\\"', '"')))
|
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):
|
def gen_table(lints, link=None):
|
||||||
"""Write lint table in Markdown format."""
|
"""Write lint table in Markdown format."""
|
||||||
@ -86,7 +100,6 @@ def gen_deprecated(lints):
|
|||||||
for lint in lints:
|
for lint in lints:
|
||||||
yield ' store.register_removed("%s", "%s");\n' % (lint[1], lint[2])
|
yield ' store.register_removed("%s", "%s");\n' % (lint[1], lint[2])
|
||||||
|
|
||||||
|
|
||||||
def replace_region(fn, region_start, region_end, callback,
|
def replace_region(fn, region_start, region_end, callback,
|
||||||
replace_start=True, write_back=True):
|
replace_start=True, write_back=True):
|
||||||
"""Replace a region in a file delimited by two lines matching regexes.
|
"""Replace a region in a file delimited by two lines matching regexes.
|
||||||
@ -128,6 +141,7 @@ def replace_region(fn, region_start, region_end, callback,
|
|||||||
def main(print_only=False, check=False):
|
def main(print_only=False, check=False):
|
||||||
lints = []
|
lints = []
|
||||||
deprecated_lints = []
|
deprecated_lints = []
|
||||||
|
restriction_lints = []
|
||||||
|
|
||||||
# check directory
|
# check directory
|
||||||
if not os.path.isfile('src/lib.rs'):
|
if not os.path.isfile('src/lib.rs'):
|
||||||
@ -138,22 +152,24 @@ def main(print_only=False, check=False):
|
|||||||
for root, _, files in os.walk('src'):
|
for root, _, files in os.walk('src'):
|
||||||
for fn in files:
|
for fn in files:
|
||||||
if fn.endswith('.rs'):
|
if fn.endswith('.rs'):
|
||||||
collect(lints, deprecated_lints, os.path.join(root, fn))
|
collect(lints, deprecated_lints, restriction_lints,
|
||||||
|
os.path.join(root, fn))
|
||||||
|
|
||||||
if print_only:
|
if print_only:
|
||||||
sys.stdout.writelines(gen_table(lints))
|
sys.stdout.writelines(gen_table(lints + restriction_lints))
|
||||||
return
|
return
|
||||||
|
|
||||||
# replace table in README.md
|
# replace table in README.md
|
||||||
changed = replace_region(
|
changed = replace_region(
|
||||||
'README.md', r'^name +\|', '^$',
|
'README.md', r'^name +\|', '^$',
|
||||||
lambda: gen_table(lints, link=wiki_link),
|
lambda: gen_table(lints + restriction_lints, link=wiki_link),
|
||||||
write_back=not check)
|
write_back=not check)
|
||||||
|
|
||||||
changed |= replace_region(
|
changed |= replace_region(
|
||||||
'README.md',
|
'README.md',
|
||||||
r'^There are \d+ lints included in this crate:', "",
|
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)
|
write_back=not check)
|
||||||
|
|
||||||
# update the links in the CHANGELOG
|
# update the links in the CHANGELOG
|
||||||
@ -162,13 +178,14 @@ def main(print_only=False, check=False):
|
|||||||
"<!-- begin autogenerated links to wiki -->",
|
"<!-- begin autogenerated links to wiki -->",
|
||||||
"<!-- end autogenerated links to wiki -->",
|
"<!-- end autogenerated links to wiki -->",
|
||||||
lambda: ["[`{0}`]: {1}#{0}\n".format(l[1], wiki_link) for l in
|
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)
|
replace_start=False, write_back=not check)
|
||||||
|
|
||||||
# update the `pub mod` list
|
# update the `pub mod` list
|
||||||
changed |= replace_region(
|
changed |= replace_region(
|
||||||
'src/lib.rs', r'begin lints modules', r'end lints modules',
|
'src/lib.rs', r'begin lints modules', r'end lints modules',
|
||||||
lambda: gen_mods(lints),
|
lambda: gen_mods(lints + restriction_lints),
|
||||||
replace_start=False, write_back=not check)
|
replace_start=False, write_back=not check)
|
||||||
|
|
||||||
# same for "clippy" lint collection
|
# same for "clippy" lint collection
|
||||||
@ -190,6 +207,12 @@ def main(print_only=False, check=False):
|
|||||||
lambda: gen_group(lints, levels=('allow',)),
|
lambda: gen_group(lints, levels=('allow',)),
|
||||||
replace_start=False, write_back=not check)
|
replace_start=False, write_back=not check)
|
||||||
|
|
||||||
|
# same for "clippy_restrictions" lint collection
|
||||||
|
changed |= replace_region(
|
||||||
|
'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:
|
if check and changed:
|
||||||
print('Please run util/update_lints.py to regenerate lints lists.')
|
print('Please run util/update_lints.py to regenerate lints lists.')
|
||||||
return 1
|
return 1
|
||||||
|
Loading…
Reference in New Issue
Block a user