Skip to content

Fix manual_find suggests wrongly when return type needs adjustment #14892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions clippy_lints/src/loops/manual_find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ pub(super) fn check<'tcx>(
)[..],
);
}

// If the return type requires adjustments, we need to add a `.map` after the iterator
let inner_ret_adjust = cx.typeck_results().expr_adjustments(inner_ret);
if !inner_ret_adjust.is_empty() {
snippet.push_str(".map(|v| v as _)");
}

// Extends to `last_stmt` to include semicolon in case of `return None;`
let lint_span = span.to(last_stmt.span).to(last_ret.span);
span_lint_and_then(
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/manual_find_fixable.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,13 @@ fn two_bindings(v: Vec<(u8, u8)>) -> Option<u8> {
}

fn main() {}

mod issue14826 {
fn adjust_fixable(needle: &str) -> Option<&'static str> {
["foo", "bar"].iter().find(|&candidate| candidate.eq_ignore_ascii_case(needle)).map(|v| v as _)
}

fn adjust_unfixable(needle: &str) -> Option<*const str> {
["foo", "bar"].iter().find(|&&candidate| candidate.eq_ignore_ascii_case(needle)).copied().map(|v| v as _)
}
}
22 changes: 22 additions & 0 deletions tests/ui/manual_find_fixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,25 @@ fn two_bindings(v: Vec<(u8, u8)>) -> Option<u8> {
}

fn main() {}

mod issue14826 {
fn adjust_fixable(needle: &str) -> Option<&'static str> {
for candidate in &["foo", "bar"] {
//~^ manual_find
if candidate.eq_ignore_ascii_case(needle) {
return Some(candidate);
}
}
None
}

fn adjust_unfixable(needle: &str) -> Option<*const str> {
for &candidate in &["foo", "bar"] {
//~^ manual_find
if candidate.eq_ignore_ascii_case(needle) {
return Some(candidate);
}
}
None
}
}
24 changes: 23 additions & 1 deletion tests/ui/manual_find_fixable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,27 @@ LL | | return Some(x);
LL | | None
| |____________^ help: replace with an iterator: `arr.into_iter().find(|&x| x < 1)`

error: aborting due to 12 previous errors
error: manual implementation of `Iterator::find`
--> tests/ui/manual_find_fixable.rs:257:9
|
LL | / for candidate in &["foo", "bar"] {
LL | |
LL | | if candidate.eq_ignore_ascii_case(needle) {
LL | | return Some(candidate);
... |
LL | | None
| |____________^ help: replace with an iterator: `["foo", "bar"].iter().find(|&candidate| candidate.eq_ignore_ascii_case(needle)).map(|v| v as _)`

error: manual implementation of `Iterator::find`
--> tests/ui/manual_find_fixable.rs:267:9
|
LL | / for &candidate in &["foo", "bar"] {
LL | |
LL | | if candidate.eq_ignore_ascii_case(needle) {
LL | | return Some(candidate);
... |
LL | | None
| |____________^ help: replace with an iterator: `["foo", "bar"].iter().find(|&&candidate| candidate.eq_ignore_ascii_case(needle)).copied().map(|v| v as _)`

error: aborting due to 14 previous errors

Loading