Skip to content

Commit e6387b6

Browse files
committed
Fix rebase and move suggestion to its own method
1 parent f2718dc commit e6387b6

File tree

1 file changed

+46
-29
lines changed

1 file changed

+46
-29
lines changed

src/librustc/traits/error_reporting.rs

+46-29
Original file line numberDiff line numberDiff line change
@@ -648,28 +648,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
648648
trait_ref.to_predicate(), post_message)
649649
));
650650

651-
let parent_node = self.tcx.hir().get_parent_node(obligation.cause.body_id);
652-
let node = self.tcx.hir().find(parent_node);
653-
if let Some(hir::Node::Item(hir::Item {
654-
node: hir::ItemKind::Fn(decl, _, _, body_id),
655-
..
656-
})) = node {
657-
let body = self.tcx.hir().body(*body_id);
658-
if let hir::ExprKind::Block(blk, _) = &body.value.node {
659-
if decl.output.span().overlaps(span) && blk.expr.is_none() &&
660-
"()" == &trait_ref.self_ty().to_string()
661-
{
662-
// FIXME(estebank): When encountering a method with a trait
663-
// bound not satisfied in the return type with a body that has
664-
// no return, suggest removal of semicolon on last statement.
665-
// Once that is added, close #54771.
666-
if let Some(ref stmt) = blk.stmts.last() {
667-
let sp = self.tcx.sess.source_map().end_point(stmt.span);
668-
err.span_label(sp, "consider removing this semicolon");
669-
}
670-
}
671-
}
672-
}
673651
let explanation =
674652
if obligation.cause.code == ObligationCauseCode::MainFunctionType {
675653
"consider using `()`, or a `Result`".to_owned()
@@ -695,6 +673,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
695673

696674
self.suggest_borrow_on_unsized_slice(&obligation.cause.code, &mut err);
697675
self.suggest_remove_reference(&obligation, &mut err, &trait_ref);
676+
self.suggest_semicolon_removal(&obligation, &mut err, span, &trait_ref);
698677

699678
// Try to report a help message
700679
if !trait_ref.has_infer_types() &&
@@ -923,9 +902,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
923902

924903
/// When encountering an assignment of an unsized trait, like `let x = ""[..];`, provide a
925904
/// suggestion to borrow the initializer in order to use have a slice instead.
926-
fn suggest_borrow_on_unsized_slice(&self,
927-
code: &ObligationCauseCode<'tcx>,
928-
err: &mut DiagnosticBuilder<'tcx>) {
905+
fn suggest_borrow_on_unsized_slice(
906+
&self,
907+
code: &ObligationCauseCode<'tcx>,
908+
err: &mut DiagnosticBuilder<'tcx>,
909+
) {
929910
if let &ObligationCauseCode::VariableType(node_id) = code {
930911
let parent_node = self.tcx.hir().get_parent_node(node_id);
931912
if let Some(Node::Local(ref local)) = self.tcx.hir().find(parent_node) {
@@ -947,10 +928,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
947928

948929
/// Whenever references are used by mistake, like `for (i, e) in &vec.iter().enumerate()`,
949930
/// suggest removing these references until we reach a type that implements the trait.
950-
fn suggest_remove_reference(&self,
951-
obligation: &PredicateObligation<'tcx>,
952-
err: &mut DiagnosticBuilder<'tcx>,
953-
trait_ref: &ty::Binder<ty::TraitRef<'tcx>>) {
931+
fn suggest_remove_reference(
932+
&self,
933+
obligation: &PredicateObligation<'tcx>,
934+
err: &mut DiagnosticBuilder<'tcx>,
935+
trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
936+
) {
954937
let trait_ref = trait_ref.skip_binder();
955938
let span = obligation.cause.span;
956939

@@ -992,6 +975,40 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
992975
}
993976
}
994977

978+
fn suggest_semicolon_removal(
979+
&self,
980+
obligation: &PredicateObligation<'tcx>,
981+
err: &mut DiagnosticBuilder<'tcx>,
982+
span: Span,
983+
trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
984+
) {
985+
let hir = self.tcx.hir();
986+
let parent_node = hir.get_parent_node(
987+
hir.hir_to_node_id(obligation.cause.body_id),
988+
);
989+
let node = hir.find(parent_node);
990+
if let Some(hir::Node::Item(hir::Item {
991+
node: hir::ItemKind::Fn(decl, _, _, body_id),
992+
..
993+
})) = node {
994+
let body = hir.body(*body_id);
995+
if let hir::ExprKind::Block(blk, _) = &body.value.node {
996+
if decl.output.span().overlaps(span) && blk.expr.is_none() &&
997+
"()" == &trait_ref.self_ty().to_string()
998+
{
999+
// FIXME(estebank): When encountering a method with a trait
1000+
// bound not satisfied in the return type with a body that has
1001+
// no return, suggest removal of semicolon on last statement.
1002+
// Once that is added, close #54771.
1003+
if let Some(ref stmt) = blk.stmts.last() {
1004+
let sp = self.tcx.sess.source_map().end_point(stmt.span);
1005+
err.span_label(sp, "consider removing this semicolon");
1006+
}
1007+
}
1008+
}
1009+
}
1010+
}
1011+
9951012
/// Given some node representing a fn-like thing in the HIR map,
9961013
/// returns a span and `ArgKind` information that describes the
9971014
/// arguments it expects. This can be supplied to

0 commit comments

Comments
 (0)