Skip to content

Commit 47e0eff

Browse files
authored
Merge 'Kill unwrap() in macros' from Pekka Enberg
Closes #4004
2 parents a97081e + 36d2dfc commit 47e0eff

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

macros/src/atomic_enum.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ pub(crate) fn derive_atomic_enum_inner(input: TokenStream) -> TokenStream {
123123
}
124124
}
125125
}
126-
Fields::Unnamed(_) => {
126+
Fields::Unnamed(fields) => {
127127
// same strategy as above, but for tuple variants like `Write(bool)`
128-
if is_bool_type(&variant.fields.iter().next().unwrap().ty) {
128+
let field = &fields.unnamed[0];
129+
if is_bool_type(&field.ty) {
129130
if use_u16 {
130131
quote! {
131132
#name::#var_name(ref val) => {
@@ -186,8 +187,9 @@ pub(crate) fn derive_atomic_enum_inner(input: TokenStream) -> TokenStream {
186187
}
187188
}
188189
}
189-
Fields::Unnamed(_) => {
190-
if is_bool_type(&variant.fields.iter().next().unwrap().ty) {
190+
Fields::Unnamed(fields) => {
191+
let field = &fields.unnamed[0];
192+
if is_bool_type(&field.ty) {
191193
if use_u16 {
192194
quote! { #disc => #name::#var_name((val >> 8) != 0) }
193195
} else {

macros/src/ext/match_ignore_ascii_case.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,12 @@ pub fn match_ignore_ascci_case(input: TokenStream) -> TokenStream {
8686
let mut current = &mut paths;
8787

8888
for b in keyword_b {
89-
match current.sub_entries.get(&b) {
90-
Some(_) => {
91-
current = current.sub_entries.get_mut(&b).unwrap();
92-
}
93-
None => {
94-
let new_entry = Box::new(PathEntry {
95-
result: None,
96-
sub_entries: HashMap::new(),
97-
});
98-
current.sub_entries.insert(b, new_entry);
99-
current = current.sub_entries.get_mut(&b).unwrap();
100-
}
101-
}
89+
current = current.sub_entries.entry(b).or_insert_with(|| {
90+
Box::new(PathEntry {
91+
result: None,
92+
sub_entries: HashMap::new(),
93+
})
94+
});
10295
}
10396

10497
assert!(current.result.is_none());

macros/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,17 @@ fn generate_get_description(
132132
}
133133

134134
let enum_impl = format!(
135-
"impl {enum_name} {{
135+
"impl {enum_name} {{
136136
pub fn get_description(&self) -> Option<&str> {{
137137
match self {{
138138
{all_enum_arms}
139139
}}
140140
}}
141141
}}"
142142
);
143-
enum_impl.parse().unwrap()
143+
enum_impl
144+
.parse()
145+
.expect("generated code should be valid Rust")
144146
}
145147

146148
/// Register your extension with 'core' by providing the relevant functions

0 commit comments

Comments
 (0)