Skip to content

Commit

Permalink
[wildcards] report dead late wildcard variable initializers
Browse files Browse the repository at this point in the history
Fixes: #55905

Change-Id: I78f40c754ae2daf0894b7a2b114a739c60214f72
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/375200
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Phil Quitslund <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Jul 12, 2024
1 parent 9913084 commit 6067833
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#
# Stats:
# - 42 "needsEvaluation"
# - 306 "needsFix"
# - 307 "needsFix"
# - 441 "hasFix"
# - 517 "noFix"

Expand Down Expand Up @@ -3389,6 +3389,10 @@ WarningCode.DEAD_CODE:
status: hasFix
WarningCode.DEAD_CODE_CATCH_FOLLOWING_CATCH:
status: hasFix
WarningCode.DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER:
status: needsFix
notes: |-
Remove the initializer or remove the late keyword.
WarningCode.DEAD_CODE_ON_CATCH_SUBTYPE:
status: hasFix
WarningCode.DEPRECATED_EXPORT_USE:
Expand Down
13 changes: 13 additions & 0 deletions pkg/analyzer/lib/src/error/codes.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6288,6 +6288,19 @@ class WarningCode extends AnalyzerErrorCode {
hasPublishedDocs: true,
);

/// No parameters.
static const WarningCode DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER =
WarningCode(
'DEAD_CODE',
"Dead code: The assigned-to wildcard variable is marked late and can never "
"be referenced so this initializer will never be evaluated.",
correctionMessage:
"Try removing the code, removing the late modifier or changing the "
"variable to a non-wildcard.",
hasPublishedDocs: true,
uniqueName: 'DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER',
);

/// Dead code is code that is never reached. This case covers cases where the
/// user has an on-catch clause such as `on A catch (e)`, where a supertype of
/// `A` was already caught.
Expand Down
17 changes: 17 additions & 0 deletions pkg/analyzer/lib/src/error/dead_code_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ class DeadCodeVerifier extends RecursiveAstVisitor<void> {
});
}

@override
void visitVariableDeclaration(VariableDeclaration node) {
var initializer = node.initializer;
if (initializer != null && node.isLate) {
var element = node.declaredElement;
// TODO(pq): ask the LocalVariableElement once implemented
if (_wildCardVariablesEnabled &&
element is LocalVariableElement &&
element.name == '_') {
_errorReporter.atNode(initializer,
WarningCode.DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER);
}
}

super.visitVariableDeclaration(node);
}

/// Resolve the names in the given [combinator] in the scope of the given
/// [library].
void _checkCombinator(LibraryElement library, Combinator combinator) {
Expand Down
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/error/error_code_values.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@ const List<ErrorCode> errorCodeValues = [
WarningCode.CONSTANT_PATTERN_NEVER_MATCHES_VALUE_TYPE,
WarningCode.DEAD_CODE,
WarningCode.DEAD_CODE_CATCH_FOLLOWING_CATCH,
WarningCode.DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER,
WarningCode.DEAD_CODE_ON_CATCH_SUBTYPE,
WarningCode.DEPRECATED_EXPORT_USE,
WarningCode.DEPRECATED_EXTENDS_FUNCTION,
Expand Down
6 changes: 6 additions & 0 deletions pkg/analyzer/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23360,6 +23360,12 @@ WarningCode:
}
}
```
DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER:
problemMessage: "Dead code: The assigned-to wildcard variable is marked late and can never be referenced so this initializer will never be evaluated."
correctionMessage: Try removing the code, removing the late modifier or changing the variable to a non-wildcard.
sharedName: DEAD_CODE
hasPublishedDocs: true
comment: No parameters.
DEAD_CODE_ON_CATCH_SUBTYPE:
problemMessage: "Dead code: This on-catch block won't be executed because '{0}' is a subtype of '{1}' and hence will have been caught already."
correctionMessage: Try reordering the catch clauses so that this block can be reached, or removing the unreachable catch clause.
Expand Down
30 changes: 29 additions & 1 deletion pkg/analyzer/test/src/diagnostics/dead_code_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,17 @@ void f(Object x) {

@reflectiveTest
class DeadCodeTest_Language219 extends PubPackageResolutionTest
with WithLanguage219Mixin, DeadCodeTestCases_Language212 {}
with WithLanguage219Mixin, DeadCodeTestCases_Language212 {
@override
test_lateWildCardVariable_initializer() async {
await assertNoErrorsInCode(r'''
f() {
// Not a wildcard variable.
late var _ = 0;
}
''');
}
}

mixin DeadCodeTestCases_Language212 on PubPackageResolutionTest {
@override
Expand Down Expand Up @@ -1192,6 +1202,24 @@ void g(Never f) {
]);
}

test_lateWildCardVariable_initializer() async {
await assertErrorsInCode(r'''
f() {
late var _ = 0;
}
''', [
error(WarningCode.DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER, 21, 1),
]);
}

test_lateWildCardVariable_noInitializer() async {
await assertNoErrorsInCode(r'''
f() {
late var _;
}
''');
}

test_notUnassigned_propertyAccess() async {
await assertNoErrorsInCode(r'''
void f(int? i) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/analyzer/tool/diagnostics/diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -3651,6 +3651,9 @@ void g() {

_Dead code._

_Dead code: The assigned-to wildcard variable is marked late and can never be
referenced so this initializer will never be evaluated._

#### Description

The analyzer produces this diagnostic when code is found that won't be
Expand Down

0 comments on commit 6067833

Please sign in to comment.