rust-lang / rust-lang/rust-clippy
Suggest implementing `TryFrom<&str>` for types implementing `FromStr`
Open
Nobody has claimed this yet.
A-lint
E-medium
L-style
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
It is not much work to implement TryFrom<&str> for types implementing FromStr, you can even generalize it;
impl<T> TryFrom<&str> for T
where
T: FromStr
{
type Error = T::Err;
fn try_from(input: &str) -> Result<Self, Self::Error> {
Self::from_str(input)
}
}
I don't think there is much of a reason to not implement it.
You might even be able to provide a more efficient implementation:
struct Hello<'a>(Cow<'a, str>);
impl FromStr for Hello<'static> {
fn from_str(input: &str) -> Result<Self, Self::Err> {
Ok(Self(Cow::Owned(input.into())))
}
}
impl<'a> TryFrom<&'a str> for Hello<'a> {
fn try_from(input: &'a str) -> Result<Self, Self::Error> {
Ok(Self(Cow::Borrowed(input)))
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no files, tests, or entry points. Start by evaluating whether the proposed TryFrom<&str> implementation belongs in Clippy or the Rust standard library, including the generic implementation and the borrowed Cow case. Define the intended behavior and resolve the design before implementation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100