Analyzer: fix dead code detection in top level declarations.

Fixes #49701.

Bug: https://github.com/dart-lang/sdk/issues/49701
Change-Id: I77fa7faf1dbfcda04a73eedcf0614c2f852b1dac
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/275041
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
This commit is contained in:
Paul Berry 2022-12-13 14:13:18 +00:00 committed by Commit Queue
parent 87f2429051
commit a29cd031d3
2 changed files with 27 additions and 0 deletions

View file

@ -59,6 +59,7 @@ class VariableDeclarationResolver {
if (isTopLevel) {
_resolver.flowAnalysis.topLevelDeclaration_exit();
_resolver.nullSafetyDeadCodeVerifier.flowEnd(node);
} else if (element.isLate) {
_resolver.flowAnalysis.flow?.lateInitializer_end();
}

View file

@ -101,6 +101,20 @@ void f() {
]);
}
test_class_field_initializer_listLiteral() async {
// Based on https://github.com/dart-lang/sdk/issues/49701
await assertErrorsInCode(
'''
Never f() { throw ''; }
class C {
static final x = [1, 2, f(), 4];
}
''',
isNullSafetyEnabled ? [error(HintCode.DEAD_CODE, 66, 2)] : [],
);
}
test_deadBlock_conditionalElse() async {
await assertErrorsInCode(r'''
f() {
@ -939,6 +953,18 @@ void f(int a) {
''', expectedErrors);
}
test_topLevelVariable_initializer_listLiteral() async {
// Based on https://github.com/dart-lang/sdk/issues/49701
await assertErrorsInCode(
'''
Never f() { throw ''; }
var x = [1, 2, f(), 4];
''',
isNullSafetyEnabled ? [error(HintCode.DEAD_CODE, 45, 2)] : [],
);
}
test_yield() async {
await assertErrorsInCode(r'''
Iterable<int> f() sync* {