Skip to content

Suggest .as_ref() when appropriate for Option and Result #57158

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
Dec 30, 2018

Conversation

estebank
Copy link
Contributor

Fix #55198.

@rust-highfive
Copy link
Contributor

r? @zackmdavis

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 28, 2018
@zackmdavis
Copy link
Member

(hope to catch up on reviews Saturday the twenty-ninth)

@Xanewok
Copy link
Member

Xanewok commented Dec 28, 2018

I know I missed this sorely when I was at the beginning of my Rust journey. Many thanks for working on this!

@estebank estebank force-pushed the as-ref branch 2 times, most recently from a945167 to 1c31447 Compare December 28, 2018 18:03
@rust-highfive

This comment has been minimized.

let mut show_suggestion = true;
for (exp_ty, found_ty) in exp_substs.types().zip(found_substs.types()) {
if let TyKind::Ref(_, exp_ty, _) = exp_ty.sty {
match (&exp_ty.sty, &found_ty.sty) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would like to replace this entire match with FnCtxt::can_coerce, but first I have to find a way to get ahold of a FnCtxt here :-|

@petrochenkov when you have the time, could you point me in the right direction for this? I've noticed that we have same_tys and same_type which don't do exactly the same thing, but are pretty similar...

Copy link
Member

@zackmdavis zackmdavis left a comment

Choose a reason for hiding this comment

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

I have some quibbles, but I don't think they're actually blocking (as opposed to providing more pressure towards things like "We should really do that error-message language audit someday")

LL | opt.map(|arg| takes_ref(arg));
| - ^^^ expected &Foo, found struct `Foo`
| |
| help: consider using `as_ref` instead: `as_ref().`
Copy link
Member

Choose a reason for hiding this comment

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

as_ref(). (trailing dot!) as the suggestion text faked me out for a moment; our insertion suggestions can look kind of awkward sometimes. (But making the span cover opt.map and suggesting opt.as_ref().map is ugly in its own way.)

In any case, the code that sets this was actually introduced in #51100, not here, so it doesn't affect the outcome of this review.

if let TyKind::Adt(found_def, found_substs) = found_ty.sty {
let path_str = format!("{:?}", exp_def);
if exp_def == &found_def {
let opt_msg = "you can convert from `&Option<T>` to `Option<&T>` using \
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let opt_msg = "you can convert from `&Option<T>` to `Option<&T>` using \
let opt_msg = "`&Option<T>` can be converted to `Option<&T>` using \

I lean against the second person in diagnostic messages. (But that's my opinion; we haven't codifed that anywhere.)

match (&exp_found.expected.sty, &exp_found.found.sty) {
(TyKind::Adt(exp_def, exp_substs), TyKind::Ref(_, found_ty, _)) => {
if let TyKind::Adt(found_def, found_substs) = found_ty.sty {
let path_str = format!("{:?}", exp_def);
Copy link
Member

Choose a reason for hiding this comment

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

Relying on debug output for functionality seems like poor form? (On the grounds that it's meant for debugging convenience and should be subject to change rather than guaranteed stable, even if it happens to be probably-stable in this case.)

@@ -972,6 +973,72 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
self.note_error_origin(diag, &cause);
}

/// When encountering a case where `.as_ref()` on a `Result` or `Option` would be appropriate,
/// suggest it.
fn suggest_as_ref_where_appropriate(
Copy link
Member

Choose a reason for hiding this comment

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

I like _where_appropriate as part of a function name, but I don't think I've seen it used anywhere else, and I know we have a lot of these "conditionally set a diagnostic" functions ...

@zackmdavis
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Dec 30, 2018

📌 Commit 0ecc128 has been approved by zackmdavis

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 30, 2018
@bors
Copy link
Collaborator

bors commented Dec 30, 2018

⌛ Testing commit 0ecc128 with merge 499a5af507151593ba0de1f9bc566147e179392a...

@bors

This comment has been minimized.

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Dec 30, 2018
@rust-highfive

This comment has been minimized.

@estebank
Copy link
Contributor Author

@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 30, 2018
@bors
Copy link
Collaborator

bors commented Dec 30, 2018

⌛ Testing commit 0ecc128 with merge 7155690...

bors added a commit that referenced this pull request Dec 30, 2018
Suggest `.as_ref()` when appropriate for `Option` and `Result`

Fix #55198.
@bors
Copy link
Collaborator

bors commented Dec 30, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: zackmdavis
Pushing 7155690 to master...

@bors bors merged commit 0ecc128 into rust-lang:master Dec 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants