about summary refs log tree commit diff
diff options
context:
space:
mode:
authordswij <dswijj@gmail.com>2021-10-19 18:27:37 +0800
committerdswij <dswijj@gmail.com>2021-10-19 18:50:49 +0800
commit3fc99b6a3376a568adc2030e93b82ad3f99eb189 (patch)
tree1cf6cf73082e5475e78d0678eb5cdcd5d5733863
parent687f3925da6481d938be0d787808328f8a93fdee (diff)
downloadrust-3fc99b6a3376a568adc2030e93b82ad3f99eb189.tar.gz
rust-3fc99b6a3376a568adc2030e93b82ad3f99eb189.zip
Update test for `question_mark` to cover `Result`
-rw-r--r--tests/ui/question_mark.fixed17
-rw-r--r--tests/ui/question_mark.rs19
-rw-r--r--tests/ui/question_mark.stderr16
3 files changed, 51 insertions, 1 deletions
diff --git a/tests/ui/question_mark.fixed b/tests/ui/question_mark.fixed
index 0b5746cb522..ccb2e5a302e 100644
--- a/tests/ui/question_mark.fixed
+++ b/tests/ui/question_mark.fixed
@@ -104,6 +104,21 @@ fn func() -> Option<i32> {
     Some(0)
 }
 
+fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
+    let _ = x?;
+
+    x?;
+
+    // No warning
+    let y = if let Ok(x) = x {
+        x
+    } else {
+        return Err("some error");
+    };
+
+    Ok(y)
+}
+
 fn main() {
     some_func(Some(42));
     some_func(None);
@@ -123,4 +138,6 @@ fn main() {
     returns_something_similar_to_option(so);
 
     func();
+
+    let _ = result_func(Ok(42));
 }
diff --git a/tests/ui/question_mark.rs b/tests/ui/question_mark.rs
index 0f0825c9334..ca3722371f5 100644
--- a/tests/ui/question_mark.rs
+++ b/tests/ui/question_mark.rs
@@ -134,6 +134,23 @@ fn func() -> Option<i32> {
     Some(0)
 }
 
+fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
+    let _ = if let Ok(x) = x { x } else { return x };
+
+    if x.is_err() {
+        return x;
+    }
+
+    // No warning
+    let y = if let Ok(x) = x {
+        x
+    } else {
+        return Err("some error");
+    };
+
+    Ok(y)
+}
+
 fn main() {
     some_func(Some(42));
     some_func(None);
@@ -153,4 +170,6 @@ fn main() {
     returns_something_similar_to_option(so);
 
     func();
+
+    let _ = result_func(Ok(42));
 }
diff --git a/tests/ui/question_mark.stderr b/tests/ui/question_mark.stderr
index 6f330cfa385..161588cb73c 100644
--- a/tests/ui/question_mark.stderr
+++ b/tests/ui/question_mark.stderr
@@ -100,5 +100,19 @@ LL | |         return None;
 LL | |     }
    | |_____^ help: replace it with: `f()?;`
 
-error: aborting due to 11 previous errors
+error: this if-let-else may be rewritten with the `?` operator
+  --> $DIR/question_mark.rs:138:13
+   |
+LL |     let _ = if let Ok(x) = x { x } else { return x };
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x?`
+
+error: this block may be rewritten with the `?` operator
+  --> $DIR/question_mark.rs:140:5
+   |
+LL | /     if x.is_err() {
+LL | |         return x;
+LL | |     }
+   | |_____^ help: replace it with: `x?;`
+
+error: aborting due to 13 previous errors