Skip to content

Commit

Permalink
If a try block terminates, don't consider the contents of the `fina…
Browse files Browse the repository at this point in the history
…lly` 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]>
  • Loading branch information
stereotype441 authored and Commit Bot committed Feb 1, 2022
1 parent 0e67d98 commit 4104427
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/analyzer/lib/src/generated/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2445,10 +2445,10 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
flow.tryCatchStatement_bodyBegin();
}
body.accept(this);
nullSafetyDeadCodeVerifier.flowEnd(node.body);
nullSafetyDeadCodeVerifier.tryStatementEnter(node);
if (catchClauses.isNotEmpty) {
flow.tryCatchStatement_bodyEnd(body);
nullSafetyDeadCodeVerifier.flowEnd(node.body);
nullSafetyDeadCodeVerifier.tryStatementEnter(node);

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

flow.tryCatchStatement_end();
nullSafetyDeadCodeVerifier.tryStatementExit(node);
}
nullSafetyDeadCodeVerifier.tryStatementExit(node);

if (finallyBlock != null) {
flow.tryFinallyStatement_finallyBegin(
Expand Down
18 changes: 18 additions & 0 deletions pkg/analyzer/test/src/diagnostics/dead_code_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ int f(Foo foo) {
error(HintCode.DEAD_CODE, 111, 10),
]);
}

test_try_finally() async {
await assertErrorsInCode('''
main() {
try {
foo();
print('dead');
} finally {
print('alive');
}
print('dead');
}
Never foo() => throw 'exception';
''', [
error(HintCode.DEAD_CODE, 32, 14),
error(HintCode.DEAD_CODE, 87, 14),
]);
}
}

mixin DeadCodeTestCases on PubPackageResolutionTest {
Expand Down

0 comments on commit 4104427

Please sign in to comment.