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

feat(clippy): add needless_pass_by_value #9197

Closed
wants to merge 3 commits into from

Conversation

tcoratger
Copy link
Contributor

@tcoratger tcoratger commented Jun 29, 2024

Should close #8886

Comment on lines +88 to +89
primary_tx: &impl DbTx,
secondary_tx: &impl DbTx,
Copy link
Member

@emhane emhane Jun 29, 2024

Choose a reason for hiding this comment

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

I'm fine with this for the perf improvement, and imo the flexibility of having impl T instead of &impl T is rarely used, may break some public api though, so anywhere we should allow this @mattsse ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

&impl looks very horrible

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

this is very invasive and I don't want to let clippy decide how function signatures should look like.

I believe there are a few instances where the suggestions are valid, but I'm not a fan of this lint overall

@tcoratger
Copy link
Contributor Author

this is very invasive and I don't want to let clippy decide how function signatures should look like.

I believe there are a few instances where the suggestions are valid, but I'm not a fan of this lint overall

@mattsse Tell me what you want to do, I have the impression that all the suggestions are more or less relevant and there is no false postive I have the impression.

From what I saw it removed unnecessary memory allocations due to cloning and made certain implementations more flexible in my opinion. I feel like this is a good guideline to follow for implementations when you're not sure whether to put a reference or a value.

On the other hand I noticed that it never altered the functioning of a function and no change other than passing by reference instead of by value was necessary.

For the &impl, I agree that it's horrible, if it's only in this place the problem, we can put an allow.

I also saw https://rust-lang.github.io/rust-clippy/master/#/needless_pass_by_ref_mut which can be a good addition to this to counterbalance.

But if there are too many places where this lint bothers you then it's not worth putting allows everywhere.

@emhane
Copy link
Member

emhane commented Jul 2, 2024

I even wanted to open an issue for fixing this is several places where we copy a 64 byte PeerId for now reason, where we can pass a ref. also I'm for the & impl because it helps informing the a type is sized! hope we can consider merging this rather sooner than later

@emhane
Copy link
Member

emhane commented Jul 2, 2024

& impl also gives guarantees ab the lifetime of the type, which is helpful

Copy link
Member

@Rjected Rjected left a comment

Choose a reason for hiding this comment

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

I would rather deal with unnecessary pass by value w.r.t. clones on a case by case basis. While this can be helpful, it is still in the pedantic group of clippy lints. I personally prefer to have more control on APIs, since it sometimes makes sense to use pass by value

My preference would be to not add this lint, and instead only change the signature in places where we do have unnecessary clones or copies. This lint can be used locally anyways to catch these types of issues

Comment on lines +258 to +260
pub fn check_and_decrypt(self, keys: &RLPxSymmetricKeys) -> Result<&'a mut [u8], ECIESError> {
self.check_integrity(keys)?;
Ok(self.decrypt(keys))
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer this consume the keys for example

@tcoratger
Copy link
Contributor Author

I would rather deal with unnecessary pass by value w.r.t. clones on a case by case basis. While this can be helpful, it is still in the pedantic group of clippy lints. I personally prefer to have more control on APIs, since it sometimes makes sense to use pass by value

My preference would be to not add this lint, and instead only change the signature in places where we do have unnecessary clones or copies. This lint can be used locally anyways to catch these types of issues

@Rjected I have the impression that if we do it on a case by case basis it will be difficult to maintain and very difficult for us to find the faulty elements, right?

Also I would say that if we apply the lint locally, this will slightly destructure our current classification which aims to apply the lints globally by registering them in the root cargo.toml right?

@emhane
Copy link
Member

emhane commented Jul 3, 2024

closing, @tcoratger feel free to recycle some of the code to change signatures in places identified as meaningful to pass by ref, for example

  • PeerId, TxHash and maybe other Copy types bigger than usize as param, when not cloned in function body
  • returning references in getters and cloning in place when needed, rather than cloning by default

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.

clippy: add clippy::needless_pass_by_value warn rule
4 participants