[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250207-macros-section-v1-1-8f018cb05a20@gmail.com>
Date: Fri, 07 Feb 2025 12:21:52 -0500
From: Tamir Duberstein <tamird@...il.com>
To: Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>, Danilo Krummrich <dakr@...nel.org>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
Tamir Duberstein <tamird@...il.com>
Subject: [PATCH 1/2] rust: macros: improve panic messages
Include unexpected input on parsing failures. This has the side effect
of avoiding a spurious rust-analyzer warning:
Variable `None` should have snake_case name, e.g. `none`
Signed-off-by: Tamir Duberstein <tamird@...il.com>
---
rust/macros/module.rs | 21 +++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)
diff --git a/rust/macros/module.rs b/rust/macros/module.rs
index cdf94f4982df..ca1b7e6a71ff 100644
--- a/rust/macros/module.rs
+++ b/rust/macros/module.rs
@@ -11,12 +11,14 @@ fn expect_string_array(it: &mut token_stream::IntoIter) -> Vec<String> {
let mut it = group.stream().into_iter();
while let Some(val) = try_string(&mut it) {
- assert!(val.is_ascii(), "Expected ASCII string");
+ assert!(val.is_ascii(), "Expected ASCII string, got {}", val);
values.push(val);
- match it.next() {
- Some(TokenTree::Punct(punct)) => assert_eq!(punct.as_char(), ','),
- None => break,
- _ => panic!("Expected ',' or end of array"),
+ let Some(token) = it.next() else {
+ break;
+ };
+ match token {
+ TokenTree::Punct(punct) => assert_eq!(punct.as_char(), ','),
+ token => panic!("Expected ',' or end of array, got {}", token),
}
}
values
@@ -116,11 +118,10 @@ fn parse(it: &mut token_stream::IntoIter) -> Self {
const REQUIRED_KEYS: &[&str] = &["type", "name", "license"];
let mut seen_keys = Vec::new();
- loop {
- let key = match it.next() {
- Some(TokenTree::Ident(ident)) => ident.to_string(),
- Some(_) => panic!("Expected Ident or end"),
- None => break,
+ while let Some(token) = it.next() {
+ let key = match token {
+ TokenTree::Ident(ident) => ident.to_string(),
+ token => panic!("Expected Ident or end, got {}", token),
};
if seen_keys.contains(&key) {
--
2.48.1
Powered by blists - more mailing lists