Skip to content

fix option_if_let_else when Err variant is ignored #14429

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion clippy_lints/src/option_if_let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn try_get_option_occurrence<'tcx>(
let mut app = Applicability::Unspecified;

let (none_body, is_argless_call) = match none_body.kind {
ExprKind::Call(call_expr, []) if !none_body.span.from_expansion() => (call_expr, true),
ExprKind::Call(call_expr, []) if !none_body.span.from_expansion() && !is_result => (call_expr, true),
_ => (none_body, false),
};

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/option_if_let_else.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ fn complex_subpat() -> DummyEnum {
DummyEnum::Two
}

// #10335
pub fn test_result_err_ignored_1(r: Result<&[u8], &[u8]>) -> Vec<u8> {
r.map_or_else(|_| Vec::new(), |s| s.to_owned())
}

// #10335
pub fn test_result_err_ignored_2(r: Result<&[u8], &[u8]>) -> Vec<u8> {
r.map_or_else(|_| Vec::new(), |s| s.to_owned())
}

fn main() {
let optional = Some(5);
let _ = optional.map_or(5, |x| x + 2);
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/option_if_let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ fn complex_subpat() -> DummyEnum {
DummyEnum::Two
}

// #10335
pub fn test_result_err_ignored_1(r: Result<&[u8], &[u8]>) -> Vec<u8> {
match r {
//~^ option_if_let_else
Ok(s) => s.to_owned(),
Err(_) => Vec::new(),
}
}

// #10335
pub fn test_result_err_ignored_2(r: Result<&[u8], &[u8]>) -> Vec<u8> {
if let Ok(s) = r { s.to_owned() }
//~^ option_if_let_else
else { Vec::new() }
}

fn main() {
let optional = Some(5);
let _ = if let Some(x) = optional { x + 2 } else { 5 };
Expand Down
46 changes: 32 additions & 14 deletions tests/ui/option_if_let_else.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,32 @@ LL + true
LL + })
|

error: use Option::map_or_else instead of an if let/else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: this lint says Option, and has Option in its name, we should probably not be linting here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we already lint on Result here.

Tbh I'm not convinced of the value of this lint overall, we should probably have a team discussion about it, but separately to that we shoudl figure out if it should also handle Result in the same lint (and if so, it needs to be updated)

--> tests/ui/option_if_let_else.rs:157:5
|
LL | / match r {
LL | |
LL | | Ok(s) => s.to_owned(),
LL | | Err(_) => Vec::new(),
LL | | }
| |_____^ help: try: `r.map_or_else(|_| Vec::new(), |s| s.to_owned())`

error: use Option::map_or_else instead of an if let/else
--> tests/ui/option_if_let_else.rs:166:5
|
LL | / if let Ok(s) = r { s.to_owned() }
LL | |
LL | | else { Vec::new() }
| |_______________________^ help: try: `r.map_or_else(|_| Vec::new(), |s| s.to_owned())`

error: use Option::map_or instead of an if let/else
--> tests/ui/option_if_let_else.rs:157:13
--> tests/ui/option_if_let_else.rs:173:13
|
LL | let _ = if let Some(x) = optional { x + 2 } else { 5 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `optional.map_or(5, |x| x + 2)`

error: use Option::map_or instead of an if let/else
--> tests/ui/option_if_let_else.rs:168:13
--> tests/ui/option_if_let_else.rs:184:13
|
LL | let _ = if let Some(x) = Some(0) {
| _____________^
Expand All @@ -217,13 +235,13 @@ LL ~ });
|

error: use Option::map_or instead of an if let/else
--> tests/ui/option_if_let_else.rs:197:13
--> tests/ui/option_if_let_else.rs:213:13
|
LL | let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(0).map_or(s.len(), |x| s.len() + x)`

error: use Option::map_or instead of an if let/else
--> tests/ui/option_if_let_else.rs:202:13
--> tests/ui/option_if_let_else.rs:218:13
|
LL | let _ = if let Some(x) = Some(0) {
| _____________^
Expand All @@ -245,7 +263,7 @@ LL ~ });
|

error: use Option::map_or instead of an if let/else
--> tests/ui/option_if_let_else.rs:242:13
--> tests/ui/option_if_let_else.rs:258:13
|
LL | let _ = match s {
| _____________^
Expand All @@ -256,7 +274,7 @@ LL | | };
| |_____^ help: try: `s.map_or(1, |string| string.len())`

error: use Option::map_or instead of an if let/else
--> tests/ui/option_if_let_else.rs:247:13
--> tests/ui/option_if_let_else.rs:263:13
|
LL | let _ = match Some(10) {
| _____________^
Expand All @@ -267,7 +285,7 @@ LL | | };
| |_____^ help: try: `Some(10).map_or(5, |a| a + 1)`

error: use Option::map_or instead of an if let/else
--> tests/ui/option_if_let_else.rs:254:13
--> tests/ui/option_if_let_else.rs:270:13
|
LL | let _ = match res {
| _____________^
Expand All @@ -278,7 +296,7 @@ LL | | };
| |_____^ help: try: `res.map_or(1, |a| a + 1)`

error: use Option::map_or instead of an if let/else
--> tests/ui/option_if_let_else.rs:259:13
--> tests/ui/option_if_let_else.rs:275:13
|
LL | let _ = match res {
| _____________^
Expand All @@ -289,13 +307,13 @@ LL | | };
| |_____^ help: try: `res.map_or(1, |a| a + 1)`

error: use Option::map_or instead of an if let/else
--> tests/ui/option_if_let_else.rs:264:13
--> tests/ui/option_if_let_else.rs:280:13
|
LL | let _ = if let Ok(a) = res { a + 1 } else { 5 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `res.map_or(5, |a| a + 1)`

error: use Option::map_or instead of an if let/else
--> tests/ui/option_if_let_else.rs:282:17
--> tests/ui/option_if_let_else.rs:298:17
|
LL | let _ = match initial {
| _________________^
Expand All @@ -306,7 +324,7 @@ LL | | };
| |_________^ help: try: `initial.as_ref().map_or(42, |value| do_something(value))`

error: use Option::map_or instead of an if let/else
--> tests/ui/option_if_let_else.rs:290:17
--> tests/ui/option_if_let_else.rs:306:17
|
LL | let _ = match initial {
| _________________^
Expand All @@ -317,7 +335,7 @@ LL | | };
| |_________^ help: try: `initial.as_mut().map_or(42, |value| do_something2(value))`

error: use Option::map_or_else instead of an if let/else
--> tests/ui/option_if_let_else.rs:314:24
--> tests/ui/option_if_let_else.rs:330:24
|
LL | let mut _hashmap = if let Some(hm) = &opt {
| ________________________^
Expand All @@ -329,10 +347,10 @@ LL | | };
| |_____^ help: try: `opt.as_ref().map_or_else(HashMap::new, |hm| hm.clone())`

error: use Option::map_or_else instead of an if let/else
--> tests/ui/option_if_let_else.rs:321:19
--> tests/ui/option_if_let_else.rs:337:19
|
LL | let mut _hm = if let Some(hm) = &opt { hm.clone() } else { new_map!() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.as_ref().map_or_else(|| new_map!(), |hm| hm.clone())`

error: aborting due to 25 previous errors
error: aborting due to 27 previous errors