Skip to content

Commit beac7de

Browse files
authored
format variant with optional brackets around the sum (#2048)
wit-bindgen generated Rust code warns because of unnecessary brackets generated by the format() function
1 parent eabdf0e commit beac7de

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

Diff for: crates/wit-parser/src/sizealign.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,38 @@ impl ArchitectureSize {
180180

181181
// create a suitable expression in bytes from a pointer size argument
182182
pub fn format(&self, ptrsize_expr: &str) -> String {
183+
self.format_term(ptrsize_expr, false)
184+
}
185+
186+
// create a suitable expression in bytes from a pointer size argument,
187+
// extended API with optional brackets around the sum
188+
pub fn format_term(&self, ptrsize_expr: &str, suppress_brackets: bool) -> String {
183189
if self.pointers != 0 {
184190
if self.bytes > 0 {
185191
// both
186-
format!(
187-
"({}+{}*{ptrsize_expr})",
188-
self.constant_bytes(),
189-
self.pointers_to_add()
190-
)
192+
if suppress_brackets {
193+
format!(
194+
"{}+{}*{ptrsize_expr}",
195+
self.constant_bytes(),
196+
self.pointers_to_add()
197+
)
198+
} else {
199+
format!(
200+
"({}+{}*{ptrsize_expr})",
201+
self.constant_bytes(),
202+
self.pointers_to_add()
203+
)
204+
}
191205
} else if self.pointers == 1 {
192206
// one pointer
193207
ptrsize_expr.into()
194208
} else {
195209
// only pointer
196-
format!("({}*{ptrsize_expr})", self.pointers_to_add())
210+
if suppress_brackets {
211+
format!("{}*{ptrsize_expr}", self.pointers_to_add())
212+
} else {
213+
format!("({}*{ptrsize_expr})", self.pointers_to_add())
214+
}
197215
}
198216
} else {
199217
// only bytes

0 commit comments

Comments
 (0)