about summary refs log tree commit diff
path: root/tests/ui/suggestions
diff options
context:
space:
mode:
authorNick Lamb <git@tree.tlrmx.org>2023-01-14 14:51:17 +0000
committerNick Lamb <git@tree.tlrmx.org>2023-01-14 21:27:14 +0000
commit130d02b62e65c5f2a434eaec63c4249e9d508487 (patch)
tree00831f01020403ed503ca559cd8abeb15a2c1085 /tests/ui/suggestions
parent44a500c8c187b245638684748f54bd6ec67e0b25 (diff)
downloadrust-130d02b62e65c5f2a434eaec63c4249e9d508487.tar.gz
rust-130d02b62e65c5f2a434eaec63c4249e9d508487.zip
Improve E0308: suggest user meant to use byte literal, w/ tests and fix
suggested by Nilstrieb

Co-authored-by: nils <48135649+Nilstrieb@users.noreply.github.com>
Diffstat (limited to 'tests/ui/suggestions')
-rw-r--r--tests/ui/suggestions/type-mismatch-byte-literal.rs18
-rw-r--r--tests/ui/suggestions/type-mismatch-byte-literal.stderr42
2 files changed, 60 insertions, 0 deletions
diff --git a/tests/ui/suggestions/type-mismatch-byte-literal.rs b/tests/ui/suggestions/type-mismatch-byte-literal.rs
new file mode 100644
index 00000000000..34199f8c37c
--- /dev/null
+++ b/tests/ui/suggestions/type-mismatch-byte-literal.rs
@@ -0,0 +1,18 @@
+// Tests that a suggestion is issued for type mismatch errors when a
+// u8 is expected and a char literal which is ASCII is supplied.
+
+fn foo(_t: u8) {}
+
+fn main() {
+    let _x: u8 = 'X';
+    //~^ ERROR: mismatched types [E0308]
+    //~| HELP: if you meant to write a byte literal, prefix with `b`
+
+    foo('#');
+    //~^ ERROR: mismatched types [E0308]
+    //~| HELP: if you meant to write a byte literal, prefix with `b`
+
+    // Do not issue the suggestion if the char literal isn't ASCII
+    let _t: u8 = '€';
+    //~^ ERROR: mismatched types [E0308]
+}
diff --git a/tests/ui/suggestions/type-mismatch-byte-literal.stderr b/tests/ui/suggestions/type-mismatch-byte-literal.stderr
new file mode 100644
index 00000000000..c9c2e7498d0
--- /dev/null
+++ b/tests/ui/suggestions/type-mismatch-byte-literal.stderr
@@ -0,0 +1,42 @@
+error[E0308]: mismatched types
+  --> $DIR/type-mismatch-byte-literal.rs:7:18
+   |
+LL |     let _x: u8 = 'X';
+   |             --   ^^^ expected `u8`, found `char`
+   |             |
+   |             expected due to this
+   |
+help: if you meant to write a byte literal, prefix with `b`
+   |
+LL |     let _x: u8 = b'X';
+   |                  ~~~~
+
+error[E0308]: mismatched types
+  --> $DIR/type-mismatch-byte-literal.rs:11:9
+   |
+LL |     foo('#');
+   |     --- ^^^ expected `u8`, found `char`
+   |     |
+   |     arguments to this function are incorrect
+   |
+note: function defined here
+  --> $DIR/type-mismatch-byte-literal.rs:4:4
+   |
+LL | fn foo(_t: u8) {}
+   |    ^^^ ------
+help: if you meant to write a byte literal, prefix with `b`
+   |
+LL |     foo(b'#');
+   |         ~~~~
+
+error[E0308]: mismatched types
+  --> $DIR/type-mismatch-byte-literal.rs:16:18
+   |
+LL |     let _t: u8 = '€';
+   |             --   ^^^ expected `u8`, found `char`
+   |             |
+   |             expected due to this
+
+error: aborting due to 3 previous errors
+
+For more information about this error, try `rustc --explain E0308`.