From 5daeba9b42ddb7f6cad383b96cca4b9e6dff560f Mon Sep 17 00:00:00 2001 From: surechen Date: Sun, 9 Jun 2024 17:20:25 +0800 Subject: [PATCH] For E0277 suggest adding `Result` return type for function which using QuesionMark `?` in the body. fixes #125997 --- .../src/traits/error_reporting/suggestions.rs | 52 +++++++++++++++++++ .../error_reporting/type_err_ctxt_ext.rs | 4 ++ ...turn-from-residual-sugg-issue-125997.fixed | 19 +++++++ .../return-from-residual-sugg-issue-125997.rs | 17 ++++++ ...urn-from-residual-sugg-issue-125997.stderr | 39 ++++++++++++++ .../ui/try-trait/try-operator-on-main.stderr | 11 ++++ 6 files changed, 142 insertions(+) create mode 100644 tests/ui/return/return-from-residual-sugg-issue-125997.fixed create mode 100644 tests/ui/return/return-from-residual-sugg-issue-125997.rs create mode 100644 tests/ui/return/return-from-residual-sugg-issue-125997.stderr diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 9336148fd672c..50ec40d71d760 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -4586,6 +4586,58 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { _ => "/* value */".to_string(), }) } + + fn suggest_add_result_as_return_type( + &self, + obligation: &PredicateObligation<'tcx>, + err: &mut Diag<'_>, + trait_ref: ty::PolyTraitRef<'tcx>, + ) { + if ObligationCauseCode::QuestionMark != *obligation.cause.code().peel_derives() { + return; + } + + let node = self.tcx.hir_node_by_def_id(obligation.cause.body_id); + if let hir::Node::Item(item) = node + && let hir::ItemKind::Fn(sig, _, body_id) = item.kind + && let hir::FnRetTy::DefaultReturn(ret_span) = sig.decl.output + && trait_ref.skip_binder().args.len() == 2 + && let ty::Tuple(l) = trait_ref.skip_binder().args.type_at(0).kind() + && l.len() == 0 + && let ty::Adt(def, _) = trait_ref.skip_binder().args.type_at(1).kind() + && self.tcx.is_diagnostic_item(sym::Result, def.did()) + { + let body = self.tcx.hir().body(body_id); + let mut sugg_spans = + vec![(ret_span, " -> Result<(), Box>".to_string())]; + + if let hir::ExprKind::Block(b, _) = body.value.kind + && b.expr.is_none() + && let Some(s) = b.stmts.last() + && !is_ret_expr(s) + { + sugg_spans.push(( + s.span.shrink_to_hi().until(body.value.span.shrink_to_hi()), + "\n\n return Ok(());\n}".to_string(), + )); + } + err.multipart_suggestion_verbose( + format!("consider adding return type"), + sugg_spans, + Applicability::MaybeIncorrect, + ); + } + + fn is_ret_expr<'hir>(stmt: &hir::Stmt<'hir>) -> bool { + if let hir::StmtKind::Semi(expr) = stmt.kind + && let hir::ExprKind::Ret(Some(_)) = expr.kind + { + true + } else { + false + } + } + } } /// Add a hint to add a missing borrow or remove an unnecessary one. diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs index da611b748da6b..9d1cf0e74f3eb 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs @@ -612,6 +612,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { &mut err, trait_predicate, ); + self.suggest_add_result_as_return_type(&obligation, + &mut err, + trait_ref); + if self.suggest_add_reference_to_arg( &obligation, &mut err, diff --git a/tests/ui/return/return-from-residual-sugg-issue-125997.fixed b/tests/ui/return/return-from-residual-sugg-issue-125997.fixed new file mode 100644 index 0000000000000..34ea71201bb5d --- /dev/null +++ b/tests/ui/return/return-from-residual-sugg-issue-125997.fixed @@ -0,0 +1,19 @@ +//@ run-rustfix + +#![allow(unused_imports)] +#![allow(dead_code)] + +use std::fs::File; +use std::io::prelude::*; + +fn test1() -> Result<(), Box> { + let mut _file = File::create("foo.txt")?; + + return Ok(()); +} + +fn main() -> Result<(), Box> { + let mut _file = File::create("foo.txt")?; + + return Ok(()); +} diff --git a/tests/ui/return/return-from-residual-sugg-issue-125997.rs b/tests/ui/return/return-from-residual-sugg-issue-125997.rs new file mode 100644 index 0000000000000..42f42b8911e83 --- /dev/null +++ b/tests/ui/return/return-from-residual-sugg-issue-125997.rs @@ -0,0 +1,17 @@ +//@ run-rustfix + +#![allow(unused_imports)] +#![allow(dead_code)] + +use std::fs::File; +use std::io::prelude::*; + +fn test1() { + let mut _file = File::create("foo.txt")?; + //~^ ERROR the `?` operator can only be used in a function +} + +fn main() { + let mut _file = File::create("foo.txt")?; + //~^ ERROR the `?` operator can only be used in a function +} diff --git a/tests/ui/return/return-from-residual-sugg-issue-125997.stderr b/tests/ui/return/return-from-residual-sugg-issue-125997.stderr new file mode 100644 index 0000000000000..17e724254c576 --- /dev/null +++ b/tests/ui/return/return-from-residual-sugg-issue-125997.stderr @@ -0,0 +1,39 @@ +error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) + --> $DIR/return-from-residual-sugg-issue-125997.rs:10:44 + | +LL | fn test1() { + | ---------- this function should return `Result` or `Option` to accept `?` +LL | let mut _file = File::create("foo.txt")?; + | ^ cannot use the `?` operator in a function that returns `()` + | + = help: the trait `FromResidual>` is not implemented for `()` +help: consider adding return type + | +LL ~ fn test1() -> Result<(), Box> { +LL ~ let mut _file = File::create("foo.txt")?; +LL + +LL + return Ok(()); +LL + } + | + +error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) + --> $DIR/return-from-residual-sugg-issue-125997.rs:15:44 + | +LL | fn main() { + | --------- this function should return `Result` or `Option` to accept `?` +LL | let mut _file = File::create("foo.txt")?; + | ^ cannot use the `?` operator in a function that returns `()` + | + = help: the trait `FromResidual>` is not implemented for `()` +help: consider adding return type + | +LL ~ fn main() -> Result<(), Box> { +LL ~ let mut _file = File::create("foo.txt")?; +LL + +LL + return Ok(()); +LL + } + | + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/try-trait/try-operator-on-main.stderr b/tests/ui/try-trait/try-operator-on-main.stderr index 7cd38e0cf95ea..c09c888327bcb 100644 --- a/tests/ui/try-trait/try-operator-on-main.stderr +++ b/tests/ui/try-trait/try-operator-on-main.stderr @@ -8,6 +8,17 @@ LL | std::fs::File::open("foo")?; | ^ cannot use the `?` operator in a function that returns `()` | = help: the trait `FromResidual>` is not implemented for `()` +help: consider adding return type + | +LL ~ fn main() -> Result<(), Box> { +LL | // error for a `Try` type on a non-`Try` fn + ... +LL | // an unrelated use of `Try` +LL ~ try_trait_generic::<()>(); +LL + +LL + return Ok(()); +LL + } + | error[E0277]: the `?` operator can only be applied to values that implement `Try` --> $DIR/try-operator-on-main.rs:10:5