@@ -41,7 +41,7 @@ use rustc_middle::ty::subst::InternalSubsts;
41
41
use rustc_middle:: ty:: util:: Discr ;
42
42
use rustc_middle:: ty:: util:: IntTypeExt ;
43
43
use rustc_middle:: ty:: { self , AdtKind , Const , DefIdTree , Ty , TyCtxt } ;
44
- use rustc_middle:: ty:: { ReprOptions , ToPredicate , TypeFoldable } ;
44
+ use rustc_middle:: ty:: { ReprOptions , ToPredicate } ;
45
45
use rustc_session:: lint;
46
46
use rustc_session:: parse:: feature_err;
47
47
use rustc_span:: symbol:: { kw, sym, Ident , Symbol } ;
@@ -2004,28 +2004,29 @@ fn infer_return_ty_for_fn_sig<'tcx>(
2004
2004
visitor. visit_ty ( ty) ;
2005
2005
let mut diag = bad_placeholder ( tcx, visitor. 0 , "return type" ) ;
2006
2006
let ret_ty = fn_sig. skip_binder ( ) . output ( ) ;
2007
- if !ret_ty. references_error ( ) {
2008
- if !ret_ty. is_closure ( ) {
2009
- let ret_ty_str = match ret_ty. kind ( ) {
2010
- // Suggest a function pointer return type instead of a unique function definition
2011
- // (e.g. `fn() -> i32` instead of `fn() -> i32 { f }`, the latter of which is invalid
2012
- // syntax)
2013
- ty:: FnDef ( ..) => ret_ty. fn_sig ( tcx) . to_string ( ) ,
2014
- _ => ret_ty. to_string ( ) ,
2015
- } ;
2007
+ if ret_ty. is_suggestable ( ) {
2008
+ diag. span_suggestion (
2009
+ ty. span ,
2010
+ "replace with the correct return type" ,
2011
+ ret_ty. to_string ( ) ,
2012
+ Applicability :: MachineApplicable ,
2013
+ ) ;
2014
+ } else if matches ! ( ret_ty. kind( ) , ty:: FnDef ( ..) ) {
2015
+ let fn_sig = ret_ty. fn_sig ( tcx) ;
2016
+ if fn_sig. skip_binder ( ) . inputs_and_output . iter ( ) . all ( |t| t. is_suggestable ( ) ) {
2016
2017
diag. span_suggestion (
2017
2018
ty. span ,
2018
2019
"replace with the correct return type" ,
2019
- ret_ty_str ,
2020
- Applicability :: MaybeIncorrect ,
2020
+ fn_sig . to_string ( ) ,
2021
+ Applicability :: MachineApplicable ,
2021
2022
) ;
2022
- } else {
2023
- // We're dealing with a closure, so we should suggest using `impl Fn` or trait bounds
2024
- // to prevent the user from getting a papercut while trying to use the unique closure
2025
- // syntax (e.g. `[closure@src/lib.rs:2:5: 2:9]`).
2026
- diag. help ( "consider using an `Fn`, `FnMut`, or `FnOnce` trait bound" ) ;
2027
- diag. note ( "for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html" ) ;
2028
2023
}
2024
+ } else if ret_ty. is_closure ( ) {
2025
+ // We're dealing with a closure, so we should suggest using `impl Fn` or trait bounds
2026
+ // to prevent the user from getting a papercut while trying to use the unique closure
2027
+ // syntax (e.g. `[closure@src/lib.rs:2:5: 2:9]`).
2028
+ diag. help ( "consider using an `Fn`, `FnMut`, or `FnOnce` trait bound" ) ;
2029
+ diag. note ( "for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html" ) ;
2029
2030
}
2030
2031
diag. emit ( ) ;
2031
2032
0 commit comments