Skip to content

unnecessary_fold: suggest bool bitwise ops #17792

Description

@vilgotf

Summary

The unnecessary_fold lint suggests using any over, e.g., fold(false, |acc, x| acc || x) because the former short-circuits. But whether or not short-circuiting is wanted depends, for example, iterators or fold closures may have side-effects. The previous example is therefore ambiguous because it uses the short-circuiting || op. It ought explicitly either short-circuit via any or use the non-short-circuiting | op.

Alternative solution to the #3351 issue

Reproducer

Code:

/// Broadcasts all previously recorded events to their respective listeners.
///
/// Returns `true` if an event was delivered to at least one listener.
fn broadcast(&self) -> bool {
    self.storage
        .iter()
        // Any signal of this kind arrived since we checked last?
        .filter(|event_info| event_info.pending.swap(false, Ordering::SeqCst))
        // Ignore errors if there are no listeners
        .map(|event_info| event_info.tx.send(()).is_ok())
        .fold(false, |acc, x| acc || x)
}

Current output:

warning: this `.fold` can be written more succinctly using another method
  --> tokio/src/signal/registry.rs:91:14
   |
91 |             .fold(false, |acc, x| acc || x)
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `any(|x| x)`
   |
   = note: the `any` method is short circuiting and may change the program semantics if the iterator has side effects
   = help: for further information visit https://rust-lang.github.io/rust-clippy/main/index.html#unnecessary_fold
   = note: `#[warn(clippy::unnecessary_fold)]` on by default

Desired output:

Suggest .fold(false, |acc, x| acc | x) as the explicit non-short-circuiting method.

Version

rustc 1.100.0-nightly (5ceaf6608 2026-09-25)
binary: rustc
commit-hash: 5ceaf6608eb354c2f5bbb3b8d974caa367dac81c
commit-date: 2026-09-25
host: aarch64-apple-darwin
release: 1.100.0-nightly
LLVM version: 23.1.1

Additional Labels

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesD-confusingDiagnostics: Confusing error or lint that should be reworked.good first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions