Skip to content

Commit e11eb2c

Browse files
[nnbd_migration] Fix #42263, tool inserting ! on toString/hashCode
Fixed: 42263 Change-Id: Ia4c6d3543bc7628c76b85d986b9709de58925884 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151441 Auto-Submit: Mike Fairhurst <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent 021a49e commit e11eb2c

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

pkg/nnbd_migration/lib/src/fix_builder.dart

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,13 @@ class FixReason_NullCheckHint implements SimpleFixReasonInfo {
293293

294294
/// Implementation of [MigrationResolutionHooks] that interfaces with
295295
/// [FixBuilder].
296-
class MigrationResolutionHooksImpl implements MigrationResolutionHooks {
296+
class MigrationResolutionHooksImpl
297+
with ResolutionUtils
298+
implements MigrationResolutionHooks {
297299
FixBuilder _fixBuilder;
298300

301+
TypeProvider get typeProvider => _fixBuilder.typeProvider;
302+
299303
final Expando<List<CollectionElement>> _collectionElements = Expando();
300304

301305
final Expando<bool> _shouldStayNullAware = Expando();
@@ -583,8 +587,18 @@ class MigrationResolutionHooksImpl implements MigrationResolutionHooks {
583587
return type;
584588
}
585589

590+
Element get _elementToString =>
591+
_fixBuilder.typeProvider.objectType.getMethod('toString');
592+
Element get _elementHashCode =>
593+
_fixBuilder.typeProvider.objectType.getGetter('hashCode');
594+
Element get _elementRuntimeType =>
595+
_fixBuilder.typeProvider.objectType.getMethod('runtimeType');
596+
Element get _elementNoSuchMethod =>
597+
_fixBuilder.typeProvider.objectType.getGetter('noSuchMethod');
598+
586599
bool _needsNullCheckDueToStructure(Expression node) {
587600
var parent = node.parent;
601+
588602
if (parent is BinaryExpression) {
589603
if (identical(node, parent.leftOperand)) {
590604
var operatorType = parent.operator.type;
@@ -597,16 +611,22 @@ class MigrationResolutionHooksImpl implements MigrationResolutionHooks {
597611
}
598612
}
599613
} else if (parent is PrefixedIdentifier) {
600-
// TODO(paulberry): ok for toString etc. if the shape is correct
614+
if (isDeclaredOnObject(parent.identifier.name)) {
615+
return false;
616+
}
601617
return identical(node, parent.prefix);
602618
} else if (parent is PropertyAccess) {
619+
if (isDeclaredOnObject(parent.propertyName.name)) {
620+
return false;
621+
}
603622
// TODO(paulberry): what about cascaded?
604-
// TODO(paulberry): ok for toString etc. if the shape is correct
605623
return parent.operator.type == TokenType.PERIOD &&
606624
identical(node, parent.target);
607625
} else if (parent is MethodInvocation) {
626+
if (isDeclaredOnObject(parent.methodName.name)) {
627+
return false;
628+
}
608629
// TODO(paulberry): what about cascaded?
609-
// TODO(paulberry): ok for toString etc. if the shape is correct
610630
return parent.operator.type == TokenType.PERIOD &&
611631
identical(node, parent.target);
612632
} else if (parent is IndexExpression) {

pkg/nnbd_migration/test/fix_builder_test.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,6 @@ int _g() => 1;
19051905
visitSubexpression(findNode.methodInvocation('_g();'), 'int');
19061906
}
19071907

1908-
@FailingTest(reason: 'TODO(paulberry)')
19091908
Future<void> test_methodInvocation_toString() async {
19101909
await analyze('''
19111910
abstract class _C {}
@@ -2379,7 +2378,6 @@ _f(_C c) => c.x;
23792378
visitSubexpression(findNode.prefixed('c.x'), 'int?');
23802379
}
23812380

2382-
@FailingTest(reason: 'TODO(paulberry)')
23832381
Future<void> test_prefixedIdentifier_object_getter() async {
23842382
await analyze('''
23852383
class _C {}
@@ -2388,7 +2386,6 @@ _f(_C/*?*/ c) => c.hashCode;
23882386
visitSubexpression(findNode.prefixed('c.hashCode'), 'int');
23892387
}
23902388

2391-
@FailingTest(reason: 'TODO(paulberry)')
23922389
Future<void> test_prefixedIdentifier_object_tearoff() async {
23932390
await analyze('''
23942391
class _C {}
@@ -2784,7 +2781,6 @@ _f(_C<int>/*?*/ c) => c?.x;
27842781
visitSubexpression(findNode.propertyAccess('c?.x'), 'List<int>?');
27852782
}
27862783

2787-
@FailingTest(reason: 'TODO(paulberry)')
27882784
Future<void> test_propertyAccess_object_getter() async {
27892785
await analyze('''
27902786
class _C {}
@@ -2793,7 +2789,6 @@ _f(_C/*?*/ c) => (c).hashCode;
27932789
visitSubexpression(findNode.propertyAccess('(c).hashCode'), 'int');
27942790
}
27952791

2796-
@FailingTest(reason: 'TODO(paulberry)')
27972792
Future<void> test_propertyAccess_object_tearoff() async {
27982793
await analyze('''
27992794
class _C {}

0 commit comments

Comments
 (0)