Skip to content

Commit

Permalink
chore: add test cases to verify ref and tmp checks
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-ang committed Jan 23, 2025
1 parent 28f651d commit 48e4fe8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
13 changes: 13 additions & 0 deletions tests/ui/return_and_then.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ fn main() {
test_res_block(Ok(n))
}

fn test_ref_only() -> Option<i32> {
let x = Some("")?;
if x.len() > 2 { Some(3) } else { None }
}

fn test_tmp_only() -> Option<i32> {
let x = Some(match (vec![1, 2, 3], vec![1, 2, 4]) {
(a, _) if a.len() > 1 => a,
(_, b) => b,
})?;
if x.len() > 2 { Some(3) } else { None }
}

// should not lint
fn test_tmp_ref() -> Option<String> {
String::from("<BOOM>")
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/return_and_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ fn main() {
opt.and_then(|n| test_res_block(Ok(n)))
}

fn test_ref_only() -> Option<i32> {
Some("").and_then(|x| if x.len() > 2 { Some(3) } else { None })
}

fn test_tmp_only() -> Option<i32> {
Some(match (vec![1, 2, 3], vec![1, 2, 4]) {
(a, _) if a.len() > 1 => a,
(_, b) => b,
})
.and_then(|x| if x.len() > 2 { Some(3) } else { None })
}

// should not lint
fn test_tmp_ref() -> Option<String> {
String::from("<BOOM>")
Expand Down
33 changes: 32 additions & 1 deletion tests/ui/return_and_then.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,36 @@ LL ~ let n = opt?;
LL + test_res_block(Ok(n))
|

error: aborting due to 5 previous errors
error: use the question mark operator instead of an `and_then` call
--> tests/ui/return_and_then.rs:29:9
|
LL | Some("").and_then(|x| if x.len() > 2 { Some(3) } else { None })
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL ~ let x = Some("")?;
LL + if x.len() > 2 { Some(3) } else { None }
|

error: use the question mark operator instead of an `and_then` call
--> tests/ui/return_and_then.rs:33:9
|
LL | / Some(match (vec![1, 2, 3], vec![1, 2, 4]) {
LL | | (a, _) if a.len() > 1 => a,
LL | | (_, b) => b,
LL | | })
LL | | .and_then(|x| if x.len() > 2 { Some(3) } else { None })
| |_______________________________________________________________^
|
help: try
|
LL ~ let x = Some(match (vec![1, 2, 3], vec![1, 2, 4]) {
LL + (a, _) if a.len() > 1 => a,
LL + (_, b) => b,
LL + })?;
LL + if x.len() > 2 { Some(3) } else { None }
|

error: aborting due to 7 previous errors

0 comments on commit 48e4fe8

Please sign in to comment.