Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty iterator error #100751

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/lint.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ lint_enum_intrinsics_mem_variant =
the return value of `mem::variant_count` is unspecified when called with a non-enum type
.note = the type parameter of `variant_count` should be an enum, but it was instantiated with the type `{$ty_param}`, which is not an enum.

lint_empty_iter_ranges = this `for` loop is never run
.clarification = this loop is never run because it's iterating over an empty iterator
.note = ranges that have a bigger start than their end will produce an empty iterator
.help = if you want a decreasing range sequence, create an increasing range and call `.rev()` on it

lint_expectation = this lint expectation is unfulfilled
.note = the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message

Expand Down
81 changes: 81 additions & 0 deletions compiler/rustc_lint/src/empty_iterator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::{LateContext, LateLintPass, LintContext};

use rustc_ast::ast::LitKind;
use rustc_errors::fluent;
use rustc_hir::{Expr, ExprField, ExprKind, MatchSource, PatKind, StmtKind};
use rustc_span::Span;

declare_lint! {
/// The `empty_iterator_range` lint checks for empty iterator ranges.
///
/// ### Example
///
/// ```rust
/// for i in 10..0 { /* ... */ }
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// It is usually a mistake to have a statement that has no effect.
pub EMPTY_ITERATOR_RANGE,
Warn,
"empty iterator range"
}

declare_lint_pass!(EmptyIteratorRange => [EMPTY_ITERATOR_RANGE]);

impl<'tcx> LateLintPass<'tcx> for EmptyIteratorRange {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
let Some(for_expr) = extract_for_loop(expr) else { return };
let span = expr.span.with_hi(for_expr.span.hi());
if let ExprKind::Struct(_, expr_fields, _) = &for_expr.kind {
is_empty_range(cx, span, expr_fields);
} else if let ExprKind::MethodCall(_, expr, _, _) = &for_expr.kind {
if let ExprKind::Struct(_, expr_fields, _) = &expr.kind {
is_empty_range(cx, span, expr_fields);
}
}
}
}

fn extract_for_loop<'tcx>(expr: &Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
if let ExprKind::DropTemps(e) = expr.kind
&& let ExprKind::Match(iterexpr, [arm], MatchSource::ForLoopDesugar) = e.kind
&& let ExprKind::Call(_, [arg]) = iterexpr.kind
&& let ExprKind::Loop(block, ..) = arm.body.kind
&& let [stmt] = block.stmts
&& let StmtKind::Expr(e) = stmt.kind
&& let ExprKind::Match(_, [_, some_arm], _) = e.kind
&& let PatKind::Struct(..) = some_arm.pat.kind
{
Some(arg)
} else {
None
}
}

fn is_empty_range(cx: &LateContext<'_>, span: Span, expr_fields: &[ExprField<'_>]) {
let mut prev = 0u128;
for (index, expr_field) in expr_fields.iter().enumerate() {
if let ExprKind::Lit(lit) = &expr_field.expr.kind {
if let LitKind::Int(u, _) = lit.node {
if index == 0 {
prev = u;
} else if prev > u {
cx.struct_span_lint(
EMPTY_ITERATOR_RANGE,
span,
fluent::lint::empty_iter_ranges,
|lint| {
lint.span_label(span, fluent::lint::clarification)
.note(fluent::lint::note)
.help(fluent::lint::help)
},
);
}
}
}
}
}
3 changes: 3 additions & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod array_into_iter;
pub mod builtin;
mod context;
mod early;
mod empty_iterator;
mod enum_intrinsics_non_enums;
mod errors;
mod expect;
Expand Down Expand Up @@ -85,6 +86,7 @@ use rustc_span::Span;

use array_into_iter::ArrayIntoIter;
use builtin::*;
use empty_iterator::EmptyIteratorRange;
use enum_intrinsics_non_enums::EnumIntrinsicsNonEnums;
use hidden_unicode_codepoints::*;
use internal::*;
Expand Down Expand Up @@ -226,6 +228,7 @@ macro_rules! late_lint_mod_passes {
InvalidAtomicOrdering: InvalidAtomicOrdering,
NamedAsmLabels: NamedAsmLabels,
OpaqueHiddenInferredBound: OpaqueHiddenInferredBound,
EmptyIteratorRange: EmptyIteratorRange,
]
);
};
Expand Down
2 changes: 2 additions & 0 deletions library/core/tests/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ fn test_range_inclusive_size_hint() {

#[test]
fn test_double_ended_range() {
#![cfg_attr(not(bootstrap), allow(empty_iterator_range))]

assert_eq!((11..14).rev().collect::<Vec<_>>(), [13, 12, 11]);
for _ in (10..0).rev() {
panic!("unreachable");
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/lint/empty-iterator-range.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![deny(empty_iterator_range)]

fn main() {
for _i in 10..0 {
//~^ ERROR this `for` loop is never run [empty_iterator_range]
}
for _i in (10..0).rev() {
//~^ ERROR this `for` loop is never run [empty_iterator_range]
}
for _i in (10..0).step_by(1) {
//~^ ERROR this `for` loop is never run [empty_iterator_range]
}
}
34 changes: 34 additions & 0 deletions src/test/ui/lint/empty-iterator-range.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
error: this `for` loop is never run
--> $DIR/empty-iterator-range.rs:4:5
|
LL | for _i in 10..0 {
| ^^^^^^^^^^^^^^^ this loop is never run because it's iterating over an empty iterator
|
= note: ranges that have a bigger start than their end will produce an empty iterator
= help: if you want a decreasing range sequence, create an increasing range and call `.rev()` on it
note: the lint level is defined here
--> $DIR/empty-iterator-range.rs:1:9
|
LL | #![deny(empty_iterator_range)]
| ^^^^^^^^^^^^^^^^^^^^

error: this `for` loop is never run
--> $DIR/empty-iterator-range.rs:7:5
|
LL | for _i in (10..0).rev() {
| ^^^^^^^^^^^^^^^^^^^^^^^ this loop is never run because it's iterating over an empty iterator
|
= note: ranges that have a bigger start than their end will produce an empty iterator
= help: if you want a decreasing range sequence, create an increasing range and call `.rev()` on it

error: this `for` loop is never run
--> $DIR/empty-iterator-range.rs:10:5
|
LL | for _i in (10..0).step_by(1) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this loop is never run because it's iterating over an empty iterator
|
= note: ranges that have a bigger start than their end will produce an empty iterator
= help: if you want a decreasing range sequence, create an increasing range and call `.rev()` on it

error: aborting due to 3 previous errors