@@ -254,6 +254,18 @@ pub trait Try: FromResidual {
254254 label = "this `?` produces `{R}`, which is incompatible with `{Self}`" ,
255255 enclosing_scope = "this function returns a `Result`"
256256 ) ,
257+ on(
258+ all(
259+ from_method = "from_residual" ,
260+ from_desugaring = "QuestionMark" ,
261+ _Self = "std::option::Option<T>" ,
262+ R = "std::result::Result<T, E>" ,
263+ ) ,
264+ message = "the `?` operator can only be used on `Option`s, not `Result`s, \
265+ in {ItemContext} that returns `Option`",
266+ label = "use `.ok()?` if you want to discard the `{R}` error information" ,
267+ enclosing_scope = "this function returns an `Option`"
268+ ) ,
257269 on(
258270 all(
259271 from_method = "from_residual" ,
@@ -272,13 +284,26 @@ pub trait Try: FromResidual {
272284 from_method = "from_residual" ,
273285 from_desugaring = "QuestionMark" ,
274286 _Self = "std::ops::ControlFlow<B, C>" ,
287+ R = "std::ops::ControlFlow<B, C>" ,
275288 ) ,
276- message = "the `?` operator can only be used on `ControlFlow<B, _>`s \
277- in {ItemContext} that returns `ControlFlow<B, _>`",
289+ message = "the `?` operator in {ItemContext} that returns `ControlFlow<B, _>` \
290+ can only be used on other `ControlFlow<B, _>`s (with the same Break type) ",
278291 label = "this `?` produces `{R}`, which is incompatible with `{Self}`" ,
279292 enclosing_scope = "this function returns a `ControlFlow`" ,
280293 note = "unlike `Result`, there's no `From`-conversion performed for `ControlFlow`"
281294 ) ,
295+ on(
296+ all(
297+ from_method = "from_residual" ,
298+ from_desugaring = "QuestionMark" ,
299+ _Self = "std::ops::ControlFlow<B, C>" ,
300+ // `R` is not a `ControlFlow`, as that case was matched previously
301+ ) ,
302+ message = "the `?` operator can only be used on `ControlFlow`s \
303+ in {ItemContext} that returns `ControlFlow`",
304+ label = "this `?` produces `{R}`, which is incompatible with `{Self}`" ,
305+ enclosing_scope = "this function returns a `ControlFlow`" ,
306+ ) ,
282307 on(
283308 all(
284309 from_method = "from_residual" ,
0 commit comments