Skip to content

Commit 48e4fe8

Browse files
committed
chore: add test cases to verify ref and tmp checks
1 parent 28f651d commit 48e4fe8

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

tests/ui/return_and_then.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ fn main() {
2828
test_res_block(Ok(n))
2929
}
3030

31+
fn test_ref_only() -> Option<i32> {
32+
let x = Some("")?;
33+
if x.len() > 2 { Some(3) } else { None }
34+
}
35+
36+
fn test_tmp_only() -> Option<i32> {
37+
let x = Some(match (vec![1, 2, 3], vec![1, 2, 4]) {
38+
(a, _) if a.len() > 1 => a,
39+
(_, b) => b,
40+
})?;
41+
if x.len() > 2 { Some(3) } else { None }
42+
}
43+
3144
// should not lint
3245
fn test_tmp_ref() -> Option<String> {
3346
String::from("<BOOM>")

tests/ui/return_and_then.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ fn main() {
2525
opt.and_then(|n| test_res_block(Ok(n)))
2626
}
2727

28+
fn test_ref_only() -> Option<i32> {
29+
Some("").and_then(|x| if x.len() > 2 { Some(3) } else { None })
30+
}
31+
32+
fn test_tmp_only() -> Option<i32> {
33+
Some(match (vec![1, 2, 3], vec![1, 2, 4]) {
34+
(a, _) if a.len() > 1 => a,
35+
(_, b) => b,
36+
})
37+
.and_then(|x| if x.len() > 2 { Some(3) } else { None })
38+
}
39+
2840
// should not lint
2941
fn test_tmp_ref() -> Option<String> {
3042
String::from("<BOOM>")

tests/ui/return_and_then.stderr

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,36 @@ LL ~ let n = opt?;
6666
LL + test_res_block(Ok(n))
6767
|
6868

69-
error: aborting due to 5 previous errors
69+
error: use the question mark operator instead of an `and_then` call
70+
--> tests/ui/return_and_then.rs:29:9
71+
|
72+
LL | Some("").and_then(|x| if x.len() > 2 { Some(3) } else { None })
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
74+
|
75+
help: try
76+
|
77+
LL ~ let x = Some("")?;
78+
LL + if x.len() > 2 { Some(3) } else { None }
79+
|
80+
81+
error: use the question mark operator instead of an `and_then` call
82+
--> tests/ui/return_and_then.rs:33:9
83+
|
84+
LL | / Some(match (vec![1, 2, 3], vec![1, 2, 4]) {
85+
LL | | (a, _) if a.len() > 1 => a,
86+
LL | | (_, b) => b,
87+
LL | | })
88+
LL | | .and_then(|x| if x.len() > 2 { Some(3) } else { None })
89+
| |_______________________________________________________________^
90+
|
91+
help: try
92+
|
93+
LL ~ let x = Some(match (vec![1, 2, 3], vec![1, 2, 4]) {
94+
LL + (a, _) if a.len() > 1 => a,
95+
LL + (_, b) => b,
96+
LL + })?;
97+
LL + if x.len() > 2 { Some(3) } else { None }
98+
|
99+
100+
error: aborting due to 7 previous errors
70101

0 commit comments

Comments
 (0)