rust-clippy
rust-clippy copied to clipboard
`nonminimal_bool` recommends calling function more than necessary
Summary
nonminimal_bool recommends calling a function more times than necessary.
Lint Name
nonminimal_bool
Reproducer
I tried this code (reduced from my actual code):
const BANNED_MODULE_SUFFIXES: &[&str] = &["/documentation", "/testcases"];
fn assertion(title: &str) -> bool {
!BANNED_MODULE_SUFFIXES
.iter()
.any(|suffix| title.ends_with(suffix))
&& !(title.starts_with("zh/data") || title.starts_with("User:"))
}
I saw this happen:
warning: this boolean expression can be simplified
--> src/main.rs:15:5
|
15 | / !BANNED_MODULE_SUFFIXES
16 | | .iter()
17 | | .any(|suffix| title.ends_with(suffix))
18 | | && !(title.starts_with("zh/data") || title.starts_with("User:"))
| |________________________________________________________________________^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#nonminimal_bool
= note: `#[warn(clippy::nonminimal_bool)]` on by default
help: try
|
15 ~ !(BANNED_MODULE_SUFFIXES
16 + .iter()
17 + .any(|suffix| title.ends_with(suffix)) || title.starts_with("zh/data") || title.starts_with("User:"))
This is a bad suggestion because it causes each instance of str::starts_with to be executed twice with the same arguments (once for each suffix in BANNED_MODULE_SUFFIXES). This is unnecessary and would cause the code to take a little more time, if no optimization passes somehow eliminate all but the first of each call.
Version
rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6
Additional Labels
No response
...causes each instance of
str::starts_withto be executed twice with the same arguments (once for eachsuffixinBANNED_MODULE_SUFFIXES)
What do you mean by this?