diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 0185d00518699..fa78b38dbb7a8 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -4625,21 +4625,23 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, '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) { - (&hir::FunctionRetTy::DefaultReturn(span), true, true) => { + match (&fn_decl.output, found.is_suggestable(), can_suggest, expected.is_nil()) { + (&hir::FunctionRetTy::DefaultReturn(span), true, true, true) => { err.span_suggestion(span, "try adding a return type", format!("-> {} ", self.resolve_type_vars_with_obligations(found))); } - (&hir::FunctionRetTy::DefaultReturn(span), false, true) => { + (&hir::FunctionRetTy::DefaultReturn(span), false, true, true) => { err.span_label(span, "possibly return type missing here?"); } - (&hir::FunctionRetTy::DefaultReturn(span), _, _) => { + (&hir::FunctionRetTy::DefaultReturn(span), _, false, true) => { // `fn main()` must return `()`, do not suggest changing return type err.span_label(span, "expected `()` because of default return type"); } - (&hir::FunctionRetTy::Return(ref ty), _, _) => { + // expectation was caused by something else, not the default return + (&hir::FunctionRetTy::DefaultReturn(_), _, _, false) => {} + (&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. debug!("suggest_missing_return_type: return type {:?} node {:?}", ty, ty.node); diff --git a/src/test/ui/break-while-condition.stderr b/src/test/ui/break-while-condition.stderr index c8f06db960392..9d22dcce4540c 100644 --- a/src/test/ui/break-while-condition.stderr +++ b/src/test/ui/break-while-condition.stderr @@ -13,9 +13,6 @@ LL | | }; error[E0308]: mismatched types --> $DIR/break-while-condition.rs:26:13 | -LL | fn main() { - | - expected `()` because of default return type -... LL | / while false { //~ ERROR mismatched types LL | | break LL | | } @@ -27,9 +24,6 @@ LL | | } error[E0308]: mismatched types --> $DIR/break-while-condition.rs:34:13 | -LL | fn main() { - | - expected `()` because of default return type -... LL | / while false { //~ ERROR mismatched types LL | | return LL | | } diff --git a/src/test/ui/issue-50585.stderr b/src/test/ui/issue-50585.stderr index 3f9328de93fd5..e4edc18c3f8e4 100644 --- a/src/test/ui/issue-50585.stderr +++ b/src/test/ui/issue-50585.stderr @@ -1,8 +1,6 @@ error[E0308]: mismatched types --> $DIR/issue-50585.rs:12:18 | -LL | fn main() { - | - expected `()` because of default return type LL | |y: Vec<[(); for x in 0..2 {}]>| {}; | ^^^^^^^^^^^^^^^^ expected usize, found () | diff --git a/src/test/ui/suggestions/issue-46302.rs b/src/test/ui/suggestions/issue-46302.rs new file mode 100644 index 0000000000000..6ae6b549b070e --- /dev/null +++ b/src/test/ui/suggestions/issue-46302.rs @@ -0,0 +1,19 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn foo() { + let s = "abc"; + let u: &str = if true { s[..2] } else { s }; + //~^ ERROR mismatched types +} + +fn main() { + foo(); +} diff --git a/src/test/ui/suggestions/issue-46302.stderr b/src/test/ui/suggestions/issue-46302.stderr new file mode 100644 index 0000000000000..8e399136fadba --- /dev/null +++ b/src/test/ui/suggestions/issue-46302.stderr @@ -0,0 +1,15 @@ +error[E0308]: mismatched types + --> $DIR/issue-46302.rs:13:27 + | +LL | let u: &str = if true { s[..2] } else { s }; + | ^^^^^^ + | | + | expected &str, found str + | help: consider borrowing here: `&s[..2]` + | + = note: expected type `&str` + found type `str` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/suggestions/str-array-assignment.stderr b/src/test/ui/suggestions/str-array-assignment.stderr index 041bae4a42108..5af936eec5bb6 100644 --- a/src/test/ui/suggestions/str-array-assignment.stderr +++ b/src/test/ui/suggestions/str-array-assignment.stderr @@ -10,9 +10,6 @@ LL | let t = if true { s[..2] } else { s }; error[E0308]: mismatched types --> $DIR/str-array-assignment.rs:15:27 | -LL | fn main() { - | - expected `()` because of default return type -... LL | let u: &str = if true { s[..2] } else { s }; | ^^^^^^ | |