@@ -599,11 +599,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
599
599
}
600
600
}
601
601
602
- pub fn report_selection_error ( & self ,
603
- obligation : & PredicateObligation < ' tcx > ,
604
- error : & SelectionError < ' tcx > ,
605
- fallback_has_occurred : bool )
606
- {
602
+ pub fn report_selection_error (
603
+ & self ,
604
+ obligation : & PredicateObligation < ' tcx > ,
605
+ error : & SelectionError < ' tcx > ,
606
+ fallback_has_occurred : bool ,
607
+ ) {
607
608
let span = obligation. cause . span ;
608
609
609
610
let mut err = match * error {
@@ -673,6 +674,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
673
674
674
675
self . suggest_borrow_on_unsized_slice ( & obligation. cause . code , & mut err) ;
675
676
self . suggest_remove_reference ( & obligation, & mut err, & trait_ref) ;
677
+ self . suggest_semicolon_removal ( & obligation, & mut err, span, & trait_ref) ;
676
678
677
679
// Try to report a help message
678
680
if !trait_ref. has_infer_types ( ) &&
@@ -901,9 +903,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
901
903
902
904
/// When encountering an assignment of an unsized trait, like `let x = ""[..];`, provide a
903
905
/// suggestion to borrow the initializer in order to use have a slice instead.
904
- fn suggest_borrow_on_unsized_slice ( & self ,
905
- code : & ObligationCauseCode < ' tcx > ,
906
- err : & mut DiagnosticBuilder < ' tcx > ) {
906
+ fn suggest_borrow_on_unsized_slice (
907
+ & self ,
908
+ code : & ObligationCauseCode < ' tcx > ,
909
+ err : & mut DiagnosticBuilder < ' tcx > ,
910
+ ) {
907
911
if let & ObligationCauseCode :: VariableType ( node_id) = code {
908
912
let parent_node = self . tcx . hir ( ) . get_parent_node ( node_id) ;
909
913
if let Some ( Node :: Local ( ref local) ) = self . tcx . hir ( ) . find ( parent_node) {
@@ -925,10 +929,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
925
929
926
930
/// Whenever references are used by mistake, like `for (i, e) in &vec.iter().enumerate()`,
927
931
/// suggest removing these references until we reach a type that implements the trait.
928
- fn suggest_remove_reference ( & self ,
929
- obligation : & PredicateObligation < ' tcx > ,
930
- err : & mut DiagnosticBuilder < ' tcx > ,
931
- trait_ref : & ty:: Binder < ty:: TraitRef < ' tcx > > ) {
932
+ fn suggest_remove_reference (
933
+ & self ,
934
+ obligation : & PredicateObligation < ' tcx > ,
935
+ err : & mut DiagnosticBuilder < ' tcx > ,
936
+ trait_ref : & ty:: Binder < ty:: TraitRef < ' tcx > > ,
937
+ ) {
932
938
let trait_ref = trait_ref. skip_binder ( ) ;
933
939
let span = obligation. cause . span ;
934
940
@@ -970,6 +976,40 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
970
976
}
971
977
}
972
978
979
+ fn suggest_semicolon_removal (
980
+ & self ,
981
+ obligation : & PredicateObligation < ' tcx > ,
982
+ err : & mut DiagnosticBuilder < ' tcx > ,
983
+ span : Span ,
984
+ trait_ref : & ty:: Binder < ty:: TraitRef < ' tcx > > ,
985
+ ) {
986
+ let hir = self . tcx . hir ( ) ;
987
+ let parent_node = hir. get_parent_node (
988
+ hir. hir_to_node_id ( obligation. cause . body_id ) ,
989
+ ) ;
990
+ let node = hir. find ( parent_node) ;
991
+ if let Some ( hir:: Node :: Item ( hir:: Item {
992
+ node : hir:: ItemKind :: Fn ( decl, _, _, body_id) ,
993
+ ..
994
+ } ) ) = node {
995
+ let body = hir. body ( * body_id) ;
996
+ if let hir:: ExprKind :: Block ( blk, _) = & body. value . node {
997
+ if decl. output . span ( ) . overlaps ( span) && blk. expr . is_none ( ) &&
998
+ "()" == & trait_ref. self_ty ( ) . to_string ( )
999
+ {
1000
+ // FIXME(estebank): When encountering a method with a trait
1001
+ // bound not satisfied in the return type with a body that has
1002
+ // no return, suggest removal of semicolon on last statement.
1003
+ // Once that is added, close #54771.
1004
+ if let Some ( ref stmt) = blk. stmts . last ( ) {
1005
+ let sp = self . tcx . sess . source_map ( ) . end_point ( stmt. span ) ;
1006
+ err. span_label ( sp, "consider removing this semicolon" ) ;
1007
+ }
1008
+ }
1009
+ }
1010
+ }
1011
+ }
1012
+
973
1013
/// Given some node representing a fn-like thing in the HIR map,
974
1014
/// returns a span and `ArgKind` information that describes the
975
1015
/// arguments it expects. This can be supplied to
0 commit comments