diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 5cec697c6bbd9..3fdd9c34f46d9 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -664,7 +664,7 @@ impl<'hir> Map<'hir> { match *node { NodeExpr(ref expr) => { match expr.node { - ExprWhile(..) | ExprLoop(..) | ExprIf(..) => true, + ExprWhile(..) | ExprLoop(..) => true, _ => false, } } diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 73aa5ac5a0f0a..7c8eb77380f02 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -95,7 +95,7 @@ use rustc::middle::region::CodeExtent; use rustc::ty::subst::{Kind, Subst, Substs}; use rustc::traits::{self, FulfillmentContext, ObligationCause, ObligationCauseCode}; use rustc::ty::{ParamTy, LvaluePreference, NoPreference, PreferMutLvalue}; -use rustc::ty::{self, Ty, TyCtxt, Visibility}; +use rustc::ty::{self, Ty, TyCtxt, Visibility, TypeVariants}; use rustc::ty::adjustment::{Adjust, Adjustment, AutoBorrow}; use rustc::ty::fold::{BottomUpFolder, TypeFoldable}; use rustc::ty::maps::Providers; @@ -4302,7 +4302,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { expected: Ty<'tcx>, found: Ty<'tcx>, can_suggest: bool) { - // Only suggest changing the return type for methods that // haven't set a return type at all (and aren't `fn main()` or an impl). match (&fn_decl.output, found.is_suggestable(), can_suggest) { @@ -4316,13 +4315,31 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } (&hir::FunctionRetTy::DefaultReturn(span), _, _) => { // `fn main()` must return `()`, do not suggest changing return type - err.span_label(span, "expected `()` because of default return type"); + err.span_label(span, "expected () because of default return type"); } (&hir::FunctionRetTy::Return(ref ty), _, _) => { // Only point to return type if the expected type is the return type, as if they // are not, the expectation must have been caused by something else. - err.span_label(ty.span, - format!("expected `{}` because of return type", expected)); + debug!("suggest_missing_return_type: return type {:?} node {:?}", ty, ty.node); + let sp = ty.span; + let ty = AstConv::ast_ty_to_ty(self, ty); + debug!("suggest_missing_return_type: return type sty {:?}", ty.sty); + debug!("suggest_missing_return_type: expected type sty {:?}", ty.sty); + if ty.sty == expected.sty { + let quote = if let TypeVariants::TyTuple(ref slice, _) = expected.sty { + if slice.len() == 0 { // don't use backtics for () + "" + } else { + "`" + } + } else { + "`" + }; + err.span_label(sp, format!("expected {}{}{} because of return type", + quote, + expected, + quote)); + } } } } diff --git a/src/test/ui/block-result/block-must-not-have-result-res.stderr b/src/test/ui/block-result/block-must-not-have-result-res.stderr index 20c7dc416f3bb..6432375fe709b 100644 --- a/src/test/ui/block-result/block-must-not-have-result-res.stderr +++ b/src/test/ui/block-result/block-must-not-have-result-res.stderr @@ -2,7 +2,7 @@ error[E0308]: mismatched types --> $DIR/block-must-not-have-result-res.rs:15:9 | 14 | fn drop(&mut self) { - | - expected `()` because of default return type + | - expected () because of default return type 15 | true //~ ERROR mismatched types | ^^^^ expected (), found bool | diff --git a/src/test/ui/block-result/issue-13624.stderr b/src/test/ui/block-result/issue-13624.stderr index cd8c28cd2cfa8..8fe4a51257410 100644 --- a/src/test/ui/block-result/issue-13624.stderr +++ b/src/test/ui/block-result/issue-13624.stderr @@ -2,7 +2,7 @@ error[E0308]: mismatched types --> $DIR/issue-13624.rs:17:5 | 16 | pub fn get_enum_struct_variant() -> () { - | -- expected `()` because of return type + | -- expected () because of return type 17 | Enum::EnumStructVariant { x: 1, y: 2, z: 3 } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found enum `a::Enum` | diff --git a/src/test/ui/block-result/issue-22645.stderr b/src/test/ui/block-result/issue-22645.stderr index c6113ae0c9f60..ad39e38cab63f 100644 --- a/src/test/ui/block-result/issue-22645.stderr +++ b/src/test/ui/block-result/issue-22645.stderr @@ -12,7 +12,7 @@ error[E0308]: mismatched types --> $DIR/issue-22645.rs:25:3 | 23 | fn main() { - | - expected `()` because of default return type + | - expected () because of default return type 24 | let b = Bob + 3.5; 25 | b + 3 //~ ERROR E0277 | ^^^^^ expected (), found struct `Bob` diff --git a/src/test/ui/block-result/issue-5500.stderr b/src/test/ui/block-result/issue-5500.stderr index 29dbd5a8cf599..944391c1719cf 100644 --- a/src/test/ui/block-result/issue-5500.stderr +++ b/src/test/ui/block-result/issue-5500.stderr @@ -2,7 +2,7 @@ error[E0308]: mismatched types --> $DIR/issue-5500.rs:12:5 | 11 | fn main() { - | - expected `()` because of default return type + | - expected () because of default return type 12 | &panic!() | ^^^^^^^^^ expected (), found reference | diff --git a/src/test/ui/impl-trait/equality.stderr b/src/test/ui/impl-trait/equality.stderr index 791ce4860d0cd..3fc08a0900fb9 100644 --- a/src/test/ui/impl-trait/equality.stderr +++ b/src/test/ui/impl-trait/equality.stderr @@ -1,9 +1,6 @@ error[E0308]: mismatched types --> $DIR/equality.rs:25:5 | -21 | fn two(x: bool) -> impl Foo { - | -------- expected `_` because of return type -... 25 | 0_u32 | ^^^^^ expected i32, found u32 |