Do not remove method call if type is adjusted
Propose to replace `x.map_or(false, |y| y == z)` by `x == Some(z)` only if `x` is not adjusted. Otherwise, the type of `x` in the comparaison may not be the expected one, as it may be the product of an auto-deref.
This commit is contained in:
parent
b57bf6b64d
commit
c69905a995
4 changed files with 47 additions and 2 deletions
|
|
@ -62,7 +62,8 @@ pub(super) fn check<'a>(
|
|||
|
||||
let ext_def_span = def.span.until(map.span);
|
||||
|
||||
let (sugg, method, applicability) = if let ExprKind::Closure(map_closure) = map.kind
|
||||
let (sugg, method, applicability) = if cx.typeck_results().expr_adjustments(recv).is_empty()
|
||||
&& let ExprKind::Closure(map_closure) = map.kind
|
||||
&& let closure_body = cx.tcx.hir_body(map_closure.body)
|
||||
&& let closure_body_value = closure_body.value.peel_blocks()
|
||||
&& let ExprKind::Binary(op, l, r) = closure_body_value.kind
|
||||
|
|
|
|||
|
|
@ -130,3 +130,13 @@ fn issue14201(a: Option<String>, b: Option<String>, s: &String) -> bool {
|
|||
//~^ unnecessary_map_or
|
||||
x && y
|
||||
}
|
||||
|
||||
fn issue15180() {
|
||||
let s = std::sync::Mutex::new(Some("foo"));
|
||||
_ = s.lock().unwrap().is_some_and(|s| s == "foo");
|
||||
//~^ unnecessary_map_or
|
||||
|
||||
let s = &&&&Some("foo");
|
||||
_ = s.is_some_and(|s| s == "foo");
|
||||
//~^ unnecessary_map_or
|
||||
}
|
||||
|
|
|
|||
|
|
@ -134,3 +134,13 @@ fn issue14201(a: Option<String>, b: Option<String>, s: &String) -> bool {
|
|||
//~^ unnecessary_map_or
|
||||
x && y
|
||||
}
|
||||
|
||||
fn issue15180() {
|
||||
let s = std::sync::Mutex::new(Some("foo"));
|
||||
_ = s.lock().unwrap().map_or(false, |s| s == "foo");
|
||||
//~^ unnecessary_map_or
|
||||
|
||||
let s = &&&&Some("foo");
|
||||
_ = s.map_or(false, |s| s == "foo");
|
||||
//~^ unnecessary_map_or
|
||||
}
|
||||
|
|
|
|||
|
|
@ -326,5 +326,29 @@ LL - let y = b.map_or(true, |b| b == *s);
|
|||
LL + let y = b.is_none_or(|b| b == *s);
|
||||
|
|
||||
|
||||
error: aborting due to 26 previous errors
|
||||
error: this `map_or` can be simplified
|
||||
--> tests/ui/unnecessary_map_or.rs:140:9
|
||||
|
|
||||
LL | _ = s.lock().unwrap().map_or(false, |s| s == "foo");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: use is_some_and instead
|
||||
|
|
||||
LL - _ = s.lock().unwrap().map_or(false, |s| s == "foo");
|
||||
LL + _ = s.lock().unwrap().is_some_and(|s| s == "foo");
|
||||
|
|
||||
|
||||
error: this `map_or` can be simplified
|
||||
--> tests/ui/unnecessary_map_or.rs:144:9
|
||||
|
|
||||
LL | _ = s.map_or(false, |s| s == "foo");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: use is_some_and instead
|
||||
|
|
||||
LL - _ = s.map_or(false, |s| s == "foo");
|
||||
LL + _ = s.is_some_and(|s| s == "foo");
|
||||
|
|
||||
|
||||
error: aborting due to 28 previous errors
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue