2015-08-15 16:55:25 +00:00
|
|
|
use rustc::lint::{Context, LintArray, LintPass};
|
2015-09-03 14:42:17 +00:00
|
|
|
use rustc_front::hir::*;
|
2015-08-15 16:55:25 +00:00
|
|
|
use syntax::codemap::Spanned;
|
2015-08-21 17:00:33 +00:00
|
|
|
use utils::match_type;
|
2015-08-15 16:55:25 +00:00
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub RANGE_STEP_BY_ZERO, Warn,
|
|
|
|
"using Range::step_by(0), which produces an infinite iterator"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy,Clone)]
|
|
|
|
pub struct StepByZero;
|
|
|
|
|
|
|
|
impl LintPass for StepByZero {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(RANGE_STEP_BY_ZERO)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &Context, expr: &Expr) {
|
|
|
|
if let ExprMethodCall(Spanned { node: ref ident, .. }, _,
|
|
|
|
ref args) = expr.node {
|
|
|
|
// Only warn on literal ranges.
|
2015-08-23 14:34:23 +00:00
|
|
|
if ident.name == "step_by" && args.len() == 2 &&
|
2015-08-15 16:55:25 +00:00
|
|
|
is_range(cx, &args[0]) && is_lit_zero(&args[1]) {
|
|
|
|
cx.span_lint(RANGE_STEP_BY_ZERO, expr.span,
|
|
|
|
"Range::step_by(0) produces an infinite iterator. \
|
|
|
|
Consider using `std::iter::repeat()` instead")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_range(cx: &Context, expr: &Expr) -> bool {
|
|
|
|
// No need for walk_ptrs_ty here because step_by moves self, so it
|
|
|
|
// can't be called on a borrowed range.
|
2015-08-21 17:00:33 +00:00
|
|
|
let ty = cx.tcx.expr_ty(expr);
|
|
|
|
// Note: RangeTo and RangeFull don't have step_by
|
|
|
|
match_type(cx, ty, &["core", "ops", "Range"]) || match_type(cx, ty, &["core", "ops", "RangeFrom"])
|
2015-08-15 16:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|