about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2022-11-10 10:47:38 -0500
committerGitHub <noreply@github.com>2022-11-10 10:47:38 -0500
commit529f7149d2c3fbf5e8b24325331bf3ae6f393616 (patch)
tree91cda1a28289545457d232655a091a40dc359be5 /src/test
parent9c0e783f6df363b7bc54b1988780e42b6f84dceb (diff)
parent38ada60eb68b20d72f0ab0618e3192f9d41770f0 (diff)
downloadrust-529f7149d2c3fbf5e8b24325331bf3ae6f393616.tar.gz
rust-529f7149d2c3fbf5e8b24325331bf3ae6f393616.zip
Rollup merge of #104036 - compiler-errors:option-sugg, r=petrochenkov
Suggest `is_some` when we've found `Option` but expected `bool`

Thanks `@lunasorcery` for the suggestion.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ui/suggestions/option-to-bool.rs9
-rw-r--r--src/test/ui/suggestions/option-to-bool.stderr16
2 files changed, 25 insertions, 0 deletions
diff --git a/src/test/ui/suggestions/option-to-bool.rs b/src/test/ui/suggestions/option-to-bool.rs
new file mode 100644
index 00000000000..2a1823b15f5
--- /dev/null
+++ b/src/test/ui/suggestions/option-to-bool.rs
@@ -0,0 +1,9 @@
+#![cfg_attr(let_chains, feature(let_chains))]
+
+fn foo(x: Option<i32>) {
+    if true && x {}
+    //~^ ERROR mismatched types
+    //~| HELP use `Option::is_some` to test if the `Option` has a value
+}
+
+fn main() {}
diff --git a/src/test/ui/suggestions/option-to-bool.stderr b/src/test/ui/suggestions/option-to-bool.stderr
new file mode 100644
index 00000000000..57a934b8342
--- /dev/null
+++ b/src/test/ui/suggestions/option-to-bool.stderr
@@ -0,0 +1,16 @@
+error[E0308]: mismatched types
+  --> $DIR/option-to-bool.rs:4:16
+   |
+LL |     if true && x {}
+   |                ^ expected `bool`, found enum `Option`
+   |
+   = note: expected type `bool`
+              found enum `Option<i32>`
+help: use `Option::is_some` to test if the `Option` has a value
+   |
+LL |     if true && x.is_some() {}
+   |                 ++++++++++
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.