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

Fix4894 Using Into and TryInto instead of From and TryFrom #6620

Closed
wants to merge 28 commits into from

Conversation

xiongmao86
Copy link
Contributor

@xiongmao86 xiongmao86 commented Jan 22, 2021

If you added a new lint, here's a checklist for things that will be
checked during review or continuous integration.

  • Followed [lint naming conventions][lint_naming]
  • Added lint documentation

check again after every change:

  • cargo test passes locally
  • Executed cargo dev update_lints
  • Run cargo dev fmt
  • Added passing UI tests (including committed .stderr file)

Fixes #4894. Using Into and TryInto instead of using From and TryFrom.

Please write a short comment explaining your change (or "none" for internal only changes)
changelog:
Using Into and TryInto instead of using From and TryFrom.

@rust-highfive
Copy link

r? @phansch

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 22, 2021
@xiongmao86
Copy link
Contributor Author

Oh, this is a draft for discussion, how can I disable r?

@xiongmao86 xiongmao86 changed the title Fix4894 Fix4894 Using Into and TryInto instead of From and TryFrom Jan 22, 2021
@xiongmao86
Copy link
Contributor Author

r? @flip1995
Can you take a look when you have time?

@rust-highfive rust-highfive assigned flip1995 and unassigned phansch Jan 22, 2021
@flip1995 flip1995 added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jan 22, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Jan 27, 2021
…jasper

Make more traits of the From/Into family diagnostic items

Following traits are now diagnostic items:
- `From` (unchanged)
- `Into`
- `TryFrom`
- `TryInto`

This also adds symbols for those items:
- `into_trait`
- `try_from_trait`
- `try_into_trait`

Related: rust-lang/rust-clippy#6620 (comment)
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Jan 27, 2021
…jasper

Make more traits of the From/Into family diagnostic items

Following traits are now diagnostic items:
- `From` (unchanged)
- `Into`
- `TryFrom`
- `TryInto`

This also adds symbols for those items:
- `into_trait`
- `try_from_trait`
- `try_into_trait`

Related: rust-lang/rust-clippy#6620 (comment)
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jan 28, 2021
…jasper

Make more traits of the From/Into family diagnostic items

Following traits are now diagnostic items:
- `From` (unchanged)
- `Into`
- `TryFrom`
- `TryInto`

This also adds symbols for those items:
- `into_trait`
- `try_from_trait`
- `try_into_trait`

Related: rust-lang/rust-clippy#6620 (comment)
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Jan 29, 2021
Make more traits of the From/Into family diagnostic items

Following traits are now diagnostic items:
- `From` (unchanged)
- `Into`
- `TryFrom`
- `TryInto`

This also adds symbols for those items:
- `into_trait`
- `try_from_trait`
- `try_into_trait`

Related: rust-lang#6620 (comment)
@flip1995
Copy link
Member

@xiongmao86 You now should also have access to the diagnostic items for try_from_trait (and to both Into traits if needed). Implementation LGTM otherwise.

@xiongmao86
Copy link
Contributor Author

@flip1995 Thanks for the guidance, I found toolchain update today and I git pull upstream master, and after git rebase master fix4894, I can't git push -u origin fix4894.

The error message is:

 ! [rejected]            fix4894 -> fix4894 (non-fast-forward)
error: failed to push some refs to 'https://github.com/xiongmao86/rust-clippy.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

and git branch -v says fix4894 is [ahead 243, behind 6].

What is the problem?

@flip1995
Copy link
Member

flip1995 commented Jan 31, 2021

When you rebase, you have to force push. Force pushing deletes your previous commits from the repository and you can't get them back (well you can if you still have them locally, but that requires much more git-fu), so you should be careful.

If your rebase goes through without any conflicts (which I assume is the case here) force pushing is usually save after a rebase. So git push -u origin fix4894 --force-with-lease should solve your problem. (Always use --force-with-lease instead of -f or --force, it gives you another safeguard).

@xiongmao86
Copy link
Contributor Author

@flip1995 , May be I mistake -f for -u. The git docs says "-u" means adding upstream reference. I don't really understand, what is it used for in this case?

@xiongmao86
Copy link
Contributor Author

@flip1995, I was thinking maybe I expressed this item in the wrong way, maybe the name should be FromInsteadOfInto, so I can said "allow FromInsteadOfInto"? Is it right? I am not sure?

@xiongmao86
Copy link
Contributor Author

xiongmao86 commented Feb 1, 2021

And about the -u option, I read the branch.<name>.merge part of git config, there are a lot concept to make clear, so I don't know how to ask properly.

The description is:

-u
--set-upstream
For every branch that is up to date or successfully pushed, add upstream (tracking) reference, 
used by argument-less git-pull(1) and other commands. For more information, see branch.<name>.merge 
in git-config(1).

