Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upLint against using generic conversion traits when concrete methods are available #36443
Comments
sfackler
added
the
A-lint
label
Sep 13, 2016
sfackler
referenced this issue
Sep 13, 2016
Closed
Regression in Beta and Nightly in type inference #36352
This comment has been minimized.
This comment has been minimized.
|
Makes sense to me. |
brson
added
T-lang
E-easy
C-enhancement
labels
Sep 20, 2016
sfackler
referenced this issue
Sep 26, 2016
Merged
Revert "implement `From<Vec<char>>` and `From<&'a [char]>` for `String`" #36685
This comment has been minimized.
This comment has been minimized.
|
This sounds like a good idea, but I'm not sure what an implementation of this would look like. is the idea just to annotate things like pub fn new<S: AsRef<str>>(somedata: S) -> String {
String::from(somedata.as_ref())
}could be changed to pub fn new<S: AsRef<str>>(somedata: S) -> String {
String::from(AsRef::<str>::as_ref(&somedata))
}This requires no annotations, but I believe it provides the same functionality. One drawback of this approach is that it guides users towards uglier (more verbose) code, but I think this is a worthwhile trade-off, especially considering that veteran users will probably still reach for Edit: Changed the second code block slightly to make it actually compile. |
This comment has been minimized.
This comment has been minimized.
|
@cramertj That code is unproblematic and does not need further annotations -- the explicit trait bound The issue is code more like this: |
This comment has been minimized.
This comment has been minimized.
|
@bluss The code I used in the example above was reported as causing Either way, a similar recommendation could be made for the example you provided. |
This comment has been minimized.
This comment has been minimized.
|
This is not E-easy. |
Mark-Simulacrum
removed
the
E-easy
label
May 15, 2017
This comment has been minimized.
This comment has been minimized.
|
New lints require an RFC these days; additionally, there's been no comments in two years, so there doesn't seem to be a ton of interest at this time. Closing! |
sfackler commentedSep 13, 2016
•
edited
The standard library's backwards compatibility rules allow the addition of new trait implementations even though this can cause previously working code to hit type inference failures in some contexts. Generic conversion traits (
From,AsRef,Borrow, etc) frequently come up in regression reports since they're commonly implemented multiple times for a given type.Because of this, the standard library commonly provides concrete methods with identical functionality which can be used to avoid these kinds of issues. For example, the
String::into_bytesmethod has an equivalent signature to theInto::<Vec<u8>>::intomethod onString.For example, the code in #36352 passed the result of the
Borrow::borrowmethod into theFrom::frommethod, which failed to type infer after a secondFromimplementation was added toString. This could have been avoided if the concrete.as_bytes()method was used instead of.borrow().We should consider adding a lint, probably backed by annotations on the impls, which warn against calling the generic conversion trait methods in concrete contexts when a concrete method is available.
cc @rust-lang/libs