Skip to content

Commit e25c39c

Browse files
committed
[Refactoring] Add try for extracted function call
If we're extracting a function that throws, add a `try` to the call.
1 parent 81d8c2e commit e25c39c

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,10 @@ bool RefactoringActionExtractFunction::performChange() {
13321332
llvm::raw_svector_ostream OS(Buffer);
13331333
if (RangeInfo.exit() == ExitState::Positive)
13341334
OS << tok::kw_return <<" ";
1335+
1336+
if (RangeInfo.UnhandledEffects.contains(EffectKind::Throws))
1337+
OS << tok::kw_try << " ";
1338+
13351339
CallNameOffset = Buffer.size() - ReplaceBegin;
13361340
OS << PreferredName << "(";
13371341
for (auto &RD : Parameters) {

test/refactoring/ExtractFunction/Outputs/throw_errors/L13-17.swift.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ func foo2() throws {
1818
do {
1919
try foo1()
2020
} catch {}
21-
new_name()
21+
try new_name()
2222
}
2323

test/refactoring/ExtractFunction/Outputs/throw_errors/L8-8.swift.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ return try foo1()
99
}
1010

1111
func foo2() throws {
12-
new_name()
12+
try new_name()
1313
try! foo1()
1414
do {
1515
try foo1()

0 commit comments

Comments
 (0)