Value allocated only to be compared
ID |
rust.performance.cmp_owned |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Inefficient Api Usage |
Language |
Rust |
Description
This comparison allocates an owned copy of the operand and then throws it away. PartialEq is implemented between borrowed and owned string types, so compare the borrowed values directly (candidate == expected, or owned.as_str() == borrowed) and drop the conversion.
Rationale
This comparison allocates an owned copy of the operand and then throws it away. PartialEq is implemented between borrowed and owned string types, so compare the borrowed values directly (candidate == expected, or owned.as_str() == borrowed) and drop the conversion.
The following code illustrates the pattern detected by this rule:
fn matches_name(candidate: &str, expected: &str) -> bool {
// FLAGGED: Value allocated only to be compared
candidate.to_string() == expected
}