-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix clippy expect_used
complains for derive(FromRow)
macro
#1077
Conversation
See the following report for details: cargo semver-checks output
|
There was a recent PR in this area: #1055
|
@Lorak-mmk I can't agree with you. Have a opposite opinion on this 🙂. #[allow(clippy::expect_used)]
mod result {
use scylla::FromRow;
#[derive(FromRow)]
pub(crate) struct YourStruct {
}
}
use result:: YourStruct; In general, for me it seems that the best solution would be eliminate the usage of the But in general I would somehow fix this part, to reduce a headache on the clients side, because right its annoying, I can tell from our team experience 🙂 . |
If your goal in denying use of
Yeah that's ugly :/ It would be much better if this worked: #[derive(Debug, FromRow)]
#[allow(clippy::expect_used)]
struct RowData {
} It would be good to allow this (unless it turns out to be impossible for some reason). After it is done it would be my recommended fix here instead of putting this
I agree that removing
|
Agreed. I support this, especially that it would allow solving other similar problems arising from macro expansion, too, by adding corresponding |
@Lorak-mmk , @wprzytula yea, sorry I see your point. just adding Seems I've found one possible solution how to eliminate the usage of |
83e760d
to
408af11
Compare
scylla-macros/src/from_row.rs
Outdated
let (col_ix, col_value) = vals_iter | ||
.next() | ||
.expect("BUG: Size validated iterator did not contain the expected number of values"); | ||
// to avoid unnecessary copy `std::mem::take` is used | ||
let col_value = ::std::mem::take(&mut row_columns[#col_ix]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expect
(a method that may panic) is replaced with indexing (which may panic as well). This defeats the purpose of expect_used
lint, which is to keep the code free of possible panics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've used an array instead of iterator let mut row_columns: [_; #fields_count]
, which allows for Rust compiler to detect such index out of range
errors during the compile time.
So technically speaking if you have not disabled unconditional panic lint (which you definitely not), this code is safe.
And if in this code a bug will be appears it will be 99.9%
(0.1%
stand when unconditional panic will be disabled, and I dont why someone really want to do that, its like to disable a borrow checker) catches by the compiler, with the detailed explanation what was incorrect.
E.g. I've modified this line of the code to
let col_value = ::std::mem::take(&mut row_columns[#col_ix + 1]);
And rustc immediately complains
error: this operation will panic at runtime
--> scylla-cql/src/frame/response/cql_to_rust.rs:1014:43
|
1014 | struct MyRow(i32, Option<String>, Option<Vec<i32>>);
| ^^^^^^ index out of bounds: the length is 3 but the index is 3
|
= note: `#[deny(unconditional_panic)]` on by default
error: could not compile `scylla-cql` (lib test) due to 1 previous error
https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/builtin/static.UNCONDITIONAL_PANIC.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So to sum up, this code allows to catch an error on the compile time, using .expect
does not allow to do that and will return it during the runtime only
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, it seems I have missed that you made row_columns
an array. In such case indexing can be indeed checked by the compiler, with no panic risk in runtime if no warning is emitted about that.
Could you please write in comments why the array is used (to preserve length info in the type, to avoid panics) and why indexing won't panic (because array has a compile-time known length, and the index is a compile-time const, too)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea sure, will do that !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wprzytula done
@Mr-Leshiy Note that the new deserialization framework is going to soon replace the old one, with |
@wprzytula Yea, would be happy to help there as well. |
// Enabling `expect_used` clippy lint, | ||
// validates that `derive(FromRow)` macro definition does do not violates such rule under the hood. | ||
// Could be removed after such rule will be applied for the whole crate. | ||
// <https://rust-lang.github.io/rust-clippy/master/index.html#/expect_used> | ||
#[deny(clippy::expect_used)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you delete this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following our discussion, I agreed with the value of such possible linting errors which could appear under the macro definition.
So as we dont want to prevent using expect
or unwrap
under the macro definition, this line of code just does not make sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So as we dont want to prevent using
expect
orunwrap
under the macro definition
I'm confused. I thought the purpose of this PR, that is, the changes I've just reviewed, is to remove use of expect
or unwrap
from the macro definition, so that the lint can be used with no exception of the macro-generated code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔
yea, agree will add it back, make sense
Description
Disables
clippy::expect_used
clippy rule for thederive(FromRow)
macro.As right now inside
derive(FromRow)
definition, as a part of the produced by this macro codeexpect()
is used,so it also violates and affects the clients code.
For that cases when the consumer of this library enabled
clippy::expect_used
for their project,it requires to somehow fix it and disable it for the
derive(FromRow)
produced code.Dependent issue
input-output-hk/catalyst-voices#828
Pre-review checklist
./docs/source/
.Fixes:
annotations to PR description.