提交 3fcdb8ba 编写于 作者: E Esteban Küber

Only refer to return type when it matches

上级 d96f9d47
......@@ -664,7 +664,7 @@ pub fn get_return_block(&self, id: NodeId) -> Option<NodeId> {
match *node {
NodeExpr(ref expr) => {
match expr.node {
ExprWhile(..) | ExprLoop(..) | ExprIf(..) => true,
ExprWhile(..) | ExprLoop(..) => true,
_ => false,
}
}
......
......@@ -95,7 +95,7 @@
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 @@ fn suggest_missing_return_type(&self,
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 @@ fn suggest_missing_return_type(&self,
}
(&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));
}
}
}
}
......
......@@ -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
|
......
......@@ -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`
|
......
......@@ -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`
......
......@@ -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
|
......
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
|
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册