mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[Diagnostics] In ExtraneousReturnFailure, insert the return type fix-it
after any effects specifiers like async, throws, rethrows, etc.
This commit is contained in:
@@ -5337,8 +5337,17 @@ bool ExtraneousReturnFailure::diagnoseAsError() {
|
||||
if (FD->getResultTypeRepr() == nullptr &&
|
||||
FD->getParameters()->getStartLoc().isValid() &&
|
||||
!FD->getBaseIdentifier().empty()) {
|
||||
auto fixItLoc = Lexer::getLocForEndOfToken(
|
||||
getASTContext().SourceMgr, FD->getParameters()->getEndLoc());
|
||||
// Insert the fix-it after the parameter list, and after any
|
||||
// effects specifiers.
|
||||
SourceLoc loc = FD->getParameters()->getEndLoc();
|
||||
if (auto asyncLoc = FD->getAsyncLoc())
|
||||
loc = asyncLoc;
|
||||
|
||||
if (auto throwsLoc = FD->getThrowsLoc())
|
||||
if (throwsLoc.getOpaquePointerValue() > loc.getOpaquePointerValue())
|
||||
loc = throwsLoc;
|
||||
|
||||
auto fixItLoc = Lexer::getLocForEndOfToken(getASTContext().SourceMgr, loc);
|
||||
emitDiagnostic(diag::add_return_type_note)
|
||||
.fixItInsert(fixItLoc, " -> <#Return Type#>");
|
||||
}
|
||||
|
||||
@@ -1213,6 +1213,43 @@ func voidFuncWithNestedVoidFunc() {
|
||||
}
|
||||
}
|
||||
|
||||
func voidFuncWithEffects1() throws {
|
||||
return 1
|
||||
// expected-error@-1 {{unexpected non-void return value in void function}}
|
||||
// expected-note@-2 {{did you mean to add a return type?}}{{35-35= -> <#Return Type#>}}
|
||||
}
|
||||
|
||||
func voidFuncWithEffects2() async throws {
|
||||
return 1
|
||||
// expected-error@-1 {{unexpected non-void return value in void function}}
|
||||
// expected-note@-2 {{did you mean to add a return type?}}{{41-41= -> <#Return Type#>}}
|
||||
}
|
||||
|
||||
// expected-error@+1 {{'async' must precede 'throws'}}
|
||||
func voidFuncWithEffects3() throws async {
|
||||
return 1
|
||||
// expected-error@-1 {{unexpected non-void return value in void function}}
|
||||
// expected-note@-2 {{did you mean to add a return type?}}{{41-41= -> <#Return Type#>}}
|
||||
}
|
||||
|
||||
func voidFuncWithEffects4() async {
|
||||
return 1
|
||||
// expected-error@-1 {{unexpected non-void return value in void function}}
|
||||
// expected-note@-2 {{did you mean to add a return type?}}{{34-34= -> <#Return Type#>}}
|
||||
}
|
||||
|
||||
func voidFuncWithEffects5(_ closure: () throws -> Void) rethrows {
|
||||
return 1
|
||||
// expected-error@-1 {{unexpected non-void return value in void function}}
|
||||
// expected-note@-2 {{did you mean to add a return type?}}{{65-65= -> <#Return Type#>}}
|
||||
}
|
||||
|
||||
func voidGenericFuncWithEffects<T>(arg: T) async where T: CustomStringConvertible {
|
||||
return 1
|
||||
// expected-error@-1 {{unexpected non-void return value in void function}}
|
||||
// expected-note@-2 {{did you mean to add a return type?}}{{49-49= -> <#Return Type#>}}
|
||||
}
|
||||
|
||||
// Special cases: These should not offer a note + fix-it
|
||||
|
||||
func voidFuncExplicitType() -> Void {
|
||||
|
||||
Reference in New Issue
Block a user