-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
added new lint owned_to_owned
#6730
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Manishearth (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
Should I move my implementation to |
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.
This is looking great! I know this PR is a draft but it seems nearly done to me 😄.
I did have to update the
redundant_clone
test to ignore this lint -- I felt that was the safest action.
Makes sense. And I think it's okay if those lints both fire on the same code.
You need tests for calling on a reference to a Vec
(or other). You will probably need input_type.peel_refs()
before calling same_type
.
I'm sorry to bikeshed even more but I'm having second thoughts on the lint name. How about implicit_clone
? It seems to me that the fact that we are dealing with owned types is not actually very relevant. The problem being linted is that the code is using a round-about way (deref + to_owned) to clone something rather than cloning explicitly. This lint might apply to a to_something
method that does not produce an "owned" type. Or, this lint could apply to usage of from
or into
to clone something. I think this name is more self-explanatory too.
if match_def_path(cx, meth_did, expected_path) && | ||
expected_type.map_or(true, |expected_type_path| match_type(cx,input_type,expected_type_path)); | ||
then { | ||
if is_copy(cx,return_type) { |
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 don't think there will ever be an "owned" type that implements Copy
.
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.
Nothing seems to prevent to_owned()
from being called on a custom type that implements Copy
(like the Kitten
type in my test), do we want this lint to fire on those cases?
Without this check, this will suggest replacing to_owned
with clone
, and then clone_on_copy
will suggest dropping the .clone()
entirely.
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.
Oh I see. It is technically possible but I think this situation is unusual enough that we shouldn't have a special case for it. The purpose of having a borrowed variant of a type is to avoid expensive copies. But if a type implements Copy
, then it shouldn't be expensive to copy. In any case, I think the lint should just fire normally because clone
is still better than to_owned
.
let input_type = cx.typeck_results().expr_ty(arg); | ||
if TyS::same_type(return_type, input_type); | ||
if match_def_path(cx, meth_did, expected_path) && | ||
expected_type.map_or(true, |expected_type_path| match_type(cx,input_type,expected_type_path)); |
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 believe the "expected type" check is redundant since you are verifying the method.
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.
For the current version here, I believe you are right but further changes in prep of using diagnostic items instead (including the ones I'm trying to get added in rust-lang/rust#82128) checks either the trait of the method (for to_owned
) or the return type (for to_vec
, …)
I can commit/push a new checkpoint if you'd like.
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 believe this is no longer redundant after some cleanup refactoring + moving to being able to use diagnostic items (including the ones I'm trying to have added in rust-lang/rust#82128)
I haven't committed/pushed that change yet, but I check if the method belongs to a trait_path or the type.
I'll be pushing my new changes once I've gone through everything else.
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 should clarify: when I say "to be able to use diagnostic items" I mean hopefully easily switch the function from taking paths to the function taking diagnostic items -- not a mix of both.
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.
checks either the trait of the method (for
to_owned
) or the return type (forto_vec
, …)
I think it would be better to just check the method for every case. If you only check the return type, then you might have a false positive if someone rolls their own trait with to_vec
with different semantics (even though that would be a little odd). If you just check the method, there should be no such risk.
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 guess by "check the method" while using diagnostic types, you mean "check the type/trait the method is on as well as checking the diagnostic item of return value of the type itself"?
Also, I just noticed os_string.to_os_string()
is actually a method on OsStr
so I will need to make sure that has a diagnostic item as well.
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.
"check the method":
- the method name is "to_vec"
- the method is associated with type X
- type X is diagnostic item "slice"
Not sure how to make the "associated" link but maybe tcx.associated_item
and/or tcx.impl_of_method
. We might need a util method to make this easier like is_diagnostic_assoc_fn(def_id, sym::slice, sym::to_vec)
.
Wouldn't that end up suggesting that even calling I did add a few (not-yet-committed) negative tests
That seems quite reasonable -- I'm not sure how GitHub handles tracking changes across renames, so will hold off on the rename until at least the next review pass. (I know git itself handles that sub-optimally unless you explicitly tell git to follow renames) |
I know it's been suggested (and I agree) that replacing I'm also happy waiting for those diagnostic items and doing that change before merging this, or am fine with this being merged before and then changing to the diagnostic items as soon as they are available. Whichever you prefer. |
Yes. I don't know why that would be any less valid. Both
A separate PR for that is a good idea. Getting the diagnostic items in this PR is also good. |
@bors delegate=camsteffen r? @camsteffen since you already started (thank you). Feel free to bounce the review back if you don't have time or want me to have a look too! |
✌️ @camsteffen can now approve this pull request |
… r=davidtwco add diagnostic items for OsString/PathBuf/Owned as well as to_vec on slice This is adding diagnostic items to be used by rust-lang/rust-clippy#6730, but my understanding is the clippy-side change does need to be done over there since I am adding a new clippy feature. Add diagnostic items to the following types: OsString (os_string_type) PathBuf (path_buf_type) Owned (to_owned_trait) As well as the to_vec method on slice/[T]
… r=davidtwco add diagnostic items for OsString/PathBuf/Owned as well as to_vec on slice This is adding diagnostic items to be used by rust-lang/rust-clippy#6730, but my understanding is the clippy-side change does need to be done over there since I am adding a new clippy feature. Add diagnostic items to the following types: OsString (os_string_type) PathBuf (path_buf_type) Owned (to_owned_trait) As well as the to_vec method on slice/[T]
129e2f4
to
1467261
Compare
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.
A few small things and then we'll ship it!
Very nice. Please squash into fewer commits and I'll merge. |
Oops I forgot one thing. Please change from |
a2cc8c5
to
3d3cfd3
Compare
Thanks! @bors r+ |
📌 Commit 3d3cfd3 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Adding new lint
owned_to_owned
Creating draft PR to have this looked over.
I think this takes all advice I received into account.
I did have to update the
redundant_clone
test to ignore this lint -- I felt that was the safest action.closes: #6715
changelog: added new lint
implicit_clone