Add is_integer_literal utility function

Replaces is_lit_zero and is_lit_one which were used in a couple of
places.
This commit is contained in:
Nathan Weston 2015-09-04 09:26:58 -04:00
parent f87dd31f30
commit 1e320b38c1
4 changed files with 19 additions and 47 deletions

View File

@ -9,7 +9,7 @@ use rustc::front::map::Node::{NodeBlock};
use std::collections::{HashSet,HashMap};
use utils::{snippet, span_lint, get_parent_expr, match_trait_method, match_type,
in_external_macro, expr_block, span_help_and_lint};
in_external_macro, expr_block, span_help_and_lint, is_integer_literal};
use utils::{VEC_PATH, LL_PATH};
declare_lint!{ pub NEEDLESS_RANGE_LOOP, Warn,
@ -339,7 +339,7 @@ impl<'v, 't> Visitor<'v> for IncrementVisitor<'v, 't> {
match parent.node {
ExprAssignOp(op, ref lhs, ref rhs) =>
if lhs.id == expr.id {
if op.node == BiAdd && is_lit_one(rhs) {
if op.node == BiAdd && is_integer_literal(rhs, 1) {
*state = match *state {
VarState::Initial if self.depth == 0 => VarState::IncrOnce,
_ => VarState::DontWarn
@ -392,7 +392,7 @@ impl<'v, 't> Visitor<'v> for InitializeVisitor<'v, 't> {
self.name = Some(ident.node.name);
self.state = if let Some(ref init) = local.init {
if is_lit_zero(init) {
if is_integer_literal(init, 0) {
VarState::Warn
} else {
VarState::Declared
@ -425,7 +425,7 @@ impl<'v, 't> Visitor<'v> for InitializeVisitor<'v, 't> {
self.state = VarState::DontWarn;
},
ExprAssign(ref lhs, ref rhs) if lhs.id == expr.id => {
self.state = if is_lit_zero(rhs) && self.depth == 0 {
self.state = if is_integer_literal(rhs, 0) && self.depth == 0 {
VarState::Warn
} else {
VarState::DontWarn
@ -473,23 +473,3 @@ fn is_conditional(expr: &Expr) -> bool {
_ => false
}
}
// FIXME: copy/paste from misc.rs
fn is_lit_one(expr: &Expr) -> bool {
if let ExprLit(ref spanned) = expr.node {
if let LitInt(1, _) = spanned.node {
return true;
}
}
false
}
// FIXME: copy/paste from ranges.rs
fn is_lit_zero(expr: &Expr) -> bool {
if let ExprLit(ref spanned) = expr.node {
if let LitInt(0, _) = spanned.node {
return true;
}
}
false
}

View File

@ -7,7 +7,7 @@ use syntax::codemap::{Span, Spanned};
use rustc_front::visit::FnKind;
use rustc::middle::ty;
use utils::{get_item_name, match_path, snippet, span_lint, walk_ptrs_ty};
use utils::{get_item_name, match_path, snippet, span_lint, walk_ptrs_ty, is_integer_literal};
use consts::constant;
declare_lint!(pub TOPLEVEL_REF_ARG, Warn,
@ -183,7 +183,7 @@ impl LintPass for ModuloOne {
fn check_expr(&mut self, cx: &Context, expr: &Expr) {
if let ExprBinary(ref cmp, _, ref right) = expr.node {
if let &Spanned {node: BinOp_::BiRem, ..} = cmp {
if is_lit_one(right) {
if is_integer_literal(right, 1) {
cx.span_lint(MODULO_ONE, expr.span, "any number modulo 1 will be 0");
}
}
@ -191,15 +191,6 @@ impl LintPass for ModuloOne {
}
}
fn is_lit_one(expr: &Expr) -> bool {
if let ExprLit(ref spanned) = expr.node {
if let LitInt(1, _) = spanned.node {
return true;
}
}
false
}
declare_lint!(pub REDUNDANT_PATTERN, Warn, "using `name @ _` in a pattern");
#[derive(Copy,Clone)]

View File

@ -1,7 +1,7 @@
use rustc::lint::{Context, LintArray, LintPass};
use rustc_front::hir::*;
use syntax::codemap::Spanned;
use utils::match_type;
use utils::{match_type, is_integer_literal};
declare_lint! {
pub RANGE_STEP_BY_ZERO, Warn,
@ -21,7 +21,7 @@ impl LintPass for StepByZero {
ref args) = expr.node {
// Only warn on literal ranges.
if ident.name == "step_by" && args.len() == 2 &&
is_range(cx, &args[0]) && is_lit_zero(&args[1]) {
is_range(cx, &args[0]) && is_integer_literal(&args[1], 0) {
cx.span_lint(RANGE_STEP_BY_ZERO, expr.span,
"Range::step_by(0) produces an infinite iterator. \
Consider using `std::iter::repeat()` instead")
@ -37,13 +37,3 @@ fn is_range(cx: &Context, expr: &Expr) -> bool {
// Note: RangeTo and RangeFull don't have step_by
match_type(cx, ty, &["core", "ops", "Range"]) || match_type(cx, ty, &["core", "ops", "RangeFrom"])
}
fn is_lit_zero(expr: &Expr) -> bool {
// FIXME: use constant folding
if let ExprLit(ref spanned) = expr.node {
if let LitInt(0, _) = spanned.node {
return true;
}
}
false
}

View File

@ -244,6 +244,17 @@ pub fn walk_ptrs_ty_depth(ty: ty::Ty) -> (ty::Ty, usize) {
inner(ty, 0)
}
pub fn is_integer_literal(expr: &Expr, value: u64) -> bool
{
// FIXME: use constant folding
if let ExprLit(ref spanned) = expr.node {
if let LitInt(v, _) = spanned.node {
return v == value;
}
}
false
}
/// Produce a nested chain of if-lets and ifs from the patterns:
///
/// if_let_chain! {