linter: Fix bug in no_dynamic_casts w.r.t. Object? for-loop variable

I discovered this bug while migrating Flutter to the new lint rule.

Change-Id: I7944adff58299907e50bf0b2af3b992ec25721ff
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/510142
Auto-Submit: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Sam Rawlins
2026-06-08 15:04:42 -07:00
committed by Samuel Rawlins
parent 52f1a4ef54
commit d7d118f75e
3 changed files with 11 additions and 3 deletions
+1 -1
View File
@@ -120,7 +120,7 @@ To learn more about the feature, check out the
- A `no_raw_types` lint rule is introduced, which replaces the
`strict-raw-types` analysis option, offering a more consistent approach.
- A `no_dynamic_types` lint rule is introduced, which replaces the
- A `no_dynamic_casts` lint rule is introduced, which replaces the
`strict-casts` analysis option, offering a more consistent approach.
- The following lint rules have been determined to be low value, and are
deprecated: `avoid_public_typedef_functions`, and `one_member_abstracts`.
@@ -306,8 +306,8 @@ class _Visitor extends SimpleAstVisitor<void> {
elementType = iterableType;
}
if (elementType is! DynamicType) return;
if (targetType is DynamicType) return;
if (targetType == _context.typeProvider.objectQuestionType) return;
if (loopVarType is DynamicType) return;
if (loopVarType == _context.typeProvider.objectQuestionType) return;
_rule.reportAtNode(node.iterable);
}
@@ -121,6 +121,14 @@ void f(List<dynamic> list) {
''');
}
test_forEach_variable_objectQuestionTarget() async {
await assertNoDiagnostics(r'''
void f(List<dynamic> list) {
for (Object? x in list) {}
}
''');
}
test_listLiteral() async {
await assertDiagnosticsFromMarkdown(r'''
void f(dynamic a) {