Rust

Rust

Made by DeepSource

Found unnecessary fold RS-W1022

Anti-pattern
Minor
Autofix

Certain forms of fold have concise alternatives, such as sum, product, any or all. Prefer these forms where applicable as they improve readability.

The following forms of fold can generally be simplified:

  • .fold(0, |acc, x| acc + x): use .sum()
  • .fold(1, |acc, x| acc * x): use .product()
  • .fold(false, |acc, x| acc || x): use .any(|x| x)
  • .fold(true, |acc, x| acc && x): use .all(|x| x)

Bad practice

let _ = (0..3).fold(false, |acc, x| acc || x > 2);
let _ = (0..3).fold(true, |acc, x| acc && x > 2);
let _: i32 = (0..3).fold(0, |acc, x| acc + x);
let _: i32 = (0..3).fold(1, |acc, x| acc * x);

Recommended

let _ = (0..3).any(|x| x > 2);
let _ = (0..3).all(|x| x > 2);
let _: i32 = (0..3).sum();
let _: i32 = (0..3).product();