Skip to content

Commit 5595027

Browse files
authored
Merge pull request #1869 from rust-lang-nursery/new_lint
Lint authoring tool
2 parents 86d6cec + 45bb3ec commit 5595027

File tree

134 files changed

+580
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+580
-12
lines changed

.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,4 @@ util/gh-pages/lints.json
3131

3232
helper.txt
3333

34-
*.stdout
35-
3634
.vscode

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ cache:
1616
env:
1717
global:
1818
# TRAVIS_TOKEN_CLIPPY_SERVICE
19-
secure: dj8SwwuRGuzbo2wZq5z7qXIf7P3p7cbSGs1I3pvXQmB6a58gkLiRn/qBcIIegdt/nzXs+Z0Nug+DdesYVeUPxk1hIa/eeU8p6mpyTtZ+30H4QVgVzd0VCthB5F/NUiPVxTgpGpEgCM9/p72xMwTn7AAJfsGqk7AJ4FS5ZZKhqFI=
19+
- secure: dj8SwwuRGuzbo2wZq5z7qXIf7P3p7cbSGs1I3pvXQmB6a58gkLiRn/qBcIIegdt/nzXs+Z0Nug+DdesYVeUPxk1hIa/eeU8p6mpyTtZ+30H4QVgVzd0VCthB5F/NUiPVxTgpGpEgCM9/p72xMwTn7AAJfsGqk7AJ4FS5ZZKhqFI=
20+
- RUST_BACKTRACE=1
2021

2122
install:
2223
- . $HOME/.nvm/nvm.sh

clippy_lints/src/bit_mask.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use rustc::lint::*;
44
use rustc_const_eval::lookup_const_by_id;
55
use syntax::ast::LitKind;
66
use syntax::codemap::Span;
7-
use utils::span_lint;
7+
use utils::{span_lint, span_lint_and_then};
8+
use utils::sugg::Sugg;
89

910
/// **What it does:** Checks for incompatible bit masks in comparisons.
1011
///
@@ -70,12 +71,29 @@ declare_lint! {
7071
"expressions where a bit mask will be rendered useless by a comparison, e.g. `(x | 1) > 2`"
7172
}
7273

74+
/// **What it does:** Checks for bit masks that can be replaced by a call
75+
/// to `trailing_zeros`
76+
///
77+
/// **Why is this bad?** `x.trailing_zeros() > 4` is much clearer than `x & 15 == 0`
78+
///
79+
/// **Known problems:** llvm generates better code for `x & 15 == 0` on x86
80+
///
81+
/// **Example:**
82+
/// ```rust
83+
/// x & 0x1111 == 0
84+
/// ```
85+
declare_lint! {
86+
pub VERBOSE_BIT_MASK,
87+
Warn,
88+
"expressions where a bit mask is less readable than the corresponding method call"
89+
}
90+
7391
#[derive(Copy,Clone)]
7492
pub struct BitMask;
7593

7694
impl LintPass for BitMask {
7795
fn get_lints(&self) -> LintArray {
78-
lint_array!(BAD_BIT_MASK, INEFFECTIVE_BIT_MASK)
96+
lint_array!(BAD_BIT_MASK, INEFFECTIVE_BIT_MASK, VERBOSE_BIT_MASK)
7997
}
8098
}
8199

@@ -90,6 +108,26 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
90108
}
91109
}
92110
}
111+
if_let_chain!{[
112+
let Expr_::ExprBinary(ref op, ref left, ref right) = e.node,
113+
BinOp_::BiEq == op.node,
114+
let Expr_::ExprBinary(ref op1, ref left1, ref right1) = left.node,
115+
BinOp_::BiBitAnd == op1.node,
116+
let Expr_::ExprLit(ref lit) = right1.node,
117+
let LitKind::Int(n, _) = lit.node,
118+
let Expr_::ExprLit(ref lit1) = right.node,
119+
let LitKind::Int(0, _) = lit1.node,
120+
n.leading_zeros() == n.count_zeros(),
121+
], {
122+
span_lint_and_then(cx,
123+
VERBOSE_BIT_MASK,
124+
e.span,
125+
"bit mask could be simplified with a call to `trailing_zeros`",
126+
|db| {
127+
let sugg = Sugg::hir(cx, left1, "...").maybe_par();
128+
db.span_suggestion(e.span, "try", format!("{}.trailing_zeros() > {}", sugg, n.count_ones()));
129+
});
130+
}}
93131
}
94132
}
95133

clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
222222
reg.register_early_lint_pass(box utils::internal_lints::Clippy);
223223
reg.register_late_lint_pass(box utils::internal_lints::LintWithoutLintPass::default());
224224
reg.register_late_lint_pass(box utils::inspector::Pass);
225+
reg.register_late_lint_pass(box utils::author::Pass);
225226
reg.register_late_lint_pass(box types::TypePass);
226227
reg.register_late_lint_pass(box booleans::NonminimalBool);
227228
reg.register_late_lint_pass(box eq_op::EqOp);
@@ -379,6 +380,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
379380
attrs::USELESS_ATTRIBUTE,
380381
bit_mask::BAD_BIT_MASK,
381382
bit_mask::INEFFECTIVE_BIT_MASK,
383+
bit_mask::VERBOSE_BIT_MASK,
382384
blacklisted_name::BLACKLISTED_NAME,
383385
block_in_if_condition::BLOCK_IN_IF_CONDITION_EXPR,
384386
block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT,

clippy_lints/src/literal_digit_grouping.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'a> DigitInfo<'a> {
147147
if self.digits.contains('.') {
148148
let mut parts = self.digits.split('.');
149149
let int_part_hint = parts.next()
150-
.unwrap()
150+
.expect("split always returns at least one element")
151151
.chars()
152152
.rev()
153153
.filter(|&c| c != '_')
@@ -158,7 +158,7 @@ impl<'a> DigitInfo<'a> {
158158
.collect::<Vec<String>>()
159159
.join("_");
160160
let frac_part_hint = parts.next()
161-
.unwrap()
161+
.expect("already checked that there is a `.`")
162162
.chars()
163163
.filter(|&c| c != '_')
164164
.collect::<Vec<_>>()
@@ -329,7 +329,7 @@ impl LiteralDigitGrouping {
329329
.windows(2)
330330
.all(|ps| ps[1] - ps[0] == group_size + 1)
331331
// number of digits to the left of the last group cannot be bigger than group size.
332-
&& (digits.len() - underscore_positions.last().unwrap() <= group_size + 1);
332+
&& (digits.len() - underscore_positions.last().expect("there's at least one element") <= group_size + 1);
333333

334334
if !consistent {
335335
return Err(WarningType::InconsistentDigitGrouping);

0 commit comments

Comments
 (0)