Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New lint: Range::step_by(0) (fixes #95)
Uses type information so it can detect non-literal ranges as well (Range or RangeFrom -- the other range types don't have step_by).
- Loading branch information
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use rustc::lint::{Context, LintArray, LintPass}; | ||
use rustc::middle::ty::TypeVariants::TyStruct; | ||
use syntax::ast::*; | ||
use syntax::codemap::Spanned; | ||
use utils::{match_def_path, walk_ptrs_ty}; | ||
|
||
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. | ||
if ident.name.as_str() == "step_by" && args.len() == 2 && | ||
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. | ||
if let TyStruct(did, _) = cx.tcx.expr_ty(expr).sty { | ||
// Note: RangeTo and RangeFull don't have step_by | ||
match_def_path(cx, did.did, &["core", "ops", "Range"]) || | ||
match_def_path(cx, did.did, &["core", "ops", "RangeFrom"]) | ||
} else { false } | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#![feature(step_by)] | ||
#![feature(plugin)] | ||
#![plugin(clippy)] | ||
|
||
struct NotARange; | ||
impl NotARange { | ||
fn step_by(&self, _: u32) {} | ||
} | ||
|
||
#[deny(range_step_by_zero)] | ||
fn main() { | ||
(0..1).step_by(0); //~ERROR Range::step_by(0) produces an infinite iterator | ||
// No warning for non-zero step | ||
(0..1).step_by(1); | ||
|
||
(1..).step_by(0); //~ERROR Range::step_by(0) produces an infinite iterator | ||
|
||
let x = 0..1; | ||
x.step_by(0); //~ERROR Range::step_by(0) produces an infinite iterator | ||
|
||
// No error, not a range. | ||
let y = NotARange; | ||
y.step_by(0); | ||
} |