And I see now the point is to understand the concept of upstream(tracking) reference, could you tell me what is it, or could you show me the way to make sense of it?

@flip1995
Copy link
Member

flip1995 commented Feb 1, 2021

The git docs says "-u" means adding upstream reference. I don't really understand, what is it used for in this case?

So git push needs an address to know where to push the commits. So git push is actually just a shortcut for git push <remote> <branch> (git push origin fix4894). To be able to use this shortcut you first have to tell git about the address it should use for this shortcut. You do this with -u aka --set-upstream. You know if you have set this shortcut if you see the line

Your branch is up-to-date with 'origin/master'.

when running git status.

I was thinking maybe I expressed this item in the wrong way, maybe the name should be FromInsteadOfInto, so I can said "allow FromInsteadOfInto"? Is it right? I am not sure?

I'm not really sure what you mean. The name of the struct does not really matter for the lint. The lint name comes from the all-caps name in the declare_clippy_lint! macro.

@flip1995
Copy link
Member

flip1995 commented Feb 1, 2021

And I see now the point is to understand the concept of upstream(tracking) reference, could you tell me what is it, or could you show me the way to make sense of it?

You can understand upstream as the online repository vs your local checkout on your computer.

@xiongmao86
Copy link
Contributor Author

xiongmao86 commented Feb 1, 2021

@flip1995, thanks for your explanation, I see what -u is using now, it's for shortcut using.

About the lint name, I have not been clear. I used to write lint name IntoInsteadOfFrom Into in front and From at last, but I think FromInsteadOfInto, From in front, and Into at last would be more appropriate.

And to read#[allow(from_instead_of_into)] as "allow using from trait instead of using into trait". If the lint wouldn't be triggered if we allow it. Is this understanding correct?

@xiongmao86
Copy link
Contributor Author

I think I may have made a mistake when I think of naming the lint. I want to make sure the naming is right.

@flip1995
Copy link
Member

flip1995 commented Feb 2, 2021

Ah good catch. Yeah from_instead_of_into is the better name 👍

Comment on lines +8 to +11
help: remove From bound
|
LL | ,
| --
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is probably not the best diagnostic message, From<T> is removed and only comma left.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I don't even know if that still compiles. I think you have to track how many bounds there are and tweak the suggestion depending on this.

Comment on lines +12 to +16
help: Add this bound predicate
|
LL | u32: From<T>T: Into<u32>,
| ^^^^^^^^^^^^

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made a mistake here. Didn't notice until now. I'll fix this.

| --
help: Add this bound predicate
|
LL | u32: Copy + Clone + From<T>, T: Into<u32>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this probably should be u32: Copy + Clone, T: Into<u32>, but I don't know how to remove From showing in this span.

Copy link
Member

Choose a reason for hiding this comment

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

You have to get the span of the From<T> bound and extend it to the end of the span of the Clone bound with span.with_lo(clone_bound_span.hi()) And as the suggestion you can just use String::new(), which will automatically suggest removing it.

@xiongmao86
Copy link
Contributor Author

@flip1995, are you suggesting using multispan_sugg?

@bors
Copy link
Collaborator

bors commented Feb 28, 2021

☔ The latest upstream changes (presumably #6787) made this pull request unmergeable. Please resolve the merge conflicts.

@flip1995
Copy link
Member

You can use multispan_sugg or just add 2 suggestions, by calling diag.span_suggestion 2 times with different spans and suggestions. I would first try the latter. If that doesn't work or looks weird, I would try mutlispan_sugg.

@flip1995
Copy link
Member

Sorry for being absent for so long. I currently can't really keep up with my reviews...

@flip1995 flip1995 added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Mar 19, 2021
@xiongmao86
Copy link
Contributor Author

Sorry, @flip1995, seems github.com is ban again at my location. I am trying to get to github.com(read prs, push commits etc.) in a secure way.

@xiongmao86
Copy link
Contributor Author

Sorry for being absent for so long. I currently can't really keep up with my reviews...

Thank you very much for your help. Please take your time.

@giraffate
Copy link
Contributor

ping from triage @xiongmao86. Can you have any update on this?

@xiongmao86
Copy link
Contributor Author

@giraffate, I recently don't have time for this, if you want to take over, please go ahead.

@giraffate
Copy link
Contributor

ping from triage @xiongmao86. According to the triage procedure, I'm closing this because 2 weeks have passed with no activity.

@giraffate giraffate closed this Apr 28, 2021
@giraffate giraffate added S-inactive-closed Status: Closed due to inactivity and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Apr 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive-closed Status: Closed due to inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New lint: Use Into/TryInto in bounds as opposed to From/TryFrom
6 participants