-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
suggest Option::as_deref(_mut)
on type mismatch in option combinator if it passes typeck
#111659
Conversation
r? @cjgillot (rustbot has picked a reviewer for you, use r? to override) |
//~^ 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); |
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.
Should this case suggest as_ref
?
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 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);
| +++++++++
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.
One nit and r=me.
@bors rollup
compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
Outdated
Show resolved
Hide resolved
@rustbot ready |
@bors r+ rollup |
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`
…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
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 anOption
combinator 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