Skip to content
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
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
50 changes: 34 additions & 16 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)

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a separate issue from fixing what currently exists.

Copy link
Contributor

Choose a reason for hiding this comment

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

Zulip thead started here.

--> 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,19 +347,19 @@ 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: use Option::map_or instead of an if let/else
--> tests/ui/option_if_let_else.rs:372:22
--> tests/ui/option_if_let_else.rs:388:22
|
LL | let _ = unsafe { if let Some(o) = *opt_raw_ptr { o } else { 1 } };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(*opt_raw_ptr).map_or(1, |o| o)`

error: use Option::map_or_else instead of an if let/else
--> tests/ui/option_if_let_else.rs:378:13
--> tests/ui/option_if_let_else.rs:394:13
|
LL | let _ = match res {
| _____________^
Expand All @@ -351,5 +369,5 @@ LL | | Err(_) => String::new(),
LL | | };
| |_____^ help: try: `res.map_or_else(|_| String::new(), |s| s.clone())`

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