@@ -134,7 +134,7 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
134
134
135
135
@override
136
136
void visitAsExpression (AsExpression node) {
137
- if (isUnnecessaryCast (node, _typeSystem)) {
137
+ if (_isUnnecessaryCast (node, _typeSystem)) {
138
138
_errorReporter.atNode (
139
139
node,
140
140
WarningCode .UNNECESSARY_CAST ,
@@ -1509,30 +1509,34 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
1509
1509
return _workspacePackage.contains (library.firstFragment.source);
1510
1510
}
1511
1511
1512
+ static String _formalParameterNameOrEmpty (FormalParameter node) {
1513
+ return node.name? .lexeme ?? '' ;
1514
+ }
1515
+
1516
+ static bool _hasNonVirtualAnnotation (ExecutableElement2 element) {
1517
+ if (element is PropertyAccessorElement2 && element.isSynthetic) {
1518
+ var variable = element.variable3;
1519
+ if (variable != null && variable.metadata2.hasNonVirtual) {
1520
+ return true ;
1521
+ }
1522
+ }
1523
+ return element.metadata2.hasNonVirtual;
1524
+ }
1525
+
1512
1526
/// Checks for the passed as expression for the [WarningCode.UNNECESSARY_CAST]
1513
1527
/// hint code.
1514
1528
///
1515
1529
/// Returns `true` if and only if an unnecessary cast hint should be generated
1516
1530
/// on [node] . See [WarningCode.UNNECESSARY_CAST] .
1517
- static bool isUnnecessaryCast (AsExpression node, TypeSystemImpl typeSystem) {
1531
+ static bool _isUnnecessaryCast (AsExpression node, TypeSystemImpl typeSystem) {
1518
1532
var leftType = node.expression.typeOrThrow;
1519
1533
var rightType = node.type.typeOrThrow;
1520
1534
1521
- // `dynamicValue as SomeType` is a valid use case.
1522
- if (leftType is DynamicType ) {
1523
- return false ;
1524
- }
1525
-
1526
1535
// `cannotResolve is SomeType` is already reported.
1527
1536
if (leftType is InvalidType ) {
1528
1537
return false ;
1529
1538
}
1530
1539
1531
- // `x as dynamic` is a valid use case.
1532
- if (rightType is DynamicType ) {
1533
- return false ;
1534
- }
1535
-
1536
1540
// `x as Unresolved` is already reported as an error.
1537
1541
if (rightType is InvalidType ) {
1538
1542
return false ;
@@ -1543,21 +1547,21 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
1543
1547
return false ;
1544
1548
}
1545
1549
1546
- return true ;
1547
- }
1548
-
1549
- static String _formalParameterNameOrEmpty ( FormalParameter node ) {
1550
- return node.name ? .lexeme ?? '' ;
1551
- }
1550
+ // `x as dynamic` is a valid use case. The explicit cast is a recommended
1551
+ // way to dynamically call a `Function` when the `avoid_dynamic_calls` lint
1552
+ // rule is enabled.
1553
+ if (rightType is DynamicType ) {
1554
+ return false ;
1555
+ }
1552
1556
1553
- static bool _hasNonVirtualAnnotation (ExecutableElement2 element) {
1554
- if (element is PropertyAccessorElement2 && element.isSynthetic) {
1555
- var variable = element.variable3;
1556
- if (variable != null && variable.metadata2.hasNonVirtual) {
1557
- return true ;
1558
- }
1557
+ // `x as Function` is a valid use case. The explicit cast is a recommended
1558
+ // way to dynamically call a `Function` when the `avoid_dynamic_calls` lint
1559
+ // rule is enabled.
1560
+ if (rightType.isDartCoreFunction) {
1561
+ return false ;
1559
1562
}
1560
- return element.metadata2.hasNonVirtual;
1563
+
1564
+ return true ;
1561
1565
}
1562
1566
}
1563
1567
0 commit comments