Skip to content

Commit f11cddd

Browse files
bors[bot]burrbull
andauthored
Merge #568
568: offset_calc fix parenthesizing r=adamgreig a=burrbull Closes #567 Co-authored-by: Andrey Zgarbul <zgarbul.andrey@gmail.com>
2 parents 59ae59c + acfe019 commit f11cddd

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Fixed
1111

12+
- Parenthesizing `#offset_calc` to avoid clippy's warning of operator precedence
1213
- Replace suffix in fields' name before converting to snake case when generating methods #563
1314

1415
## [v0.20.0] - 2021-12-07

src/generate/register.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ pub fn fields(
413413
};
414414

415415
if let Some((first, dim, increment, suffixes, suffixes_str)) = &field_dim {
416-
let offset_calc = calculate_offset(*first, *increment, offset);
416+
let offset_calc = calculate_offset(*first, *increment, offset, true);
417417
let value = quote! { ((self.bits >> #offset_calc) & #hexmask) #cast };
418418
let doc = &util::replace_suffix(&description, suffixes_str);
419419
r_impl_items.extend(quote! {
@@ -793,7 +793,7 @@ pub fn fields(
793793
}
794794

795795
if let Some((first, dim, increment, suffixes, suffixes_str)) = &field_dim {
796-
let offset_calc = calculate_offset(*first, *increment, offset);
796+
let offset_calc = calculate_offset(*first, *increment, offset, false);
797797
let doc = &util::replace_suffix(&description, suffixes_str);
798798
w_impl_items.extend(quote! {
799799
#[doc = #doc]
@@ -985,7 +985,12 @@ fn add_from_variants(
985985
});
986986
}
987987

988-
fn calculate_offset(first: u32, increment: u32, offset: u64) -> TokenStream {
988+
fn calculate_offset(
989+
first: u32,
990+
increment: u32,
991+
offset: u64,
992+
with_parentheses: bool,
993+
) -> TokenStream {
989994
let mut res = if first != 0 {
990995
let first = util::unsuffixed(first as u64);
991996
quote! { n - #first }
@@ -1002,7 +1007,15 @@ fn calculate_offset(first: u32, increment: u32, offset: u64) -> TokenStream {
10021007
}
10031008
if offset != 0 {
10041009
let offset = &util::unsuffixed(offset);
1005-
res = quote! { #res + #offset };
1010+
if with_parentheses {
1011+
res = quote! { (#res + #offset) };
1012+
} else {
1013+
res = quote! { #res + #offset };
1014+
}
1015+
} else {
1016+
if with_parentheses {
1017+
res = quote! { (#res) };
1018+
}
10061019
}
10071020
res
10081021
}

0 commit comments

Comments
 (0)