Skip to content

Commit

Permalink
Remove Problematic Unwraps In Macros (#1055)
Browse files Browse the repository at this point in the history
Code created by macros is compiled as part of the end users build.
This  means any lints enabled by a user of the driver will check the macro generated code as well.
This code contains some unwraps, which will trigger `unwrap_used` lint.
Those unwraps are therefore changed to `expect`s to avoid triggering the lint and give more informative error if they are ever triggered.
  • Loading branch information
Kailokk authored Aug 20, 2024
1 parent bbb2874 commit 0093431
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
7 changes: 2 additions & 5 deletions scylla-macros/src/from_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ pub(crate) fn from_row_derive(tokens_input: TokenStream) -> Result<TokenStream,
#field_name: {
let (col_ix, col_value) = vals_iter
.next()
.unwrap(); // vals_iter size is checked before this code is reached, so
// it is safe to unwrap

.expect("BUG: Size validated iterator did not contain the expected number of values");
<#field_type as FromCqlVal<::std::option::Option<CqlValue>>>::from_cql(col_value)
.map_err(|e| FromRowError::BadCqlVal {
err: e,
Expand All @@ -48,8 +46,7 @@ pub(crate) fn from_row_derive(tokens_input: TokenStream) -> Result<TokenStream,
{
let (col_ix, col_value) = vals_iter
.next()
.unwrap(); // vals_iter size is checked before this code is reached, so
// it is safe to unwrap
.expect("BUG: Size validated iterator did not contain the expected number of values");

<#field_type as FromCqlVal<::std::option::Option<CqlValue>>>::from_cql(col_value)
.map_err(|e| FromRowError::BadCqlVal {
Expand Down
2 changes: 1 addition & 1 deletion scylla-macros/src/from_user_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub(crate) fn from_user_type_derive(tokens_input: TokenStream) -> Result<TokenSt
// field)
if let Some(received_field_name) = received_field_name {
if received_field_name == stringify!(#field_name) {
let (_, value) = fields_iter.next().unwrap();
let (_, value) = fields_iter.next().expect("BUG: Validated iterator did not contain expected number of values");
value
} else {
None
Expand Down

0 comments on commit 0093431

Please sign in to comment.