Skip to content

Add lint for fallible impls of From #2143

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

Merged
merged 2 commits into from
Oct 23, 2017
Merged

Conversation

HMPerson1
Copy link
Contributor

Fixes #691.

Some notes:

  • I'm note sure about what to put for the error messages or documentation, so I would love any suggestions about that. Also maybe it should include a help string?
  • It reports the calls to begin_panic instead of the macro panic!() so it says note: this error originates in a macro outside of the current crate, which might be confusing.
  • It only detects calls to unwrap made with method-calling syntax; it won't detect UFCS syntax (i.e. Option::unwrap(foo)). AFAICT this applies to everything in methods.rs too.

@clarfonthey
Copy link

I disagree with this lint because FromStr is being deprecated in favour of From<&str>, TryFrom<&str>, and co., and I'd rather not move backwards.

@HMPerson1
Copy link
Contributor Author

HMPerson1 commented Oct 17, 2017

Well once TryFrom stabilized, it would be fairly easy to adapt this lint to say to use TryFrom instead of FromStr.

@clarfonthey
Copy link

IMHO From<String> is a very valid trait to implement under that, and the idea is that From here indicates an infalliable conversion. The point of moving away from FromStr is because it doesn't have that kind of guarantee.

@HMPerson1
Copy link
Contributor Author

Which is why this lint only fires when we see a panic!() or unwrap in the implementation of from, suggesting that the conversion is fallible. See the discussion in #691.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 17, 2017

We can make the lint allow-by-default and have it suggest TryFrom. That would not start suggesting a soon-to-be-deprecated trait, but also not suggest stuff to stable users that they can't use.

@HMPerson1 HMPerson1 changed the title Add lint for From<String> Add lint for fallible impls of From Oct 17, 2017
@HMPerson1 HMPerson1 force-pushed the master branch 5 times, most recently from 46bc95e to 165646f Compare October 21, 2017 00:40
@oli-obk
Copy link
Contributor

oli-obk commented Oct 23, 2017

I'd be willing to merge this as allow-by-default as it is, but to make it warn-by-default I'd like to run it on a bunch of big crates and see how it fares. Do you want to do this before merging, or do you want to make it allow?

@oli-obk
Copy link
Contributor

oli-obk commented Oct 23, 2017

Nevermind I'm stupid. It's already allow out of other reasons...

@oli-obk oli-obk merged commit b96639f into rust-lang:master Oct 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants