made is_negative(..) public (+doctest), fixed identity_op and precedence
This commit is contained in:
parent
e1438e7010
commit
759b45a46d
@ -246,7 +246,14 @@ fn neg_float_str(s: String) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_negative(ty: LitIntType) -> bool {
|
||||
/// is the given LitIntType negative?
|
||||
///
|
||||
/// Examples
|
||||
///
|
||||
/// ```
|
||||
/// assert!(is_negative(UnsuffixedIntLit(Minus)));
|
||||
/// ```
|
||||
pub fn is_negative(ty: LitIntType) -> bool {
|
||||
match ty {
|
||||
SignedIntLit(_, sign) | UnsuffixedIntLit(sign) => sign == Minus,
|
||||
UnsignedIntLit(_) => false,
|
||||
|
@ -7,6 +7,8 @@ use syntax::ast_util::{is_comparison_binop, binop_to_string};
|
||||
use syntax::ptr::P;
|
||||
use syntax::codemap::Span;
|
||||
|
||||
use consts::{constant, Constant, is_negative};
|
||||
use consts::ConstantVariant::ConstantInt;
|
||||
use utils::{span_lint, snippet};
|
||||
|
||||
declare_lint! { pub IDENTITY_OP, Warn,
|
||||
@ -47,35 +49,19 @@ impl LintPass for IdentityOp {
|
||||
|
||||
|
||||
fn check(cx: &Context, e: &Expr, m: i8, span: Span, arg: Span) {
|
||||
if have_lit(cx, e, m) {
|
||||
span_lint(cx, IDENTITY_OP, span, &format!(
|
||||
"the operation is ineffective. Consider reducing it to `{}`",
|
||||
snippet(cx, arg, "..")));
|
||||
}
|
||||
}
|
||||
|
||||
fn have_lit(cx: &Context, e : &Expr, m: i8) -> bool {
|
||||
match &e.node {
|
||||
&ExprUnary(UnNeg, ref litexp) => have_lit(cx, litexp, -m),
|
||||
&ExprLit(ref lit) => {
|
||||
match (&lit.node, m) {
|
||||
(&LitInt(0, _), 0) => true,
|
||||
(&LitInt(1, SignedIntLit(_, Plus)), 1) => true,
|
||||
(&LitInt(1, UnsuffixedIntLit(Plus)), 1) => true,
|
||||
(&LitInt(1, SignedIntLit(_, Minus)), -1) => true,
|
||||
(&LitInt(1, UnsuffixedIntLit(Minus)), -1) => true,
|
||||
_ => false
|
||||
}
|
||||
},
|
||||
&ExprParen(ref p) => have_lit(cx, p, m),
|
||||
&ExprPath(_, _) => {
|
||||
match cx.tcx.def_map.borrow().get(&e.id) {
|
||||
Some(&PathResolution { base_def: DefConst(id), ..}) =>
|
||||
lookup_const_by_id(cx.tcx, id, Option::None)
|
||||
.map_or(false, |l| have_lit(cx, l, m)),
|
||||
_ => false
|
||||
}
|
||||
},
|
||||
_ => false
|
||||
if let Some(c) = constant(cx, e) {
|
||||
if c.needed_resolution { return; } // skip linting w/ lookup for now
|
||||
if let ConstantInt(v, ty) = c.constant {
|
||||
if match m {
|
||||
0 => v == 0,
|
||||
-1 => is_negative(ty),
|
||||
1 => !is_negative(ty),
|
||||
_ => unreachable!(),
|
||||
} {
|
||||
span_lint(cx, IDENTITY_OP, span, &format!(
|
||||
"the operation is ineffective. Consider reducing it to `{}`",
|
||||
snippet(cx, arg, "..")));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,5 +13,8 @@ fn main() {
|
||||
|
||||
x != "foo".to_owned(); //~ERROR this creates an owned instance
|
||||
|
||||
// removed String::from_str(..), as it has finally been removed in 1.4.0
|
||||
// as of 2015-08-14
|
||||
|
||||
x != String::from("foo"); //~ERROR this creates an owned instance
|
||||
}
|
||||
|
@ -11,14 +11,14 @@ fn main() {
|
||||
|
||||
x + 0; //~ERROR the operation is ineffective
|
||||
0 + x; //~ERROR the operation is ineffective
|
||||
x - ZERO; //~ERROR the operation is ineffective
|
||||
x - ZERO; //no error, as we skip lookups (for now)
|
||||
x | (0); //~ERROR the operation is ineffective
|
||||
((ZERO)) | x; //~ERROR the operation is ineffective
|
||||
((ZERO)) | x; //no error, as we skip lookups (for now)
|
||||
|
||||
x * 1; //~ERROR the operation is ineffective
|
||||
1 * x; //~ERROR the operation is ineffective
|
||||
x / ONE; //~ERROR the operation is ineffective
|
||||
x / ONE; //no error, as we skip lookups (for now)
|
||||
|
||||
x & NEG_ONE; //~ERROR the operation is ineffective
|
||||
x & NEG_ONE; //no error, as we skip lookups (for now)
|
||||
-1 & x; //~ERROR the operation is ineffective
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
#![plugin(clippy)]
|
||||
|
||||
#[deny(precedence)]
|
||||
#[allow(identity_op)]
|
||||
#[allow(eq_op)]
|
||||
fn main() {
|
||||
format!("{} vs. {}", 1 << 2 + 3, (1 << 2) + 3); //~ERROR operator precedence can trip
|
||||
|
Loading…
Reference in New Issue
Block a user