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);
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 | | }
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 | | }
--- /dev/null
+// 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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();
+}
--- /dev/null
+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`.