Skip to content

Commit 0b40ae1

Browse files
committed
fixed tests, added clippy_restrictions lint group
1 parent a967440 commit 0b40ae1

File tree

4 files changed

+53
-24
lines changed

4 files changed

+53
-24
lines changed

src/arithmetic.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ use utils::span_lint;
1515
/// ```
1616
/// a + 1
1717
/// ```
18-
declare_lint! {
18+
declare_restriction_lint! {
1919
pub INTEGER_ARITHMETIC,
20-
Allow,
2120
"Any integer arithmetic statement"
2221
}
2322

@@ -32,9 +31,8 @@ declare_lint! {
3231
/// ```
3332
/// a + 1.0
3433
/// ```
35-
declare_lint! {
34+
declare_restriction_lint! {
3635
pub FLOAT_ARITHMETIC,
37-
Allow,
3836
"Any floating-point arithmetic statement"
3937
}
4038

@@ -55,32 +53,32 @@ impl LateLintPass for Arithmetic {
5553
match expr.node {
5654
hir::ExprBinary(ref op, ref l, ref r) => {
5755
match op.node {
58-
hir::BiAnd | hir::BiOr | hir::BiBitAnd |
59-
hir::BiBitOr | hir::BiBitXor | hir::BiShl | hir::BiShr |
60-
hir::BiEq | hir::BiLt | hir::BiLe | hir::BiNe | hir::BiGe |
56+
hir::BiAnd | hir::BiOr | hir::BiBitAnd |
57+
hir::BiBitOr | hir::BiBitXor | hir::BiShl | hir::BiShr |
58+
hir::BiEq | hir::BiLt | hir::BiLe | hir::BiNe | hir::BiGe |
6159
hir::BiGt => return,
6260
_ => ()
6361
}
6462
let (l_ty, r_ty) = (cx.tcx.expr_ty(l), cx.tcx.expr_ty(r));
6563
if l_ty.is_integral() && r_ty.is_integral() {
66-
span_lint(cx,
67-
INTEGER_ARITHMETIC,
64+
span_lint(cx,
65+
INTEGER_ARITHMETIC,
6866
expr.span,
6967
"integer arithmetic detected");
70-
self.span = Some(expr.span);
68+
self.span = Some(expr.span);
7169
} else if l_ty.is_floating_point() && r_ty.is_floating_point() {
7270
span_lint(cx,
7371
FLOAT_ARITHMETIC,
7472
expr.span,
7573
"floating-point arithmetic detected");
76-
self.span = Some(expr.span);
74+
self.span = Some(expr.span);
7775
}
7876
},
7977
hir::ExprUnary(hir::UnOp::UnNeg, ref arg) => {
8078
let ty = cx.tcx.expr_ty(arg);
8179
if ty.is_integral() {
82-
span_lint(cx,
83-
INTEGER_ARITHMETIC,
80+
span_lint(cx,
81+
INTEGER_ARITHMETIC,
8482
expr.span,
8583
"integer arithmetic detected");
8684
self.span = Some(expr.span);
@@ -95,7 +93,7 @@ impl LateLintPass for Arithmetic {
9593
_ => ()
9694
}
9795
}
98-
96+
9997
fn check_expr_post(&mut self, _: &LateContext, expr: &hir::Expr) {
10098
if Some(expr.span) == self.span {
10199
self.span = None;

src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ extern crate rustc_const_eval;
4444
extern crate rustc_const_math;
4545
use rustc_plugin::Registry;
4646

47+
macro_rules! declare_restriction_lint {
48+
{ pub $name:tt, $description:tt } => {
49+
declare_lint! { pub $name, Allow, $description }
50+
};
51+
}
52+
4753
pub mod consts;
4854
#[macro_use]
4955
pub mod utils;
@@ -242,9 +248,12 @@ pub fn plugin_registrar(reg: &mut Registry) {
242248
reg.register_late_lint_pass(box mem_forget::MemForget);
243249
reg.register_late_lint_pass(box arithmetic::Arithmetic::default());
244250

245-
reg.register_lint_group("clippy_pedantic", vec![
251+
reg.register_lint_group("clippy_restrictions", vec![
246252
arithmetic::FLOAT_ARITHMETIC,
247253
arithmetic::INTEGER_ARITHMETIC,
254+
]);
255+
256+
reg.register_lint_group("clippy_pedantic", vec![
248257
array_indexing::INDEXING_SLICING,
249258
booleans::NONMINIMAL_BOOL,
250259
enum_glob_use::ENUM_GLOB_USE,

tests/compile-fail/arithmetic.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ fn main() {
1515
i & 1; // no wrapping
1616
i | 1;
1717
i ^ 1;
18-
i % 7;
1918
i >> 1;
2019
i << 1;
2120

util/update_lints.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@
2121
" (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
2222
''', re.VERBOSE | re.DOTALL)
2323

24+
declare_restriction_lint_re = re.compile(r'''
25+
declare_restriction_lint! \s* [{(] \s*
26+
pub \s+ (?P<name>[A-Z_][A-Z_0-9]*) \s*,\s*
27+
" (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
28+
''', re.VERBOSE | re.DOTALL)
29+
2430
nl_escape_re = re.compile(r'\\\n\s*')
2531

2632
wiki_link = 'https://github.com/Manishearth/rust-clippy/wiki'
2733

2834

29-
def collect(lints, deprecated_lints, fn):
35+
def collect(lints, deprecated_lints, restriction_lints, fn):
3036
"""Collect all lints from a file.
3137
3238
Adds entries to the lints list as `(module, name, level, desc)`.
@@ -48,6 +54,14 @@ def collect(lints, deprecated_lints, fn):
4854
match.group('name').lower(),
4955
desc.replace('\\"', '"')))
5056

57+
for match in declare_restriction_lint_re.finditer(code):
58+
# remove \-newline escapes from description string
59+
desc = nl_escape_re.sub('', match.group('desc'))
60+
restriction_lints.append((os.path.splitext(os.path.basename(fn))[0],
61+
match.group('name').lower(),
62+
"allow",
63+
desc.replace('\\"', '"')))
64+
5165

5266
def gen_table(lints, link=None):
5367
"""Write lint table in Markdown format."""
@@ -86,7 +100,6 @@ def gen_deprecated(lints):
86100
for lint in lints:
87101
yield ' store.register_removed("%s", "%s");\n' % (lint[1], lint[2])
88102

89-
90103
def replace_region(fn, region_start, region_end, callback,
91104
replace_start=True, write_back=True):
92105
"""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,
128141
def main(print_only=False, check=False):
129142
lints = []
130143
deprecated_lints = []
144+
restriction_lints = []
131145

132146
# check directory
133147
if not os.path.isfile('src/lib.rs'):
@@ -138,22 +152,24 @@ def main(print_only=False, check=False):
138152
for root, _, files in os.walk('src'):
139153
for fn in files:
140154
if fn.endswith('.rs'):
141-
collect(lints, deprecated_lints, os.path.join(root, fn))
155+
collect(lints, deprecated_lints, restriction_lints,
156+
os.path.join(root, fn))
142157

143158
if print_only:
144-
sys.stdout.writelines(gen_table(lints))
159+
sys.stdout.writelines(gen_table(lints + restriction_lints))
145160
return
146161

147162
# replace table in README.md
148163
changed = replace_region(
149164
'README.md', r'^name +\|', '^$',
150-
lambda: gen_table(lints, link=wiki_link),
165+
lambda: gen_table(lints + restriction_lints, link=wiki_link),
151166
write_back=not check)
152167

153168
changed |= replace_region(
154169
'README.md',
155170
r'^There are \d+ lints included in this crate:', "",
156-
lambda: ['There are %d lints included in this crate:\n' % len(lints)],
171+
lambda: ['There are %d lints included in this crate:\n' % (len(lints)
172+
+ len(restriction_lints))],
157173
write_back=not check)
158174

159175
# update the links in the CHANGELOG
@@ -162,13 +178,14 @@ def main(print_only=False, check=False):
162178
"<!-- begin autogenerated links to wiki -->",
163179
"<!-- end autogenerated links to wiki -->",
164180
lambda: ["[`{0}`]: {1}#{0}\n".format(l[1], wiki_link) for l in
165-
sorted(lints + deprecated_lints, key=lambda l: l[1])],
181+
sorted(lints + restriction_lints + deprecated_lints,
182+
key=lambda l: l[1])],
166183
replace_start=False, write_back=not check)
167184

168185
# update the `pub mod` list
169186
changed |= replace_region(
170187
'src/lib.rs', r'begin lints modules', r'end lints modules',
171-
lambda: gen_mods(lints),
188+
lambda: gen_mods(lints + restriction_lints),
172189
replace_start=False, write_back=not check)
173190

174191
# same for "clippy" lint collection
@@ -190,6 +207,12 @@ def main(print_only=False, check=False):
190207
lambda: gen_group(lints, levels=('allow',)),
191208
replace_start=False, write_back=not check)
192209

210+
# same for "clippy_restrictions" lint collection
211+
changed |= replace_region(
212+
'src/lib.rs', r'reg.register_lint_group\("clippy_restrictions"',
213+
r'\]\);', lambda: gen_group(restriction_lints),
214+
replace_start=False, write_back=not check)
215+
193216
if check and changed:
194217
print('Please run util/update_lints.py to regenerate lints lists.')
195218
return 1

0 commit comments

Comments
 (0)