Fix manual_find suggests wrongly when return type needs adjustment (#14892)
Closes rust-lang/rust-clippy#14826 changelog: [`manual_find`] fix wrong suggestions when return type needs adjustment
This commit is contained in:
commit
954034b497
4 changed files with 62 additions and 1 deletions
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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 _)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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…
Add table
Add a link
Reference in a new issue