suggest Option::as_deref(_mut) on type mismatch in option combinator if it passes typeck#111659
Merged
bors merged 2 commits intorust-lang:masterfrom Jun 3, 2023
Merged
suggest Option::as_deref(_mut) on type mismatch in option combinator if it passes typeck#111659bors merged 2 commits intorust-lang:masterfrom
Option::as_deref(_mut) on type mismatch in option combinator if it passes typeck#111659bors merged 2 commits intorust-lang:masterfrom
Conversation
Collaborator
|
r? @cjgillot (rustbot has picked a reviewer for you, use r? to override) |
cjgillot
reviewed
May 19, 2023
| //~^ ERROR expected a `FnOnce<(String,)>` closure, found `for<'a> unsafe fn(&'a str) -> Option<()> {takes_str_but_unsafe}` | ||
| let _ = produces_string().and_then(no_args); | ||
| //~^ ERROR function is expected to take 1 argument, but it takes 0 arguments | ||
| let _ = produces_string().and_then(generic_ref); |
Contributor
There was a problem hiding this comment.
Should this case suggest as_ref?
Member
Author
There was a problem hiding this comment.
I initially had it suggest as_ref() where applicable too, but there's already a help message for calling and_then with an fn(&T) on some Option<T>, though it suggests removing the borrow on the function parameter and I'm not sure how it would work out when there are two suggestions made by the compiler (is that fine?)
help: do not borrow the argument
|
14 - fn generic<T>(_: &T) {}
14 + fn generic<T>(_: T) {}
|
help: call `Option::as_ref()` first
|
18 | let x = produces_string().as_ref().and_then(generic);
| +++++++++
cjgillot
reviewed
Jun 3, 2023
compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
Member
Author
|
@rustbot ready |
Contributor
|
@bors r+ rollup |
Collaborator
matthiaskrgr
added a commit
to matthiaskrgr/rust
that referenced
this pull request
Jun 3, 2023
suggest `Option::as_deref(_mut)` on type mismatch in option combinator if it passes typeck Fixes rust-lang#106342. This adds a suggestion to call `.as_deref()` (or `.as_deref_mut()` resp.) if typeck fails due to a type mismatch in the function passed to an `Option` combinator such as `.map()` or `.and_then()`. For example: ```rs fn foo(_: &str) {} Some(String::new()).map(foo); ``` The `.map()` method requires its argument to satisfy `F: FnOnce(String)`, but it received `fn(&str)`, which won't pass. However, placing a `.as_deref()` before the `.map()` call fixes this since `&str == &<String as Deref>::Target`
This was referenced Jun 3, 2023
bors
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Jun 3, 2023
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#111659 (suggest `Option::as_deref(_mut)` on type mismatch in option combinator if it passes typeck) - rust-lang#111702 (Option::map_or_else: Show an example of integrating with Result) - rust-lang#111878 (Fix codegen test suite for bare-metal-like targets) - rust-lang#111969 (bootstrap: Make `clean` respect `dry-run`) - rust-lang#111998 (Add other workspaces to `linkedProjects` in rust_analyzer_settings) - rust-lang#112215 (only suppress coercion error if type is definitely unsized) - rust-lang#112231 (Make sure the build.rustc version is either the same or 1 apart (revised)) r? `@ghost` `@rustbot` modify labels: rollup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #106342.
This adds a suggestion to call
.as_deref()(or.as_deref_mut()resp.) if typeck fails due to a type mismatch in the function passed to anOptioncombinator such as.map()or.and_then().For example:
The
.map()method requires its argument to satisfyF: FnOnce(String), but it receivedfn(&str), which won't pass. However, placing a.as_deref()before the.map()call fixes this since&str == &<String as Deref>::Target