Skip to content

Commit 53ba44f

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer] Improve dead code highlighting for null-aware expressions
Previously, when a null-aware property access, method invocation, or index invocation was performed on a variable that was definitely unassigned, the entire expression was highlighted as dead code. This change modifies the `DeadCodeVerifier` to highlight only from the null-aware operator (`?.`, `?[]`, etc.) to the end of the expression. This provides a more precise and helpful diagnostic by highlighting the exact portion of the code that can be safely removed. Fixes #61251. Bug: #61251 Change-Id: I4792b84d16a797f70b752a20e6affa69f3fe3d8c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/444040 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 333460e commit 53ba44f

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ class NullSafetyDeadCodeVerifier {
472472
var flowAnalysis = _flowAnalysis;
473473
if (flowAnalysis == null) return;
474474

475-
var operatorType = operator?.type;
475+
if (operator == null) return;
476+
var operatorType = operator.type;
476477
if (operatorType != TokenType.QUESTION &&
477478
operatorType != TokenType.QUESTION_PERIOD &&
478479
operatorType != TokenType.QUESTION_PERIOD_PERIOD) {
@@ -494,7 +495,11 @@ class NullSafetyDeadCodeVerifier {
494495
node = parent!;
495496
parent = node.parent;
496497
}
497-
_diagnosticReporter.atNode(node, WarningCode.DEAD_CODE);
498+
_diagnosticReporter.atOffset(
499+
offset: operator.offset,
500+
length: node.end - operator.offset,
501+
diagnosticCode: WarningCode.DEAD_CODE,
502+
);
498503
}
499504
}
500505
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,7 @@ void f() {
22342234
l?..[0]..length;
22352235
}
22362236
''',
2237-
[error(WarningCode.DEAD_CODE, 29, 15)],
2237+
[error(WarningCode.DEAD_CODE, 30, 14)],
22382238
);
22392239
}
22402240

@@ -2246,7 +2246,7 @@ void f() {
22462246
i?..toInt()..isEven;
22472247
}
22482248
''',
2249-
[error(WarningCode.DEAD_CODE, 23, 19)],
2249+
[error(WarningCode.DEAD_CODE, 24, 18)],
22502250
);
22512251
}
22522252

@@ -2258,7 +2258,7 @@ void f() {
22582258
i?..sign..isEven;
22592259
}
22602260
''',
2261-
[error(WarningCode.DEAD_CODE, 23, 16)],
2261+
[error(WarningCode.DEAD_CODE, 24, 15)],
22622262
);
22632263
}
22642264

@@ -2270,7 +2270,7 @@ void f() {
22702270
l?[0];
22712271
}
22722272
''',
2273-
[error(WarningCode.DEAD_CODE, 29, 5)],
2273+
[error(WarningCode.DEAD_CODE, 30, 4)],
22742274
);
22752275
}
22762276

@@ -2282,7 +2282,7 @@ void f() {
22822282
l?[0][0];
22832283
}
22842284
''',
2285-
[error(WarningCode.DEAD_CODE, 35, 8)],
2285+
[error(WarningCode.DEAD_CODE, 36, 7)],
22862286
);
22872287
}
22882288

@@ -2294,7 +2294,7 @@ void f() {
22942294
i?.truncate();
22952295
}
22962296
''',
2297-
[error(WarningCode.DEAD_CODE, 23, 13)],
2297+
[error(WarningCode.DEAD_CODE, 24, 12)],
22982298
);
22992299
}
23002300

@@ -2306,7 +2306,7 @@ void f() {
23062306
i?.truncate().truncate();
23072307
}
23082308
''',
2309-
[error(WarningCode.DEAD_CODE, 23, 24)],
2309+
[error(WarningCode.DEAD_CODE, 24, 23)],
23102310
);
23112311
}
23122312

@@ -2318,7 +2318,7 @@ void f() {
23182318
i?.truncate().sign;
23192319
}
23202320
''',
2321-
[error(WarningCode.DEAD_CODE, 23, 18)],
2321+
[error(WarningCode.DEAD_CODE, 24, 17)],
23222322
);
23232323
}
23242324

@@ -2330,7 +2330,7 @@ void f() {
23302330
(i)?.sign;
23312331
}
23322332
''',
2333-
[error(WarningCode.DEAD_CODE, 23, 9)],
2333+
[error(WarningCode.DEAD_CODE, 26, 6)],
23342334
);
23352335
}
23362336

@@ -2342,7 +2342,7 @@ void f() {
23422342
(i)?.sign.sign;
23432343
}
23442344
''',
2345-
[error(WarningCode.DEAD_CODE, 23, 14)],
2345+
[error(WarningCode.DEAD_CODE, 26, 11)],
23462346
);
23472347
}
23482348

0 commit comments

Comments
 (0)