Skip to content
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

scylla-cql, examples: fix clippy 1.75 issues #900

Merged
merged 2 commits into from
Jan 2, 2024

Conversation

piodul
Copy link
Collaborator

@piodul piodul commented Dec 30, 2023

With the new rust toolchain version comes a new clippy version, and therefore new complaints of the linter to address. This PR fixes the newly reported issues.

Pre-review checklist

  • I have split my patch into logically separate commits.
  • All commit messages clearly explain what they change and why.
  • I added relevant tests for new features and bug fixes.
  • All commits compile, pass static checks and pass test.
  • PR description sums up the changes and reasons why they should be introduced.
  • I have provided docstrings for the public items that I want to introduce.
  • I have adjusted the documentation in ./docs/source/.
  • I added appropriate Fixes: annotations to PR description.

In some places, we have the following expression:

    x.iter().map(|(k, v)| (k, v))

Although the .map call looks like a no-op, it changes the type of the
argument from `&(K, V)` to `(&K, &V)`. This happens due to the so-called
"reference matching ergonomics" feature of Rust which allows binding a
reference to a tuple to the `(k, v)` expression and allows referring to
the tuple's constituents by reference (`k: &K`, `v: &V`).

The issue is already known and reported and will most likely be fixed
in a future release, but for the time being let's reformulate the code
to avoid the lint like this:

    x.iter().map(|p| (&p.0, &p.1))
Clippy suggests that .first() is more readable than .get(0).
@piodul piodul requested a review from Lorak-mmk December 30, 2023 16:21
CqlValue::Map(m) => serialize_map(m.iter().map(|(k, v)| (k, v)), m.len(), buf),
CqlValue::Map(m) => serialize_map(m.iter().map(|p| (&p.0, &p.1)), m.len(), buf),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What lint does it fail? Can the change be made differently? Imo the old version is much easier to read - you know that the argument is 2-element tuple, in the new version you don't know that

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What lint does it fail?

https://rust-lang.github.io/rust-clippy/master/index.html#map_identity

For example, I get this message:

warning: unnecessary map of the identity function
   --> scylla-cql/src/frame/value.rs:944:55
    |
944 |             CqlValue::Map(m) => serialize_map(m.iter().map(|(k, v)| (k, v)), m.len(), buf),
    |                                                       ^^^^^^^^^^^^^^^^^^^^^ help: remove the call to `map`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_identity
    = note: `#[warn(clippy::map_identity)]` on by default

See the commit description for more details.

Imo the old version is much easier to read - you know that the argument is 2-element tuple, in the new version you don't know that

I think it's quite clear here that m.iter() iterates over pairs, so I'm not sure if it's a huge problem. I'd argue that the code is now more clear as it's now more obvious what the lambda in the .map does.

Can the change be made differently?

Perhaps it can, but I'm not sure how would you like me to change it.

@@ -488,7 +488,7 @@ fn serialize_cql_value<'b>(
CqlValue::Map(m) => serialize_mapping(
std::any::type_name::<CqlValue>(),
m.len(),
m.iter().map(|(ref k, ref v)| (k, v)),
m.iter().map(|p| (&p.0, &p.1)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@Lorak-mmk Lorak-mmk merged commit 8263c53 into scylladb:main Jan 2, 2024
8 checks passed
@Lorak-mmk Lorak-mmk mentioned this pull request Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants