unnecessary_fold: suggest bool bitwise ops
Author: vilgotfCreated Sep 26, 2026Updated Sep 30, 2026
Labelsgood first issueC-enhancementD-confusing
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 defaultDesired 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.1Additional Labels
No response
Source: rust-lang/rust-clippy