rust/clippy_lints/src/methods/iterator_step_by_zero.rs

22 lines
668 B
Rust
Raw Normal View History

use crate::consts::{constant, Constant};
use clippy_utils::diagnostics::span_lint;
2021-03-16 11:06:34 -05:00
use clippy_utils::is_trait_method;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_span::sym;
use super::ITERATOR_STEP_BY_ZERO;
2021-03-10 23:40:20 -06:00
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, arg: &'tcx hir::Expr<'_>) {
if is_trait_method(cx, expr, sym::Iterator) {
2021-03-10 23:40:20 -06:00
if let Some((Constant::Int(0), _)) = constant(cx, cx.typeck_results(), arg) {
span_lint(
cx,
ITERATOR_STEP_BY_ZERO,
expr.span,
"`Iterator::step_by(0)` will panic at runtime",
);
}
}
}