filter() followed by next() instead of find()

ID

rust.maintainability.filter_next

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Inefficient Api Usage

Language

Rust

Description

Reports an iterator chain that filters with a predicate and then takes the first surviving item with next(), which is exactly what find() does in one step.

Rationale

The two-call form makes the reader assemble the intent from an adaptor plus a consumer before seeing that only the first match is wanted. find() states that directly, and it keeps the chain shorter so the predicate stays the thing that stands out.

The following code illustrates the pattern detected by this rule:

pub struct User {
    pub name: String,
    pub admin: bool,
}

pub fn first_admin(users: &[User]) -> Option<&User> {
    // FLAGGED: filter() followed by next() instead of find()
    users.iter().filter(|u| u.admin).next()
}

Remediation

Replace .filter(predicate).next() with .find(predicate). The predicate keeps the same signature, so the closure moves across unchanged.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved