Closed
Description
Summary
I created a function which essentially re-implements Iterator::find
, with different lifetimes for the iterated items and the target ('needle') item. I think the reproducer code explains it best.
clippy 0.1.87 (b48576b4db 2025-03-22)
cargo 1.87.0 (99624be96 2025-05-06)
rustc 1.87.0 (17067e9ac 2025-05-09)
Reproducer
Original code:
fn find2(needle: &str) -> Option<&'static str> {
for candidate in &["foo", "bar"] {
if candidate.eq_ignore_ascii_case(needle) {
return Some(candidate);
}
}
None
}
After cargo clippy --fix
(broken fix, caught before writing file, full output below):
fn find2(needle: &str) -> Option<&'static str> {
["foo", "bar"]
.iter()
.find(|&candidate| candidate.eq_ignore_ascii_case(needle))
}
After cargo clippy --fix --broken-code
(correct fix, no issues):
fn find2(needle: &str) -> Option<&'static str> {
["foo", "bar"]
.iter()
.find(|&candidate| candidate.eq_ignore_ascii_case(needle))
.map(|v| &**v)
}
If i remove the
&*
from the last line, clippy now warns me that I should be using.copied()
instead of.map(...)
, which looks like the best option out of everything.
Full output of cargo --fix
:
warning: manual implementation of `Iterator::find`
--> src/lib.rs:2:5
|
2 | / for candidate in &["foo", "bar"] {
3 | | if candidate.eq_ignore_ascii_case(needle) {
4 | | return Some(candidate);
... |
7 | | None
| |________^ help: replace with an iterator: `["foo", "bar"].iter().find(|&candidate| candidate.eq_ignore_ascii_case(needle))`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_find
= note: `#[warn(clippy::manual_find)]` on by default
warning: `sandbox` (lib) generated 2 warnings (run `cargo clippy --fix --lib -p sandbox` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `sandbox`
after fixes were automatically applied the compiler reported errors within these files:
* src/lib.rs
This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag
The following errors were reported:
error[E0308]: mismatched types
--> src/lib.rs:2:5
|
1 | fn find2(needle: &str) -> Option<&'static str> {
| -------------------- expected `std::option::Option<&'static str>` because of return type
2 | ["foo", "bar"].iter().find(|&candidate| candidate.eq_ignore_ascii_case(needle))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Option<&str>`, found `Option<&&str>`
|
= note: expected enum `std::option::Option<&'static _>`
found enum `std::option::Option<&&_>`
help: try using `.map(|v| &**v)` to convert `std::option::Option<&&str>` to `std::option::Option<&'static str>`
|
2 | ["foo", "bar"].iter().find(|&candidate| candidate.eq_ignore_ascii_case(needle)).map(|v| &**v)
| ++++++++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.
Version
rustc 1.87.0-nightly (b48576b4d 2025-03-22)
binary: rustc
commit-hash: b48576b4db5a595f453891f0b7243ef75d8c0afa
commit-date: 2025-03-22
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.1
Additional Labels
No response