Skip to content

Commit

Permalink
Merge pull request #1869 from rust-lang-nursery/new_lint
Browse files Browse the repository at this point in the history
Lint authoring tool
  • Loading branch information
oli-obk committed Aug 1, 2017
2 parents 86d6cec + 45bb3ec commit 5595027
Show file tree
Hide file tree
Showing 134 changed files with 580 additions and 12 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,4 @@ util/gh-pages/lints.json

helper.txt

*.stdout

.vscode
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ cache:
env:
global:
# TRAVIS_TOKEN_CLIPPY_SERVICE
secure: dj8SwwuRGuzbo2wZq5z7qXIf7P3p7cbSGs1I3pvXQmB6a58gkLiRn/qBcIIegdt/nzXs+Z0Nug+DdesYVeUPxk1hIa/eeU8p6mpyTtZ+30H4QVgVzd0VCthB5F/NUiPVxTgpGpEgCM9/p72xMwTn7AAJfsGqk7AJ4FS5ZZKhqFI=
- secure: dj8SwwuRGuzbo2wZq5z7qXIf7P3p7cbSGs1I3pvXQmB6a58gkLiRn/qBcIIegdt/nzXs+Z0Nug+DdesYVeUPxk1hIa/eeU8p6mpyTtZ+30H4QVgVzd0VCthB5F/NUiPVxTgpGpEgCM9/p72xMwTn7AAJfsGqk7AJ4FS5ZZKhqFI=
- RUST_BACKTRACE=1

install:
- . $HOME/.nvm/nvm.sh
Expand Down
42 changes: 40 additions & 2 deletions clippy_lints/src/bit_mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use rustc::lint::*;
use rustc_const_eval::lookup_const_by_id;
use syntax::ast::LitKind;
use syntax::codemap::Span;
use utils::span_lint;
use utils::{span_lint, span_lint_and_then};
use utils::sugg::Sugg;

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

/// **What it does:** Checks for bit masks that can be replaced by a call
/// to `trailing_zeros`
///
/// **Why is this bad?** `x.trailing_zeros() > 4` is much clearer than `x & 15 == 0`
///
/// **Known problems:** llvm generates better code for `x & 15 == 0` on x86
///
/// **Example:**
/// ```rust
/// x & 0x1111 == 0
/// ```
declare_lint! {
pub VERBOSE_BIT_MASK,
Warn,
"expressions where a bit mask is less readable than the corresponding method call"
}

#[derive(Copy,Clone)]
pub struct BitMask;

impl LintPass for BitMask {
fn get_lints(&self) -> LintArray {
lint_array!(BAD_BIT_MASK, INEFFECTIVE_BIT_MASK)
lint_array!(BAD_BIT_MASK, INEFFECTIVE_BIT_MASK, VERBOSE_BIT_MASK)
}
}

Expand All @@ -90,6 +108,26 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
}
}
}
if_let_chain!{[
let Expr_::ExprBinary(ref op, ref left, ref right) = e.node,
BinOp_::BiEq == op.node,
let Expr_::ExprBinary(ref op1, ref left1, ref right1) = left.node,
BinOp_::BiBitAnd == op1.node,
let Expr_::ExprLit(ref lit) = right1.node,
let LitKind::Int(n, _) = lit.node,
let Expr_::ExprLit(ref lit1) = right.node,
let LitKind::Int(0, _) = lit1.node,
n.leading_zeros() == n.count_zeros(),
], {
span_lint_and_then(cx,
VERBOSE_BIT_MASK,
e.span,
"bit mask could be simplified with a call to `trailing_zeros`",
|db| {
let sugg = Sugg::hir(cx, left1, "...").maybe_par();
db.span_suggestion(e.span, "try", format!("{}.trailing_zeros() > {}", sugg, n.count_ones()));
});
}}
}
}

Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
reg.register_early_lint_pass(box utils::internal_lints::Clippy);
reg.register_late_lint_pass(box utils::internal_lints::LintWithoutLintPass::default());
reg.register_late_lint_pass(box utils::inspector::Pass);
reg.register_late_lint_pass(box utils::author::Pass);
reg.register_late_lint_pass(box types::TypePass);
reg.register_late_lint_pass(box booleans::NonminimalBool);
reg.register_late_lint_pass(box eq_op::EqOp);
Expand Down Expand Up @@ -379,6 +380,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
attrs::USELESS_ATTRIBUTE,
bit_mask::BAD_BIT_MASK,
bit_mask::INEFFECTIVE_BIT_MASK,
bit_mask::VERBOSE_BIT_MASK,
blacklisted_name::BLACKLISTED_NAME,
block_in_if_condition::BLOCK_IN_IF_CONDITION_EXPR,
block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT,
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/literal_digit_grouping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl<'a> DigitInfo<'a> {
if self.digits.contains('.') {
let mut parts = self.digits.split('.');
let int_part_hint = parts.next()
.unwrap()
.expect("split always returns at least one element")
.chars()
.rev()
.filter(|&c| c != '_')
Expand All @@ -158,7 +158,7 @@ impl<'a> DigitInfo<'a> {
.collect::<Vec<String>>()
.join("_");
let frac_part_hint = parts.next()
.unwrap()
.expect("already checked that there is a `.`")
.chars()
.filter(|&c| c != '_')
.collect::<Vec<_>>()
Expand Down Expand Up @@ -329,7 +329,7 @@ impl LiteralDigitGrouping {
.windows(2)
.all(|ps| ps[1] - ps[0] == group_size + 1)
// number of digits to the left of the last group cannot be bigger than group size.
&& (digits.len() - underscore_positions.last().unwrap() <= group_size + 1);
&& (digits.len() - underscore_positions.last().expect("there's at least one element") <= group_size + 1);

if !consistent {
return Err(WarningType::InconsistentDigitGrouping);
Expand Down
Loading

0 comments on commit 5595027

Please sign in to comment.