-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
New lint: Use Into
/TryInto
in bounds as opposed to From
/TryFrom
#4894
Comments
@Kixunil Why is it that
|
It's possible to implement |
Thanks, @Kixunil . |
As far as I remember, several months ago coherence checking rules were improved, and now there is no case when |
I think there's still the case where you want to implement a Source: https://doc.rust-lang.org/std/convert/trait.Into.html |
|
@blasrodri The point of this issue is to lint bounds, not impls. I guess another lint could be created for impls. (Basically lint whether the code is written according to what the 1.41 docs says.) |
If no one is working on this, I'll take a stab. |
The history of rust-clippy on the component history pages shows that the cilippy is successfully build from 05-30 to 06-02 today. But I have failed to compile the master of 5-31 with nightly-2020-05-31, a commit which pass ci test at that day. I have also tried 5-30's nightly and also failed. This is really strange, since both ci test show passed when I look at the pr of the commit that I tried to compile. And the compile of my fork master update to that remote master is also failed. Does anybody have idea about what's going on, what makes compile failed? I am really curios why this would happen. Right now I am trying to rustup to the 06-02 to see if I can build the master. But I suspect that would be failing too. |
@xiongmao86 Clippy is now a subtree in the rust repo, which effectively means, that Clippy will never show up as "missing" on this page again. It may be the case, that some commits from the rust repo have to be copied/synced to the Clippy repo. See https://github.com/rust-lang/rust-clippy/blob/master/CONTRIBUTING.md#fixing-build-failures-caused-by-rust for the new process. We're currently looking in pinning a nightly, but aren't quite there yet: #5561 |
@flip1995, Thanks for replying, I understand subtree is said to be the new way recommended against submodule. I vaguely grabbed the idea. But I couldn't find articles discussing this in detail. I am interesting in why clippy is decided to transit to subtree way. But I didn't find discussion by searching on this repo. And what is the impact on the way of development: after pinning to the specific version of nightly, I am very happy that I won't need proxy to download rustc, rust-std etc. and the mirror in my country can really speed up my download. But I not sure do we still need to make "rustup" pr, do we still have to change clippy to catch up the change happen in rustc-dev? Or do we shift the responsibility to rustc developers, and here we only focus on lints, and if failed to build, we sync the change? |
@xiongmao86 This is the issue about moving to subtrees: rust-lang/rust#70651 TL;DR: with subtrees rustc contributors have to deal with fixing the Clippy build in the rustc repo, while folks who just want to contribute to Clippy and not clone/compile rustc, can still do this in this repo.
Yes, currently we have to do a subtree sync for every build failure, since we're still testing with rustc master toolchain. In the future, we want to pin a nightly, which would mean that Clippy development is possible with just rustup (instead of RTIM, which is currently used to install the master toolchain). Internally, we then also can sync whenever we want, and don't have pressure to sync every time a rustc PR broke Clippy. |
Thanks, I'll digest the information. |
@xiongmao86 Yes, Clippy now compiles with the nightly specified in |
@flip1995, I am not sure which pass to choose, early lint or late lint? |
If I can use late lint pass, this lint would cover wider range of code, by I am not sure it is solvable using late lint. |
You will probably have to use a late pass, to make sure that the bound is actually talking about the |
@flip1995, do I need to specify a minimum supported rust version? In what case should I specify one? |
An MSRV only has to be added if you produce a suggestion, that includes code, that is not available in early Rust versions. I don't think this is necessary for this lint. |
Linting bounds should be independent of rustc version, since changing Linting impl should depend on version >= 1.41, see https://doc.rust-lang.org/std/convert/trait.Into.html#implementing-into-for-conversions-to-external-types-in-old-versions-of-rust |
You will have to check this with |
I ran into a message generated by this in a case where both Into and TryFrom are implemented for B. When this is the case, it is a good indication that you can go from B -> A, but not always from A -> B and the warning isn't needed. |
@Allen-Webb Thanks for noting that! Do you have a specific code example where you got this message? The lint in this issue isn't in Clippy yet, but WIP. Maybe it's another bug in Clippy? If you can't share the code or a minimal reproducer just the message you got would already help. |
Sounds like what @Allen-Webb describes is something like this: struct ValidatedString(String);
impl TryFrom<String> for ValidatedString {
...
}
impl Into<String> or ValidatedString {
...
} In this case the lint would be correct and the fix is to change last impl to this: impl From<ValidatedString> for String {
...
} |
This makes sense. My mistake. In that case it might be helpful if the recommendation actually printed the type so it was explicitly obvious (implied). Here is an example error:
I would replace I haven't looked at the code that generates the warnings, so I don't know the difficulty involved, but a better message might look something like:
|
Yeah, maybe even:
|
Thanks! This is indeed in another lint. I opened a new issue about this: #7088 |
When writing generic bounds such as:
Into
should be preferred, like this:Why the former is bad:
Into
is a superset ofFrom
. In some cases coherence rules prevent implementingFrom
but do allow implementingInto
. As a resultInto
is more generic bound thanFrom
.Category: Style, I guess?
The text was updated successfully, but these errors were encountered: