From 6b48b090f8fac94927d0a6d08c0fb0efef979e66 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 4 Nov 2024 15:14:31 -0500 Subject: [PATCH 1/2] Add test of non-rawable keyword in format string --- tests/test_display.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_display.rs b/tests/test_display.rs index 89bdc4a..7fad386 100644 --- a/tests/test_display.rs +++ b/tests/test_display.rs @@ -318,6 +318,15 @@ fn test_keyword() { assert("error: 1", Error); } +#[test] +fn test_self() { + #[derive(Error, Debug)] + #[error("error: {self:?}")] + struct Error; + + assert("error: Error", Error); +} + #[test] fn test_str_special_chars() { #[derive(Error, Debug)] From 4d397bc1864e44f3cfdd788c3ef4c1b3e3ef06c0 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 4 Nov 2024 15:15:44 -0500 Subject: [PATCH 2/2] Add test of underscore variable in format string --- tests/ui/display-underscore.rs | 7 +++++++ tests/ui/display-underscore.stderr | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 tests/ui/display-underscore.rs create mode 100644 tests/ui/display-underscore.stderr diff --git a/tests/ui/display-underscore.rs b/tests/ui/display-underscore.rs new file mode 100644 index 0000000..335614b --- /dev/null +++ b/tests/ui/display-underscore.rs @@ -0,0 +1,7 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +#[error("{_}")] +pub struct Error; + +fn main() {} diff --git a/tests/ui/display-underscore.stderr b/tests/ui/display-underscore.stderr new file mode 100644 index 0000000..36882b9 --- /dev/null +++ b/tests/ui/display-underscore.stderr @@ -0,0 +1,7 @@ +error: invalid format string: invalid argument name `_` + --> tests/ui/display-underscore.rs:4:11 + | +4 | #[error("{_}")] + | ^ invalid argument name in format string + | + = note: argument name cannot be a single underscore