mirror of https://github.com/rust-lang/rust.git
commit
5eb884b7b0
|
@ -6,10 +6,11 @@ A collection of lints to catch common mistakes and improve your Rust code.
|
||||||
[Jump to usage instructions](#usage)
|
[Jump to usage instructions](#usage)
|
||||||
|
|
||||||
##Lints
|
##Lints
|
||||||
There are 108 lints included in this crate:
|
There are 109 lints included in this crate:
|
||||||
|
|
||||||
name | default | meaning
|
name | default | meaning
|
||||||
---------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
---------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
[absurd_unsigned_comparisons](https://github.com/Manishearth/rust-clippy/wiki#absurd_unsigned_comparisons) | warn | testing whether an unsigned integer is non-positive
|
||||||
[approx_constant](https://github.com/Manishearth/rust-clippy/wiki#approx_constant) | warn | the approximate of a known float constant (in `std::f64::consts` or `std::f32::consts`) is found; suggests to use the constant
|
[approx_constant](https://github.com/Manishearth/rust-clippy/wiki#approx_constant) | warn | the approximate of a known float constant (in `std::f64::consts` or `std::f32::consts`) is found; suggests to use the constant
|
||||||
[bad_bit_mask](https://github.com/Manishearth/rust-clippy/wiki#bad_bit_mask) | warn | expressions of the form `_ & mask == select` that will only ever return `true` or `false` (because in the example `select` containing bits that `mask` doesn't have)
|
[bad_bit_mask](https://github.com/Manishearth/rust-clippy/wiki#bad_bit_mask) | warn | expressions of the form `_ & mask == select` that will only ever return `true` or `false` (because in the example `select` containing bits that `mask` doesn't have)
|
||||||
[block_in_if_condition_expr](https://github.com/Manishearth/rust-clippy/wiki#block_in_if_condition_expr) | warn | braces can be eliminated in conditions that are expressions, e.g `if { true } ...`
|
[block_in_if_condition_expr](https://github.com/Manishearth/rust-clippy/wiki#block_in_if_condition_expr) | warn | braces can be eliminated in conditions that are expressions, e.g `if { true } ...`
|
||||||
|
|
|
@ -137,7 +137,7 @@ impl<'a, 'tcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
|
||||||
.get(&borrow_id) {
|
.get(&borrow_id) {
|
||||||
if LoanCause::AutoRef == loan_cause {
|
if LoanCause::AutoRef == loan_cause {
|
||||||
// x.foo()
|
// x.foo()
|
||||||
if adj.autoderefs <= 0 {
|
if adj.autoderefs == 0 {
|
||||||
self.set.remove(&lid); // Used without autodereffing (i.e. x.clone())
|
self.set.remove(&lid); // Used without autodereffing (i.e. x.clone())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -149,6 +149,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
||||||
reg.register_late_lint_pass(box print::PrintLint);
|
reg.register_late_lint_pass(box print::PrintLint);
|
||||||
reg.register_late_lint_pass(box vec::UselessVec);
|
reg.register_late_lint_pass(box vec::UselessVec);
|
||||||
reg.register_late_lint_pass(box drop_ref::DropRefPass);
|
reg.register_late_lint_pass(box drop_ref::DropRefPass);
|
||||||
|
reg.register_late_lint_pass(box types::AbsurdUnsignedComparisons);
|
||||||
|
|
||||||
|
|
||||||
reg.register_lint_group("clippy_pedantic", vec![
|
reg.register_lint_group("clippy_pedantic", vec![
|
||||||
|
@ -254,6 +255,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
||||||
strings::STRING_LIT_AS_BYTES,
|
strings::STRING_LIT_AS_BYTES,
|
||||||
temporary_assignment::TEMPORARY_ASSIGNMENT,
|
temporary_assignment::TEMPORARY_ASSIGNMENT,
|
||||||
transmute::USELESS_TRANSMUTE,
|
transmute::USELESS_TRANSMUTE,
|
||||||
|
types::ABSURD_UNSIGNED_COMPARISONS,
|
||||||
types::BOX_VEC,
|
types::BOX_VEC,
|
||||||
types::CHAR_LIT_AS_U8,
|
types::CHAR_LIT_AS_U8,
|
||||||
types::LET_UNIT_VALUE,
|
types::LET_UNIT_VALUE,
|
||||||
|
|
52
src/types.rs
52
src/types.rs
|
@ -557,3 +557,55 @@ impl LateLintPass for CharLitAsU8 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// **What it does:** This lint checks for expressions where an unsigned integer is tested to be non-positive and suggests testing for equality with zero instead.
|
||||||
|
///
|
||||||
|
/// **Why is this bad?** `x <= 0` may mislead the reader into thinking `x` can be negative. `x == 0` makes explicit that zero is the only possibility.
|
||||||
|
///
|
||||||
|
/// **Known problems:** None
|
||||||
|
///
|
||||||
|
/// **Example:** `vec.len() <= 0`
|
||||||
|
declare_lint!(pub ABSURD_UNSIGNED_COMPARISONS, Warn,
|
||||||
|
"testing whether an unsigned integer is non-positive");
|
||||||
|
|
||||||
|
pub struct AbsurdUnsignedComparisons;
|
||||||
|
|
||||||
|
impl LintPass for AbsurdUnsignedComparisons {
|
||||||
|
fn get_lints(&self) -> LintArray {
|
||||||
|
lint_array!(ABSURD_UNSIGNED_COMPARISONS)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_zero_lit(expr: &Expr) -> bool {
|
||||||
|
use syntax::ast::Lit_;
|
||||||
|
|
||||||
|
if let ExprLit(ref l) = expr.node {
|
||||||
|
if let Lit_::LitInt(val, _) = l.node {
|
||||||
|
return val == 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LateLintPass for AbsurdUnsignedComparisons {
|
||||||
|
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
|
||||||
|
if let ExprBinary(ref cmp, ref lhs, ref rhs) = expr.node {
|
||||||
|
let op = cmp.node;
|
||||||
|
|
||||||
|
let comparee = match op {
|
||||||
|
BiLe if is_zero_lit(rhs) => lhs, // x <= 0
|
||||||
|
BiGe if is_zero_lit(lhs) => rhs, // 0 >= x
|
||||||
|
_ => return,
|
||||||
|
};
|
||||||
|
|
||||||
|
if let ty::TyUint(_) = cx.tcx.expr_ty(comparee).sty {
|
||||||
|
if !in_macro(cx, expr.span) {
|
||||||
|
let msg = "testing whether an unsigned integer is non-positive";
|
||||||
|
let help = format!("consider using {} == 0 instead",
|
||||||
|
snippet(cx, comparee.span, "x"));
|
||||||
|
span_help_and_lint(cx, ABSURD_UNSIGNED_COMPARISONS, expr.span, msg, &help);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
#![feature(plugin)]
|
||||||
|
#![plugin(clippy)]
|
||||||
|
|
||||||
|
#![allow(unused)]
|
||||||
|
|
||||||
|
#[deny(absurd_unsigned_comparisons)]
|
||||||
|
fn main() {
|
||||||
|
1u32 <= 0; //~ERROR testing whether an unsigned integer is non-positive
|
||||||
|
1u8 <= 0; //~ERROR testing whether an unsigned integer is non-positive
|
||||||
|
1i32 <= 0;
|
||||||
|
0 >= 1u32; //~ERROR testing whether an unsigned integer is non-positive
|
||||||
|
0 >= 1;
|
||||||
|
1u32 > 0;
|
||||||
|
}
|
Loading…
Reference in New Issue