Skip to content
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

Merged
merged 9 commits into from Feb 9, 2024

Conversation

roife
Copy link
Contributor

@roife roife commented Jan 1, 2024

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

@rustbot
Copy link
Collaborator

rustbot commented Jan 1, 2024

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 (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 1, 2024
@roife roife changed the title Fix issue #12034: add autofix for unnecessary_fallible_conversions Fix issue #12034: add autofixes for unnecessary_fallible_conversions Jan 1, 2024
@roife roife requested a review from m-rph January 9, 2024 08:38
Copy link
Contributor

@m-rph m-rph left a 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

Comment on lines 73 to 80
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"),
});
Copy link
Contributor

@m-rph m-rph Jan 15, 2024

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!

Copy link
Contributor Author

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::TryFromFunctions and FunctionKind::TryIntoFunctions) and made the code more complicated

Copy link
Contributor

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?

@rustbot
Copy link
Collaborator

rustbot commented Jan 15, 2024

Error: Parsing assign command in comment failed: ...'' | error: specify user to assign to at >| ''...

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@rustbot rustbot assigned Centri3 and unassigned Alexendoo Jan 15, 2024
Copy link
Member

@Centri3 Centri3 left a 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

@roife
Copy link
Contributor Author

roife commented Feb 2, 2024

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.

Copy link
Member

@Centri3 Centri3 left a 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

@roife roife requested a review from Centri3 February 4, 2024 16:41
@roife roife requested a review from Centri3 February 9, 2024 06:48
@Centri3
Copy link
Member

Centri3 commented Feb 9, 2024

@bors r+

ty for your work on this!

@bors
Copy link
Collaborator

bors commented Feb 9, 2024

📌 Commit 3c76b2c has been approved by Centri3

It is now in the queue for this repository.

bors added a commit that referenced this pull request Feb 9, 2024
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
@bors
Copy link
Collaborator

bors commented Feb 9, 2024

⌛ Testing commit 3c76b2c with merge 9c2373d...

@bors
Copy link
Collaborator

bors commented Feb 9, 2024

💔 Test failed - checks-action_test

@Centri3
Copy link
Member

Centri3 commented Feb 9, 2024

@bors
Copy link
Collaborator

bors commented Feb 9, 2024

⌛ Testing commit 3c76b2c with merge 28443e6...

@bors
Copy link
Collaborator

bors commented Feb 9, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Centri3
Pushing 28443e6 to master...

@bors bors merged commit 28443e6 into rust-lang:master Feb 9, 2024
5 checks passed
@roife roife deleted the fix/issue-12034 branch February 15, 2024 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unnecessary_fallible_conversions should be fixable with cargo fix
6 participants