Skip to content

fix: manual_unwrap_or_default suggests error when expression is a None variant #12688

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

Closed

Conversation

t-webber
Copy link

Suggesting error when expression is none : None.unwrap_or_default() raises an error, as explained in the following issue:

fixes #12670

changelog: [manual_unwrap_or_default]: the lint is ignored when matching None, as the unwrap_or_default method can't infer the type of a None variant.

@rustbot
Copy link
Collaborator

rustbot commented Apr 17, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Jarcho (or someone else) some time within the next two weeks.

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 Apr 17, 2024
@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Apr 18, 2024
@rustbot
Copy link
Collaborator

rustbot commented Apr 18, 2024

There are merge commits (commits with multiple parents) in your changes. We have a no merge policy so these commits will need to be removed for this pull request to be merged.

You can start a rebase with the following commands:

$ # rebase
$ git rebase -i master
$ # delete any merge commits in the editor that appears
$ git push --force-with-lease

The following commits are merge commits:

@t-webber t-webber force-pushed the fix_manual_unwrap_or_default branch from dcaf974 to 1fd8241 Compare April 18, 2024 10:38
@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Apr 18, 2024
@rustbot
Copy link
Collaborator

rustbot commented Apr 18, 2024

There are merge commits (commits with multiple parents) in your changes. We have a no merge policy so these commits will need to be removed for this pull request to be merged.

You can start a rebase with the following commands:

$ # rebase
$ git rebase -i master
$ # delete any merge commits in the editor that appears
$ git push --force-with-lease

The following commits are merge commits:

@t-webber t-webber force-pushed the fix_manual_unwrap_or_default branch 2 times, most recently from 988b08b to c6167c3 Compare April 18, 2024 19:06
@t-webber t-webber force-pushed the fix_manual_unwrap_or_default branch from c6167c3 to c9f0374 Compare April 18, 2024 21:22
@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Apr 18, 2024
@bors
Copy link
Contributor

bors commented Jun 7, 2024

☔ The latest upstream changes (presumably #12897) made this pull request unmergeable. Please resolve the merge conflicts.

Comment on lines +152 to +155
if let Some(none_def_id) = cx.tcx.lang_items().option_none_variant() {
if let ExprKind::Path(QPath::Resolved(_, path)) = &condition.kind {
if let Some(def_id) = path.res.opt_def_id() {
if cx.tcx.parent(def_id) == none_def_id {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is equivalent to path_res(cx, condition).opt_def_id().is_some_and(|id| Some(cx.tcx.parent(id)) == cx.tcx.lang_items().option_none_variant())

Comment on lines +170 to +185
// We check if the expression is not a method or function with a unspecified return type
if let ExprKind::MethodCall(_, expr, _, _) = &condition.kind {
if let ty::Adt(_, substs) = cx.typeck_results().expr_ty(expr).kind() {
if let Some(ok_type) = substs.first() {
return span_lint_and_sugg(
cx,
MANUAL_UNWRAP_OR_DEFAULT,
expr.span,
format!("{expr_name} can be simplified with `.unwrap_or_default()`"),
format!("explicit the type {ok_type} and replace your expression with"),
format!("{receiver}.unwrap_or_default()"),
Applicability::Unspecified,
);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are you trying to catch with this? This looks like it will just catch any scrutinee that happens to be a method call.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I catch all methods and functions that return a generic type that can't be inferred from it's arguments (e.g. "1".parse()) ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have clippy_utils::ty::expr_type_is_certain for that.

expr.span,
format!("{expr_name} can be simplified with `.unwrap_or_default()`"),
"replace it with",
format!("{receiver}::</* Type */>.unwrap_or_default()"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can print any Ty which will print the type's full name. The following should work:

if let ty::Adt(_, args) = cx.typeck_results().expr_ty(expr).kind()
  && let Some(arg) = args.first()
  && let Some(arg_ty) = arg.as_type()
{
  format!("{receiver}::<{}>.unwrap_or_default()", arg_ty)
}

@Jarcho
Copy link
Contributor

Jarcho commented Jul 3, 2024

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jul 3, 2024
@xFrednet
Copy link
Member

xFrednet commented Aug 3, 2024

Hey this is triage, I'm closing this due to inactivity. If you want to continue this implementation, you're welcome to create a new PR. Thank you for the time, you already put into this!

Interested parties are welcome to pick this implementation up as well :)

@rustbot label +S-inactive-closed -S-waiting-on-author -S-waiting-on-review

@xFrednet xFrednet closed this Aug 3, 2024
@rustbot rustbot added the S-inactive-closed Status: Closed due to inactivity label Aug 3, 2024
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Aug 3, 2024
@profetia
Copy link
Contributor

Hi forks! I would like to pick up this implementation, looks like there aren't much left.

github-merge-queue bot pushed a commit that referenced this pull request Feb 5, 2025
…is uncertain (#13889)

fixes #12670

Continuation of #12688. r? @Jarcho if you don't mind?

changelog: [`manual_unwrap_or_default`] fix wrong suggestions when
condition type is uncertain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive-closed Status: Closed due to inactivity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

manual_unwrap_or_default suggested fix fails to compile due to missing type annotation
8 participants