Skip to content

Commit 4104427

Browse files
stereotype441Commit Bot
authored andcommitted
If a try block terminates, don't consider the contents of the finally block to be dead code.
Fixes #48258. Bug: #48258 Change-Id: Icf88c659f3258a723970d89882b58c75f8eff6d8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/231043 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 0e67d98 commit 4104427

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

pkg/analyzer/lib/src/generated/resolver.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,10 +2445,10 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
24452445
flow.tryCatchStatement_bodyBegin();
24462446
}
24472447
body.accept(this);
2448+
nullSafetyDeadCodeVerifier.flowEnd(node.body);
2449+
nullSafetyDeadCodeVerifier.tryStatementEnter(node);
24482450
if (catchClauses.isNotEmpty) {
24492451
flow.tryCatchStatement_bodyEnd(body);
2450-
nullSafetyDeadCodeVerifier.flowEnd(node.body);
2451-
nullSafetyDeadCodeVerifier.tryStatementEnter(node);
24522452

24532453
var catchLength = catchClauses.length;
24542454
for (var i = 0; i < catchLength; ++i) {
@@ -2464,8 +2464,8 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
24642464
}
24652465

24662466
flow.tryCatchStatement_end();
2467-
nullSafetyDeadCodeVerifier.tryStatementExit(node);
24682467
}
2468+
nullSafetyDeadCodeVerifier.tryStatementExit(node);
24692469

24702470
if (finallyBlock != null) {
24712471
flow.tryFinallyStatement_finallyBegin(

pkg/analyzer/test/src/diagnostics/dead_code_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,24 @@ int f(Foo foo) {
140140
error(HintCode.DEAD_CODE, 111, 10),
141141
]);
142142
}
143+
144+
test_try_finally() async {
145+
await assertErrorsInCode('''
146+
main() {
147+
try {
148+
foo();
149+
print('dead');
150+
} finally {
151+
print('alive');
152+
}
153+
print('dead');
154+
}
155+
Never foo() => throw 'exception';
156+
''', [
157+
error(HintCode.DEAD_CODE, 32, 14),
158+
error(HintCode.DEAD_CODE, 87, 14),
159+
]);
160+
}
143161
}
144162

145163
mixin DeadCodeTestCases on PubPackageResolutionTest {

0 commit comments

Comments
 (0)