Add boxing suggestions for impl Trait return type mismatchesAdd boxing suggestions for impl Trait return type mismatches#155682
Open
Unique-Usman wants to merge 1 commit intorust-lang:mainfrom
Open
Conversation
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
Collaborator
|
r? @nnethercote rustbot has assigned @nnethercote. Use Why was this reviewer chosen?The reviewer was selected based on:
|
mejrs
requested changes
Apr 23, 2026
Contributor
There was a problem hiding this comment.
r? me
We already have a SuggestBoxingForReturnImplTrait suggestion.
Please use it instead. Or ideally, figure out why it's not being emitted in some cases, like here
// suggestion emitted
fn works() -> impl std::fmt::Display {
match 13 {
0 => 0i32,
_ => 1u32,
}
}
// suggestion not given
fn oh_no() -> impl std::fmt::Display {
match 13 {
0 => return 0i32,
_ => 1u32,
}
}error[E0308]: `match` arms have incompatible types
--> src/lib.rs:5:14
|
3 | / match 13 {
4 | | 0 => 0i32,
| | ---- this is found to be of type `i32`
5 | | _ => 1u32,
| | ^^^^ expected `i32`, found `u32`
6 | | }
| |_____- `match` arms have incompatible types
|
help: you could change the return type to be a boxed trait object
|
2 - fn works() -> impl std::fmt::Display {
2 + fn works() -> Box<dyn std::fmt::Display> {
|
help: if you change the return type to expect trait objects, box the returned expressions
|
4 ~ 0 => Box::new(0i32),
5 ~ _ => Box::new(1u32),
|
help: change the type of the numeric literal from `u32` to `i32`
|
5 - _ => 1u32,
5 + _ => 1i32,
|
error[E0308]: mismatched types
--> src/lib.rs:13:14
|
10 | fn oh_no() -> impl std::fmt::Display {
| ---------------------- expected a single type implementing `std::fmt::Display` because of return type
11 | match 13 {
12 | 0 => return 0i32,
| ---- return type resolved to be `i32`
13 | _ => 1u32,
| ^^^^ expected `i32`, found `u32`
|
help: you can convert a `u32` to an `i32` and panic if the converted value doesn't fit
|
14 | }.try_into().unwrap()
| ++++++++++++++++++++
For more information about this error, try `rustc --explain E0308`.
Collaborator
|
Reminder, once the PR becomes ready for a review, use |
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.
A sort of a follow up pr to this -> #155546