@@ -1083,20 +1083,31 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
1083
1083
let parent_node = hir. get_parent_node ( obligation. cause . body_id ) ;
1084
1084
let node = hir. find ( parent_node) ;
1085
1085
if let Some ( hir:: Node :: Item ( hir:: Item { kind : hir:: ItemKind :: Fn ( sig, _, body_id) , .. } ) ) = node
1086
- && let body = hir. body ( * body_id)
1087
- && let hir:: ExprKind :: Block ( blk, _) = & body. value . kind
1086
+ && let hir:: ExprKind :: Block ( blk, _) = & hir. body ( * body_id) . value . kind
1088
1087
&& sig. decl . output . span ( ) . overlaps ( span)
1089
1088
&& blk. expr . is_none ( )
1090
- && * trait_pred. self_ty ( ) . skip_binder ( ) . kind ( ) == ty:: Tuple ( ty:: List :: empty ( ) )
1091
- // FIXME(estebank): When encountering a method with a trait
1092
- // bound not satisfied in the return type with a body that has
1093
- // no return, suggest removal of semicolon on last statement.
1094
- // Once that is added, close #54771.
1089
+ && trait_pred. self_ty ( ) . skip_binder ( ) . is_unit ( )
1095
1090
&& let Some ( stmt) = blk. stmts . last ( )
1096
- && let hir:: StmtKind :: Semi ( _) = stmt. kind
1091
+ && let hir:: StmtKind :: Semi ( expr) = stmt. kind
1092
+ // Only suggest this if the expression behind the semicolon implements the predicate
1093
+ && let Some ( typeck_results) = self . in_progress_typeck_results
1094
+ && let Some ( ty) = typeck_results. borrow ( ) . expr_ty_opt ( expr)
1095
+ && self . predicate_may_hold ( & self . mk_trait_obligation_with_new_self_ty ( obligation. param_env , trait_pred, ty) )
1097
1096
{
1098
- let sp = self . tcx . sess . source_map ( ) . end_point ( stmt. span ) ;
1099
- err. span_label ( sp, "consider removing this semicolon" ) ;
1097
+ err. span_label (
1098
+ expr. span ,
1099
+ & format ! (
1100
+ "this expression has type `{}`, which implements `{}`" ,
1101
+ ty,
1102
+ trait_pred. print_modifiers_and_trait_path( )
1103
+ )
1104
+ ) ;
1105
+ err. span_suggestion (
1106
+ self . tcx . sess . source_map ( ) . end_point ( stmt. span ) ,
1107
+ "remove this semicolon" ,
1108
+ String :: new ( ) ,
1109
+ Applicability :: MachineApplicable
1110
+ ) ;
1100
1111
return true ;
1101
1112
}
1102
1113
false
0 commit comments