Search adaptor whose result is only tested for presence
ID |
rust.maintainability.search_is_some |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Inefficient Api Usage |
Language |
Rust |
Description
Reports find, find_map, position or rposition whose returned Option is immediately discarded by is_some() or is_none(), so the search result itself is never used.
Rationale
Producing a value and then asking only whether it exists forces the reader to check that the item really is unused before concluding the line is a membership test. any() says that in one call, and for find_map it also avoids building the mapped value that is thrown away.
The following code illustrates the pattern detected by this rule:
pub struct User {
pub name: String,
pub admin: bool,
}
pub fn has_admin(users: &[User]) -> bool {
// FLAGGED: Search adaptor whose result is only tested for presence
users.iter().find(|u| u.admin).is_some()
}