Skip to content

Commit fbc4168

Browse files
author
Duddino
committed
Provide better compiler output when using ? on Option in fn returning Result and vice-versa
1 parent ce994b6 commit fbc4168

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

src/librustc_trait_selection/traits/error_reporting/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
321321
err.span_suggestion_verbose(
322322
span.shrink_to_lo(),
323323
"consider converting the `Option<T>` into a `Result<T, _>` using `Option::ok_or` or `Option::ok_or_else`",
324-
".ok_or_else(|_| /* error value */)".to_string(),
324+
".ok_or_else(|| /* error value */)".to_string(),
325325
Applicability::HasPlaceholders,
326326
);
327327
} else if is_try && is_from && should_convert_result_to_option {
328328
err.span_suggestion_verbose(
329329
span.shrink_to_lo(),
330330
"consider converting the `Result<T, _>` into an `Option<T>` using `Result::ok`",
331331
".ok()".to_string(),
332-
Applicability::HasPlaceholders,
332+
Applicability::MachineApplicable,
333333
);
334334
}
335335

src/test/ui/option-to-result.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | a?;
88
= note: required by `std::convert::From::from`
99
help: consider converting the `Option<T>` into a `Result<T, _>` using `Option::ok_or` or `Option::ok_or_else`
1010
|
11-
LL | a.ok_or_else(|_| /* error value */)?;
12-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
LL | a.ok_or_else(|| /* error value */)?;
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1313

1414
error[E0277]: `?` couldn't convert the error to `std::option::NoneError`
1515
--> $DIR/option-to-result.rs:11:6

src/test/ui/try-on-option.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | x?;
88
= note: required by `std::convert::From::from`
99
help: consider converting the `Option<T>` into a `Result<T, _>` using `Option::ok_or` or `Option::ok_or_else`
1010
|
11-
LL | x.ok_or_else(|_| /* error value */)?;
12-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
LL | x.ok_or_else(|| /* error value */)?;
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1313

1414
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
1515
--> $DIR/try-on-option.rs:13:5

0 commit comments

Comments
 (0)