2017-08-25 15:20:52 -05:00
|
|
|
use rustc::hir::*;
|
|
|
|
use rustc::lint::*;
|
2017-08-25 17:09:31 -05:00
|
|
|
use utils::{get_trait_def_id, implements_trait, higher, match_qpath, paths, span_lint};
|
2017-08-25 15:20:52 -05:00
|
|
|
|
|
|
|
/// **What it does:** Checks for iteration that is guaranteed to be infinite.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** While there may be places where this is acceptable
|
|
|
|
/// (e.g. in event streams), in most cases this is simply an error.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// repeat(1_u8).iter().collect::<Vec<_>>()
|
|
|
|
/// ```
|
|
|
|
declare_lint! {
|
|
|
|
pub INFINITE_ITER,
|
|
|
|
Warn,
|
|
|
|
"infinite iteration"
|
|
|
|
}
|
|
|
|
|
|
|
|
/// **What it does:** Checks for iteration that may be infinite.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** While there may be places where this is acceptable
|
|
|
|
/// (e.g. in event streams), in most cases this is simply an error.
|
|
|
|
///
|
|
|
|
/// **Known problems:** The code may have a condition to stop iteration, but
|
|
|
|
/// this lint is not clever enough to analyze it.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// [0..].iter().zip(infinite_iter.take_while(|x| x > 5))
|
|
|
|
/// ```
|
|
|
|
declare_lint! {
|
|
|
|
pub MAYBE_INFINITE_ITER,
|
|
|
|
Allow,
|
|
|
|
"possible infinite iteration"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct Pass;
|
|
|
|
|
|
|
|
impl LintPass for Pass {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(INFINITE_ITER, MAYBE_INFINITE_ITER)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
|
|
|
let (lint, msg) = match complete_infinite_iter(cx, expr) {
|
2017-08-26 11:46:42 -05:00
|
|
|
Infinite => (INFINITE_ITER, "infinite iteration detected"),
|
2017-09-03 16:15:15 -05:00
|
|
|
MaybeInfinite => (MAYBE_INFINITE_ITER, "possible infinite iteration detected"),
|
|
|
|
Finite => {
|
|
|
|
return;
|
|
|
|
},
|
2017-08-25 15:20:52 -05:00
|
|
|
};
|
|
|
|
span_lint(cx, lint, expr.span, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2017-08-26 11:46:42 -05:00
|
|
|
enum Finiteness {
|
|
|
|
Infinite,
|
|
|
|
MaybeInfinite,
|
2017-09-03 16:15:15 -05:00
|
|
|
Finite,
|
2017-08-25 15:20:52 -05:00
|
|
|
}
|
|
|
|
|
2017-08-26 11:46:42 -05:00
|
|
|
use self::Finiteness::{Infinite, MaybeInfinite, Finite};
|
2017-08-25 15:20:52 -05:00
|
|
|
|
2017-08-26 11:46:42 -05:00
|
|
|
impl Finiteness {
|
2017-08-25 15:20:52 -05:00
|
|
|
fn and(self, b: Self) -> Self {
|
|
|
|
match (self, b) {
|
2017-08-26 11:46:42 -05:00
|
|
|
(Finite, _) | (_, Finite) => Finite,
|
2017-09-03 16:15:15 -05:00
|
|
|
(MaybeInfinite, _) |
|
|
|
|
(_, MaybeInfinite) => MaybeInfinite,
|
|
|
|
_ => Infinite,
|
2017-08-25 15:20:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn or(self, b: Self) -> Self {
|
|
|
|
match (self, b) {
|
2017-08-26 11:46:42 -05:00
|
|
|
(Infinite, _) | (_, Infinite) => Infinite,
|
2017-09-03 16:15:15 -05:00
|
|
|
(MaybeInfinite, _) |
|
|
|
|
(_, MaybeInfinite) => MaybeInfinite,
|
|
|
|
_ => Finite,
|
2017-08-25 15:20:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-26 11:46:42 -05:00
|
|
|
impl From<bool> for Finiteness {
|
2017-08-25 15:20:52 -05:00
|
|
|
fn from(b: bool) -> Self {
|
2017-08-26 11:46:42 -05:00
|
|
|
if b { Infinite } else { Finite }
|
2017-08-25 15:20:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-26 11:46:42 -05:00
|
|
|
/// This tells us what to look for to know if the iterator returned by
|
|
|
|
/// this method is infinite
|
2017-08-25 15:20:52 -05:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum Heuristic {
|
2017-08-26 11:46:42 -05:00
|
|
|
/// infinite no matter what
|
2017-08-25 15:20:52 -05:00
|
|
|
Always,
|
2017-08-26 11:46:42 -05:00
|
|
|
/// infinite if the first argument is
|
2017-08-25 15:20:52 -05:00
|
|
|
First,
|
2017-08-26 11:46:42 -05:00
|
|
|
/// infinite if any of the supplied arguments is
|
2017-08-25 15:20:52 -05:00
|
|
|
Any,
|
2017-08-26 11:46:42 -05:00
|
|
|
/// infinite if all of the supplied arguments are
|
2017-09-03 16:15:15 -05:00
|
|
|
All,
|
2017-08-25 15:20:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
use self::Heuristic::{Always, First, Any, All};
|
|
|
|
|
2017-08-26 11:46:42 -05:00
|
|
|
/// a slice of (method name, number of args, heuristic, bounds) tuples
|
|
|
|
/// that will be used to determine whether the method in question
|
|
|
|
/// returns an infinite or possibly infinite iterator. The finiteness
|
|
|
|
/// is an upper bound, e.g. some methods can return a possibly
|
|
|
|
/// infinite iterator at worst, e.g. `take_while`.
|
2017-09-03 16:15:15 -05:00
|
|
|
static HEURISTICS: &[(&str, usize, Heuristic, Finiteness)] = &[
|
2017-08-26 11:46:42 -05:00
|
|
|
("zip", 2, All, Infinite),
|
|
|
|
("chain", 2, Any, Infinite),
|
|
|
|
("cycle", 1, Always, Infinite),
|
|
|
|
("map", 2, First, Infinite),
|
|
|
|
("by_ref", 1, First, Infinite),
|
|
|
|
("cloned", 1, First, Infinite),
|
|
|
|
("rev", 1, First, Infinite),
|
|
|
|
("inspect", 1, First, Infinite),
|
|
|
|
("enumerate", 1, First, Infinite),
|
|
|
|
("peekable", 2, First, Infinite),
|
|
|
|
("fuse", 1, First, Infinite),
|
|
|
|
("skip", 2, First, Infinite),
|
|
|
|
("skip_while", 1, First, Infinite),
|
|
|
|
("filter", 2, First, Infinite),
|
|
|
|
("filter_map", 2, First, Infinite),
|
|
|
|
("flat_map", 2, First, Infinite),
|
|
|
|
("unzip", 1, First, Infinite),
|
|
|
|
("take_while", 2, First, MaybeInfinite),
|
2017-09-03 16:15:15 -05:00
|
|
|
("scan", 3, First, MaybeInfinite),
|
2017-08-25 15:20:52 -05:00
|
|
|
];
|
|
|
|
|
2017-08-26 11:46:42 -05:00
|
|
|
fn is_infinite(cx: &LateContext, expr: &Expr) -> Finiteness {
|
2017-08-25 15:20:52 -05:00
|
|
|
match expr.node {
|
|
|
|
ExprMethodCall(ref method, _, ref args) => {
|
|
|
|
for &(name, len, heuristic, cap) in HEURISTICS.iter() {
|
|
|
|
if method.name == name && args.len() == len {
|
|
|
|
return (match heuristic {
|
2017-09-03 16:15:15 -05:00
|
|
|
Always => Infinite,
|
|
|
|
First => is_infinite(cx, &args[0]),
|
|
|
|
Any => is_infinite(cx, &args[0]).or(is_infinite(cx, &args[1])),
|
|
|
|
All => is_infinite(cx, &args[0]).and(is_infinite(cx, &args[1])),
|
|
|
|
}).and(cap);
|
2017-08-25 15:20:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if method.name == "flat_map" && args.len() == 2 {
|
2017-08-30 03:54:24 -05:00
|
|
|
if let ExprClosure(_, _, body_id, _, _) = args[1].node {
|
2017-08-25 15:20:52 -05:00
|
|
|
let body = cx.tcx.hir.body(body_id);
|
|
|
|
return is_infinite(cx, &body.value);
|
|
|
|
}
|
|
|
|
}
|
2017-08-26 11:46:42 -05:00
|
|
|
Finite
|
2017-08-25 15:20:52 -05:00
|
|
|
},
|
2017-09-03 16:15:15 -05:00
|
|
|
ExprBlock(ref block) => block.expr.as_ref().map_or(Finite, |e| is_infinite(cx, e)),
|
|
|
|
ExprBox(ref e) |
|
|
|
|
ExprAddrOf(_, ref e) => is_infinite(cx, e),
|
2017-08-25 15:20:52 -05:00
|
|
|
ExprCall(ref path, _) => {
|
|
|
|
if let ExprPath(ref qpath) = path.node {
|
2017-08-25 17:09:31 -05:00
|
|
|
match_qpath(qpath, &paths::REPEAT).into()
|
2017-09-03 16:15:15 -05:00
|
|
|
} else {
|
|
|
|
Finite
|
|
|
|
}
|
2017-08-25 15:20:52 -05:00
|
|
|
},
|
|
|
|
ExprStruct(..) => {
|
2017-09-03 16:15:15 -05:00
|
|
|
higher::range(expr)
|
|
|
|
.map_or(false, |r| r.end.is_none())
|
|
|
|
.into()
|
2017-08-25 15:20:52 -05:00
|
|
|
},
|
2017-09-03 16:15:15 -05:00
|
|
|
_ => Finite,
|
2017-08-25 15:20:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-26 11:46:42 -05:00
|
|
|
/// the names and argument lengths of methods that *may* exhaust their
|
|
|
|
/// iterators
|
2017-09-03 16:15:15 -05:00
|
|
|
static POSSIBLY_COMPLETING_METHODS: &[(&str, usize)] = &[
|
2017-08-25 15:20:52 -05:00
|
|
|
("find", 2),
|
|
|
|
("rfind", 2),
|
|
|
|
("position", 2),
|
|
|
|
("rposition", 2),
|
|
|
|
("any", 2),
|
2017-09-03 16:15:15 -05:00
|
|
|
("all", 2),
|
2017-08-25 15:20:52 -05:00
|
|
|
];
|
|
|
|
|
2017-08-26 11:46:42 -05:00
|
|
|
/// the names and argument lengths of methods that *always* exhaust
|
|
|
|
/// their iterators
|
2017-09-03 16:15:15 -05:00
|
|
|
static COMPLETING_METHODS: &[(&str, usize)] = &[
|
2017-08-25 15:20:52 -05:00
|
|
|
("count", 1),
|
|
|
|
("collect", 1),
|
|
|
|
("fold", 3),
|
|
|
|
("for_each", 2),
|
|
|
|
("partition", 2),
|
|
|
|
("max", 1),
|
|
|
|
("max_by", 2),
|
|
|
|
("max_by_key", 2),
|
|
|
|
("min", 1),
|
|
|
|
("min_by", 2),
|
|
|
|
("min_by_key", 2),
|
|
|
|
("sum", 1),
|
2017-09-03 16:15:15 -05:00
|
|
|
("product", 1),
|
2017-08-25 15:20:52 -05:00
|
|
|
];
|
|
|
|
|
2017-08-26 11:46:42 -05:00
|
|
|
fn complete_infinite_iter(cx: &LateContext, expr: &Expr) -> Finiteness {
|
2017-08-25 15:20:52 -05:00
|
|
|
match expr.node {
|
|
|
|
ExprMethodCall(ref method, _, ref args) => {
|
|
|
|
for &(name, len) in COMPLETING_METHODS.iter() {
|
|
|
|
if method.name == name && args.len() == len {
|
|
|
|
return is_infinite(cx, &args[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for &(name, len) in POSSIBLY_COMPLETING_METHODS.iter() {
|
|
|
|
if method.name == name && args.len() == len {
|
2017-08-26 11:46:42 -05:00
|
|
|
return MaybeInfinite.and(is_infinite(cx, &args[0]));
|
2017-08-25 15:20:52 -05:00
|
|
|
}
|
|
|
|
}
|
2017-09-03 16:58:27 -05:00
|
|
|
if method.name == "last" && args.len() == 1 {
|
|
|
|
let not_double_ended = get_trait_def_id(cx,
|
|
|
|
&paths::DOUBLE_ENDED_ITERATOR)
|
|
|
|
.map_or(false, |id| {
|
2017-09-03 16:15:15 -05:00
|
|
|
!implements_trait(cx, cx.tables.expr_ty(&args[0]), id, &[])
|
2017-09-03 16:58:27 -05:00
|
|
|
});
|
|
|
|
if not_double_ended {
|
|
|
|
return is_infinite(cx, &args[0]);
|
|
|
|
}
|
2017-08-25 15:20:52 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
ExprBinary(op, ref l, ref r) => {
|
|
|
|
if op.node.is_comparison() {
|
2017-09-03 16:15:15 -05:00
|
|
|
return is_infinite(cx, l).and(is_infinite(cx, r)).and(
|
|
|
|
MaybeInfinite,
|
|
|
|
);
|
2017-08-25 15:20:52 -05:00
|
|
|
}
|
|
|
|
}, //TODO: ExprLoop + Match
|
2017-09-03 16:15:15 -05:00
|
|
|
_ => (),
|
2017-08-25 15:20:52 -05:00
|
|
|
}
|
2017-08-26 11:46:42 -05:00
|
|
|
Finite
|
2017-08-25 15:20:52 -05:00
|
|
|
}
|