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
Fix issue #12034: add autofixes for unnecessary_fallible_conversions #12070
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Alexendoo (or someone else) soon. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, thank you for the auto-fixes!
I think we need to find another reviewer for this though 😅
@rustbot r? clippy
let qpath_spans = qpath.and_then(|qpath| match qpath { | ||
QPath::Resolved(_, path) => { | ||
let segments = path.segments.iter().map(|seg| seg.ident).collect::<Vec<_>>(); | ||
(segments.len() == 2).then(|| vec![segments[0].span, segments[1].span]) | ||
}, | ||
QPath::TypeRelative(_, seg) => Some(vec![seg.ident.span]), | ||
QPath::LangItem(_, _) => unreachable!("`TryFrom` and `TryInto` are not lang items"), | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit of a nit, and may be unnecessary, but if you return Option<[&'_ span]>
, I think the matching below could be cleaner as you will be matching something like
(FunctionKind::TryFromFunction, [from_method_span], Some(unwrap_span)) => todo!(),
(FunctionKind::TryFromFunction, [from_cls_span, from_method_span], Some(unwrap_span)) => todo!(),
_ => unreachable!()
which I think would make this a bit cleaner, other than that, I think it's very good and a great addition!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry it took me so long to see this comment. I tried this but it introduced too many arms (multiple FunctionKind::TryFromFunction
s and FunctionKind::TryIntoFunction
s) and made the code more complicated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I am good then, but I think we still need another reviewer with r perms. Maybe zulip?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take a closer look once I can. For now I have a few things to suggest
Thanks for your suggestions! I have refactored the code according to your advice: I removed the large match statement, split it into several parts, and separated them into various sub-functions, which made the logic of the code much clearer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just 2 nits, once dogfood passes this is ready :3
@bors r+ ty for your work on this! |
Fix issue #12034: add autofixes for unnecessary_fallible_conversions fixes #12034 Currently, the `unnecessary_fallible_conversions` lint was capable of autofixing expressions like `0i32.try_into().unwrap()`. However, it couldn't autofix expressions in the form of `i64::try_from(0i32).unwrap()` or `<i64 as TryFrom<i32>>::try_from(0).unwrap()`. This pull request extends the functionality to correctly autofix these latter forms as well. changelog: [`unnecessary_fallible_conversions`]: Add autofixes for more forms
💔 Test failed - checks-action_test |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
fixes #12034
Currently, the
unnecessary_fallible_conversions
lint was capable of autofixing expressions like0i32.try_into().unwrap()
. However, it couldn't autofix expressions in the form ofi64::try_from(0i32).unwrap()
or<i64 as TryFrom<i32>>::try_from(0).unwrap()
.This pull request extends the functionality to correctly autofix these latter forms as well.
changelog: [
unnecessary_fallible_conversions
]: Add autofixes for more forms