Skip to content

Commit 892387e

Browse files
committed
skip generating constructors if there are no args
1 parent 4404a5e commit 892387e

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

book/src/formality_core/constructors.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
Unless you include `#[customize(constructors)]`, the `#[term]` macro automatically creates constructors as follows:
44

55
- For a `struct`, defines a `new` method that takes an `impl Upcast<T>` for each of your fields.
6-
- For an `enum`, defines a method per variant (converted to snake-case).
6+
- For an `enum`, defines a method per variant that has fields (converted to snake-case).
77
- If the name of the variant is a Rust keyword like `Struct`, the method will be called `struct_`.
8+
- We do not generate constructors for variants with no arguments.

crates/formality-macros/src/constructors.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ fn derive_new_for_variant(
5151
let type_name = &s.ast().ident;
5252
let (impl_generics, type_generics, where_clauses) = s.ast().generics.split_for_impl();
5353

54+
// If there are no bindings, not worth it.
55+
if v.bindings().is_empty() {
56+
return TokenStream::default();
57+
}
58+
5459
let binding_names = v.bindings().iter().map(|b| &b.binding).collect::<Vec<_>>();
5560
let binding_types = v.bindings().iter().map(|b| &b.ast().ty).collect::<Vec<_>>();
5661
let construct = v.construct(|_b, i| {

0 commit comments

Comments
 (0)