Skip to content

Commit ea4c5c5

Browse files
committed
Auto merge of #11035 - y21:issue10729, r=Jarcho
[`option_if_let_else`]: suggest `.as_ref()` if scrutinee is of type `&Option<_>` Fixes #10729 `Option::map_or` takes ownership, so if matching on an `&Option<_>`, we need to suggest `.as_ref()` before calling `map_or` to get the same effect and to not cause a borrowck error. changelog: [`option_if_let_else`]: suggest `.as_ref()`/`.as_mut()` if scrutinee is of type `&Option<_>`/`&mut Option<_>`
2 parents 750c7a1 + cee4c41 commit ea4c5c5

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

clippy_lints/src/option_if_let_else.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ fn try_get_option_occurrence<'tcx>(
140140
let (as_ref, as_mut) = match &expr.kind {
141141
ExprKind::AddrOf(_, Mutability::Not, _) => (true, false),
142142
ExprKind::AddrOf(_, Mutability::Mut, _) => (false, true),
143+
_ if let Some(mutb) = cx.typeck_results().expr_ty(expr).ref_mutability() => {
144+
(mutb == Mutability::Not, mutb == Mutability::Mut)
145+
}
143146
_ => (bind_annotation == BindingAnnotation::REF, bind_annotation == BindingAnnotation::REF_MUT),
144147
};
145148

tests/ui/option_if_let_else.fixed

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,19 @@ fn issue9742() -> Option<&'static str> {
208208
_ => None,
209209
}
210210
}
211+
212+
mod issue10729 {
213+
#![allow(clippy::unit_arg, dead_code)]
214+
215+
pub fn reproduce(initial: &Option<String>) {
216+
// 👇 needs `.as_ref()` because initial is an `&Option<_>`
217+
initial.as_ref().map_or({}, |value| do_something(value))
218+
}
219+
220+
pub fn reproduce2(initial: &mut Option<String>) {
221+
initial.as_mut().map_or({}, |value| do_something2(value))
222+
}
223+
224+
fn do_something(_value: &str) {}
225+
fn do_something2(_value: &mut str) {}
226+
}

tests/ui/option_if_let_else.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,25 @@ fn issue9742() -> Option<&'static str> {
249249
_ => None,
250250
}
251251
}
252+
253+
mod issue10729 {
254+
#![allow(clippy::unit_arg, dead_code)]
255+
256+
pub fn reproduce(initial: &Option<String>) {
257+
// 👇 needs `.as_ref()` because initial is an `&Option<_>`
258+
match initial {
259+
Some(value) => do_something(value),
260+
None => {},
261+
}
262+
}
263+
264+
pub fn reproduce2(initial: &mut Option<String>) {
265+
match initial {
266+
Some(value) => do_something2(value),
267+
None => {},
268+
}
269+
}
270+
271+
fn do_something(_value: &str) {}
272+
fn do_something2(_value: &mut str) {}
273+
}

tests/ui/option_if_let_else.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,5 +271,23 @@ error: use Option::map_or instead of an if let/else
271271
LL | let _ = if let Ok(a) = res { a + 1 } else { 5 };
272272
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `res.map_or(5, |a| a + 1)`
273273

274-
error: aborting due to 21 previous errors
274+
error: use Option::map_or instead of an if let/else
275+
--> $DIR/option_if_let_else.rs:258:9
276+
|
277+
LL | / match initial {
278+
LL | | Some(value) => do_something(value),
279+
LL | | None => {},
280+
LL | | }
281+
| |_________^ help: try: `initial.as_ref().map_or({}, |value| do_something(value))`
282+
283+
error: use Option::map_or instead of an if let/else
284+
--> $DIR/option_if_let_else.rs:265:9
285+
|
286+
LL | / match initial {
287+
LL | | Some(value) => do_something2(value),
288+
LL | | None => {},
289+
LL | | }
290+
| |_________^ help: try: `initial.as_mut().map_or({}, |value| do_something2(value))`
291+
292+
error: aborting due to 23 previous errors
275293

0 commit comments

Comments
 (0)