Open diseraluca opened 3 years ago
Some possible solutions might be:
#[allow(unused_braces)]
attribute to the generated implementations.generate_bitfield_size
, and reinstate the block locally where it is required.Solution 3 is what I would consider, albeit it requires knowledge of where blocks are needed and might require some code to discern between an actual sum and a value.
For example, if 3 was to be implemented sloppily, the following code would not work anymore:
#[bitfield]
struct Foo {
bar: B16,
baz: B16,
}
As the code generated in impl::bitfield::expand::generate_filled_check_for_aligned_bits
would be incorrectly parenthesized.
This can easily be solved locally by parenthesizing the expression itself, knowing that a 0-fields bitfield cannot exist and that the format of generate_bitfield_size
has, hence, always at least 2 addends, as follows:
fn generate_filled_check_for_aligned_bits(&self, config: &Config) -> TokenStream2 {
[...]
type Size = ::modular_bitfield::private::checks::TotalSize<[(); (#actual_bits) % 8usize]>;
[...]
Similar cases may exist but they should be findable by tracing the uses of generate_bitfield_size
.
I'm currently using modular-bitfield for a project and am incurring in warnings generated by the the crate.
When a bitfield structure is annotated with a
repr
representation, the generated code will implement From-conversions to and from the represented type.For example:
Would produce the following code:
In the where clause of the From implementations, the constant expression that gives the length of the of the array is enclosed in braces which are unneeded, producing the following compiler warning:
This From expansion seems to happen in
impl::bitfield::expand::expand_repr_from_impls_and_checks
, where the following lines expand to the warned-about code:Then, in
generate_target_or_actual_bitfield_size
, if the configuration has aNone
value for thebits
field, the generation is delegated togenerate_bitfield_size
which generates the expression with the unneeded braces:I consider that the correct behavior here would be to avoid generating a warning by removing the braces when there is no need for them.
If it is desirable for the project to have this change, I would gladly make it myself as I need it for my project.