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

redundant_clone in Rc context ? #4982

Closed
o0Ignition0o opened this issue Jan 2, 2020 · 8 comments
Closed

redundant_clone in Rc context ? #4982

o0Ignition0o opened this issue Jan 2, 2020 · 8 comments
Assignees
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@o0Ignition0o
Copy link

o0Ignition0o commented Jan 2, 2020

We had a small discussion on it here: mozilla/neqo#374 (comment).
I wonder if we could tell a user to use Rc::clone(&foo) if the linter warns about a redundant clone in Rc context ?

If so I would love to work on it, but that would be my first PR to clippy, so it would probably require someone to mentor me.

Thanks for your time :)

@flip1995
Copy link
Member

flip1995 commented Jan 4, 2020

The lint is implemented in redundant_clone.rs, so somewhere in there Rc could be special cased. Probably somewhere inside this if:

Since @sinkuu completely rewrote this lint, maybe he can give a little bit more insight.


For how to write tests in Clippy, you can read this part of the documentation: https://github.com/rust-lang/rust-clippy/blob/master/doc/adding_lints.md#testing

@flip1995 flip1995 added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages L-suggestion Lint: Improving, adding or fixing lint suggestions labels Jan 4, 2020
@sinkuu
Copy link
Contributor

sinkuu commented Jan 4, 2020

The type of the value being .clone()d is available here as arg_ty, so you could add something like if is_rc(arg_ty) { continue; } below that.

let (fn_def_id, arg, arg_ty, _) = unwrap_or_continue!(is_call_with_ref_arg(cx, mir, &terminator.kind));

I think Rc::clone suggestion is orthogonal to this lint and should be a new lint.

@o0Ignition0o
Copy link
Author

The type of the value being .clone()d is available here as arg_ty, so you could add something like if is_rc(arg_ty) { continue; } below that.

Pretty cool I'll try to work on it tomorrow or next week :D

I think Rc::clone suggestion is orthogonal to this lint and should be a new lint.

Would it make sense to create a redundant_rc_clone.rs file ?
maybe that ought to be part of an other commit / PR ?

@sinkuu
Copy link
Contributor

sinkuu commented Jan 4, 2020

redundant_rc_clone

Do you mean to suggest Rc::clone only when it is actually redundant? Then it may be fine to include the feature in redundant_clone.

(I don't see the point in doing so though. I was thinking of a lint that replaces all rc.clone() with Rc::clone(&rc)...)

@o0Ignition0o
Copy link
Author

Do you mean to suggest Rc::clone only when it is actually redundant? Then it may be fine to include the feature in redundant_clone.

That's what I initially thought of but the lint you mention (replacing rc.clone() with Rc::clone(&rc)) would be a great improvement IMO, it would make things explicit, and still allow redundant_clone to do its job in RC context.

I'm however not aware of the actual process to discuss a new lint and create a consensus among the clippy contributors ^^'.

@flip1995
Copy link
Member

flip1995 commented Jan 6, 2020

I'm however not aware of the actual process to discuss a new lint and create a consensus among the clippy contributors

You can do 2 things:

  1. Open an issue for a (style) lint rc.clone() -> Rc::clone(&rc) and wait for someone to take this issue or
  2. Just implement it yourself and refer to this issue.

If you decide to do (2.) and need any help with that, just ask here or open a WIP PR :)

About the consensus: If people disagree with the lint, we can still move it to a allow-by-default group.

@o0Ignition0o
Copy link
Author

I would love to try to implement it myself !

I have a couple of things to finish before I tackle it though, but feel free to assign me to it :)

@lengyijun
Copy link
Contributor

clone_on_ref_ptr lints on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

No branches or pull requests

5 participants