Skip to content

Commit 6067833

Browse files
pqCommit Queue
authored and
Commit Queue
committed
[wildcards] report dead late wildcard variable initializers
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]>
1 parent 9913084 commit 6067833

File tree

7 files changed

+74
-2
lines changed

7 files changed

+74
-2
lines changed

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#
4646
# Stats:
4747
# - 42 "needsEvaluation"
48-
# - 306 "needsFix"
48+
# - 307 "needsFix"
4949
# - 441 "hasFix"
5050
# - 517 "noFix"
5151

@@ -3389,6 +3389,10 @@ WarningCode.DEAD_CODE:
33893389
status: hasFix
33903390
WarningCode.DEAD_CODE_CATCH_FOLLOWING_CATCH:
33913391
status: hasFix
3392+
WarningCode.DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER:
3393+
status: needsFix
3394+
notes: |-
3395+
Remove the initializer or remove the late keyword.
33923396
WarningCode.DEAD_CODE_ON_CATCH_SUBTYPE:
33933397
status: hasFix
33943398
WarningCode.DEPRECATED_EXPORT_USE:

pkg/analyzer/lib/src/error/codes.g.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6288,6 +6288,19 @@ class WarningCode extends AnalyzerErrorCode {
62886288
hasPublishedDocs: true,
62896289
);
62906290

6291+
/// No parameters.
6292+
static const WarningCode DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER =
6293+
WarningCode(
6294+
'DEAD_CODE',
6295+
"Dead code: The assigned-to wildcard variable is marked late and can never "
6296+
"be referenced so this initializer will never be evaluated.",
6297+
correctionMessage:
6298+
"Try removing the code, removing the late modifier or changing the "
6299+
"variable to a non-wildcard.",
6300+
hasPublishedDocs: true,
6301+
uniqueName: 'DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER',
6302+
);
6303+
62916304
/// Dead code is code that is never reached. This case covers cases where the
62926305
/// user has an on-catch clause such as `on A catch (e)`, where a supertype of
62936306
/// `A` was already caught.

pkg/analyzer/lib/src/error/dead_code_verifier.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,23 @@ class DeadCodeVerifier extends RecursiveAstVisitor<void> {
111111
});
112112
}
113113

114+
@override
115+
void visitVariableDeclaration(VariableDeclaration node) {
116+
var initializer = node.initializer;
117+
if (initializer != null && node.isLate) {
118+
var element = node.declaredElement;
119+
// TODO(pq): ask the LocalVariableElement once implemented
120+
if (_wildCardVariablesEnabled &&
121+
element is LocalVariableElement &&
122+
element.name == '_') {
123+
_errorReporter.atNode(initializer,
124+
WarningCode.DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER);
125+
}
126+
}
127+
128+
super.visitVariableDeclaration(node);
129+
}
130+
114131
/// Resolve the names in the given [combinator] in the scope of the given
115132
/// [library].
116133
void _checkCombinator(LibraryElement library, Combinator combinator) {

pkg/analyzer/lib/src/error/error_code_values.g.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,7 @@ const List<ErrorCode> errorCodeValues = [
983983
WarningCode.CONSTANT_PATTERN_NEVER_MATCHES_VALUE_TYPE,
984984
WarningCode.DEAD_CODE,
985985
WarningCode.DEAD_CODE_CATCH_FOLLOWING_CATCH,
986+
WarningCode.DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER,
986987
WarningCode.DEAD_CODE_ON_CATCH_SUBTYPE,
987988
WarningCode.DEPRECATED_EXPORT_USE,
988989
WarningCode.DEPRECATED_EXTENDS_FUNCTION,

pkg/analyzer/messages.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23360,6 +23360,12 @@ WarningCode:
2336023360
}
2336123361
}
2336223362
```
23363+
DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER:
23364+
problemMessage: "Dead code: The assigned-to wildcard variable is marked late and can never be referenced so this initializer will never be evaluated."
23365+
correctionMessage: Try removing the code, removing the late modifier or changing the variable to a non-wildcard.
23366+
sharedName: DEAD_CODE
23367+
hasPublishedDocs: true
23368+
comment: No parameters.
2336323369
DEAD_CODE_ON_CATCH_SUBTYPE:
2336423370
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."
2336523371
correctionMessage: Try reordering the catch clauses so that this block can be reached, or removing the unreachable catch clause.

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,17 @@ void f(Object x) {
178178

179179
@reflectiveTest
180180
class DeadCodeTest_Language219 extends PubPackageResolutionTest
181-
with WithLanguage219Mixin, DeadCodeTestCases_Language212 {}
181+
with WithLanguage219Mixin, DeadCodeTestCases_Language212 {
182+
@override
183+
test_lateWildCardVariable_initializer() async {
184+
await assertNoErrorsInCode(r'''
185+
f() {
186+
// Not a wildcard variable.
187+
late var _ = 0;
188+
}
189+
''');
190+
}
191+
}
182192

183193
mixin DeadCodeTestCases_Language212 on PubPackageResolutionTest {
184194
@override
@@ -1192,6 +1202,24 @@ void g(Never f) {
11921202
]);
11931203
}
11941204

1205+
test_lateWildCardVariable_initializer() async {
1206+
await assertErrorsInCode(r'''
1207+
f() {
1208+
late var _ = 0;
1209+
}
1210+
''', [
1211+
error(WarningCode.DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER, 21, 1),
1212+
]);
1213+
}
1214+
1215+
test_lateWildCardVariable_noInitializer() async {
1216+
await assertNoErrorsInCode(r'''
1217+
f() {
1218+
late var _;
1219+
}
1220+
''');
1221+
}
1222+
11951223
test_notUnassigned_propertyAccess() async {
11961224
await assertNoErrorsInCode(r'''
11971225
void f(int? i) {

pkg/analyzer/tool/diagnostics/diagnostics.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3651,6 +3651,9 @@ void g() {
36513651

36523652
_Dead code._
36533653

3654+
_Dead code: The assigned-to wildcard variable is marked late and can never be
3655+
referenced so this initializer will never be evaluated._
3656+
36543657
#### Description
36553658

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

0 commit comments

Comments
 (0)