From 5810cd80bd375ba7505df08eda51b1dcb6c980cd Mon Sep 17 00:00:00 2001 From: Hixie Date: Wed, 9 Mar 2016 14:06:42 -0800 Subject: [PATCH] Avoid 'as' keyword It has performance implications in release mode. We'll enable the lint once https://github.com/dart-lang/linter/issues/195 is fixed. --- .../flutter/test/rendering/stack_test.dart | 3 +- .../test/widget/asset_vendor_test.dart | 6 +-- .../flutter/test/widget/build_scope_test.dart | 6 ++- .../flutter/test/widget/coordinates_test.dart | 12 +++--- packages/flutter/test/widget/input_test.dart | 9 +++-- .../flutter/test/widget/parent_data_test.dart | 3 +- .../test/widget/reparent_state_test.dart | 39 ++++++++++++------- .../flutter_test/lib/src/instrumentation.dart | 4 +- .../lib/src/application_package.dart | 2 +- .../lib/src/commands/analyze.dart | 9 +++-- 10 files changed, 57 insertions(+), 36 deletions(-) diff --git a/packages/flutter/test/rendering/stack_test.dart b/packages/flutter/test/rendering/stack_test.dart index 304a747f6d..41c9b6d269 100644 --- a/packages/flutter/test/rendering/stack_test.dart +++ b/packages/flutter/test/rendering/stack_test.dart @@ -27,7 +27,8 @@ void main() { ); RenderBox stack = new RenderStack(children: [red, green]); - (green.parentData as StackParentData) + StackParentData greenParentData = green.parentData; + greenParentData ..top = 0.0 ..right = 0.0 ..bottom = 0.0 diff --git a/packages/flutter/test/widget/asset_vendor_test.dart b/packages/flutter/test/widget/asset_vendor_test.dart index 7bacc19cdc..99b15d005b 100644 --- a/packages/flutter/test/widget/asset_vendor_test.dart +++ b/packages/flutter/test/widget/asset_vendor_test.dart @@ -69,7 +69,7 @@ class TestAssetBundle extends AssetBundle { } Future testDecodeImageFromDataPipe(core.MojoDataPipeConsumer pipe) { - TestMojoDataPipeConsumer testPipe = pipe as TestMojoDataPipeConsumer; + TestMojoDataPipeConsumer testPipe = pipe; assert(testPipe != null); ui.Image image = new TestImage(testPipe.scale); return (new Completer()..complete(image)).future; @@ -108,11 +108,11 @@ Widget buildImageAtRatio(String image, Key key, double ratio, bool inferSize) { } RenderImage getRenderImage(tester, Key key) { - return tester.findElementByKey(key).renderObject as RenderImage; + return tester.findElementByKey(key).renderObject; } TestImage getTestImage(tester, Key key) { - return getRenderImage(tester, key).image as TestImage; + return getRenderImage(tester, key).image; } void pumpTreeToLayout(WidgetTester tester, Widget widget) { diff --git a/packages/flutter/test/widget/build_scope_test.dart b/packages/flutter/test/widget/build_scope_test.dart index 8751ccfd89..b79e30df05 100644 --- a/packages/flutter/test/widget/build_scope_test.dart +++ b/packages/flutter/test/widget/build_scope_test.dart @@ -104,10 +104,12 @@ void main() { right: new ProbeWidget() )); expect(ProbeWidgetState.buildCount, equals(2)); - (flipKey.currentState as FlipComponentState).flip(); + FlipComponentState flipState1 = flipKey.currentState; + flipState1.flip(); tester.pump(); expect(ProbeWidgetState.buildCount, equals(3)); - (flipKey.currentState as FlipComponentState).flip(); + FlipComponentState flipState2 = flipKey.currentState; + flipState2.flip(); tester.pump(); expect(ProbeWidgetState.buildCount, equals(3)); tester.pumpWidget(new Container()); diff --git a/packages/flutter/test/widget/coordinates_test.dart b/packages/flutter/test/widget/coordinates_test.dart index 3f472aefe9..b474dca630 100644 --- a/packages/flutter/test/widget/coordinates_test.dart +++ b/packages/flutter/test/widget/coordinates_test.dart @@ -38,14 +38,12 @@ void main() { ) ); - expect((tester.findElementByKey(keyA).renderObject as RenderBox).localToGlobal(const Point(0.0, 0.0)), - equals(const Point(100.0, 100.0))); + RenderBox boxA = tester.findElementByKey(keyA).renderObject; + expect(boxA.localToGlobal(const Point(0.0, 0.0)), equals(const Point(100.0, 100.0))); - expect((tester.findElementByKey(keyB).renderObject as RenderBox).localToGlobal(const Point(0.0, 0.0)), - equals(const Point(100.0, 200.0))); - - expect((tester.findElementByKey(keyB).renderObject as RenderBox).globalToLocal(const Point(110.0, 205.0)), - equals(const Point(10.0, 5.0))); + RenderBox boxB = tester.findElementByKey(keyB).renderObject; + expect(boxB.localToGlobal(const Point(0.0, 0.0)), equals(const Point(100.0, 200.0))); + expect(boxB.globalToLocal(const Point(110.0, 205.0)), equals(const Point(10.0, 5.0))); }); }); } diff --git a/packages/flutter/test/widget/input_test.dart b/packages/flutter/test/widget/input_test.dart index 6e5762880c..bbccae3f93 100644 --- a/packages/flutter/test/widget/input_test.dart +++ b/packages/flutter/test/widget/input_test.dart @@ -48,7 +48,8 @@ void main() { tester.pumpWidget(builder()); Element input = tester.findElementByKey(inputKey); - Size emptyInputSize = (input.renderObject as RenderBox).size; + RenderBox inputBox = input.renderObject; + Size emptyInputSize = inputBox.size; void enterText(String testValue) { // Simulate entry of text through the keyboard. @@ -65,10 +66,12 @@ void main() { } enterText(' '); - expect((input.renderObject as RenderBox).size, equals(emptyInputSize)); + expect(input.renderObject, equals(inputBox)); + expect(inputBox.size, equals(emptyInputSize)); enterText('Test'); - expect((input.renderObject as RenderBox).size, equals(emptyInputSize)); + expect(input.renderObject, equals(inputBox)); + expect(inputBox.size, equals(emptyInputSize)); }); }); diff --git a/packages/flutter/test/widget/parent_data_test.dart b/packages/flutter/test/widget/parent_data_test.dart index 6aa060248c..940d88d70b 100644 --- a/packages/flutter/test/widget/parent_data_test.dart +++ b/packages/flutter/test/widget/parent_data_test.dart @@ -35,7 +35,8 @@ void checkTree(WidgetTester tester, List expectedParentData) { expect(parentData.right, equals(expected.right)); expect(parentData.bottom, equals(expected.bottom)); expect(parentData.left, equals(expected.left)); - child = (decoratedBox.parentData as StackParentData).nextSibling; + StackParentData decoratedBoxParentData = decoratedBox.parentData; + child = decoratedBoxParentData.nextSibling; } expect(child, isNull); } catch (e) { diff --git a/packages/flutter/test/widget/reparent_state_test.dart b/packages/flutter/test/widget/reparent_state_test.dart index 4577d9ae76..c344826711 100644 --- a/packages/flutter/test/widget/reparent_state_test.dart +++ b/packages/flutter/test/widget/reparent_state_test.dart @@ -47,8 +47,10 @@ void main() { ) ); - (left.currentState as StateMarkerState).marker = "left"; - (right.currentState as StateMarkerState).marker = "right"; + StateMarkerState leftState = left.currentState; + leftState.marker = "left"; + StateMarkerState rightState = right.currentState; + rightState.marker = "right"; StateMarkerState grandchildState = tester.findStateByConfig(grandchild); expect(grandchildState, isNotNull); @@ -71,8 +73,10 @@ void main() { ) ); - expect((left.currentState as StateMarkerState).marker, equals("left")); - expect((right.currentState as StateMarkerState).marker, equals("right")); + expect(left.currentState, equals(leftState)); + expect(leftState.marker, equals("left")); + expect(right.currentState, equals(rightState)); + expect(rightState.marker, equals("right")); StateMarkerState newGrandchildState = tester.findStateByConfig(newGrandchild); expect(newGrandchildState, isNotNull); @@ -90,7 +94,8 @@ void main() { ) ); - expect((left.currentState as StateMarkerState).marker, equals("left")); + expect(left.currentState, equals(leftState)); + expect(leftState.marker, equals("left")); expect(right.currentState, isNull); }); }); @@ -113,8 +118,10 @@ void main() { ) ); - (left.currentState as StateMarkerState).marker = "left"; - (right.currentState as StateMarkerState).marker = "right"; + StateMarkerState leftState = left.currentState; + leftState.marker = "left"; + StateMarkerState rightState = right.currentState; + rightState.marker = "right"; StateMarkerState grandchildState = tester.findStateByConfig(grandchild); expect(grandchildState, isNotNull); @@ -133,8 +140,10 @@ void main() { ) ); - expect((left.currentState as StateMarkerState).marker, equals("left")); - expect((right.currentState as StateMarkerState).marker, equals("right")); + expect(left.currentState, equals(leftState)); + expect(leftState.marker, equals("left")); + expect(right.currentState, equals(rightState)); + expect(rightState.marker, equals("right")); StateMarkerState newGrandchildState = tester.findStateByConfig(newGrandchild); expect(newGrandchildState, isNotNull); @@ -152,7 +161,8 @@ void main() { ) ); - expect((left.currentState as StateMarkerState).marker, equals("left")); + expect(left.currentState, equals(leftState)); + expect(leftState.marker, equals("left")); expect(right.currentState, isNull); }); }); @@ -163,7 +173,8 @@ void main() { tester.pumpWidget(new StateMarker(key: key)); - (key.currentState as StateMarkerState).marker = "marked"; + StateMarkerState keyState = key.currentState; + keyState.marker = "marked"; tester.pumpWidget(new ScrollableList( itemExtent: 100.0, @@ -176,11 +187,13 @@ void main() { ] )); - expect((key.currentState as StateMarkerState).marker, equals("marked")); + expect(key.currentState, equals(keyState)); + expect(keyState.marker, equals("marked")); tester.pumpWidget(new StateMarker(key: key)); - expect((key.currentState as StateMarkerState).marker, equals("marked")); + expect(key.currentState, equals(keyState)); + expect(keyState.marker, equals("marked")); }); }); } diff --git a/packages/flutter_test/lib/src/instrumentation.dart b/packages/flutter_test/lib/src/instrumentation.dart index 6d75c89b14..24f390958e 100644 --- a/packages/flutter_test/lib/src/instrumentation.dart +++ b/packages/flutter_test/lib/src/instrumentation.dart @@ -138,14 +138,14 @@ class Instrumentation { /// the element's render object has been laid out at least once. Size getSize(Element element) { assert(element != null); - RenderBox box = element.renderObject as RenderBox; + RenderBox box = element.renderObject; assert(box != null); return box.size; } Point _getElementPoint(Element element, SizeToPointFunction sizeToPoint) { assert(element != null); - RenderBox box = element.renderObject as RenderBox; + RenderBox box = element.renderObject; assert(box != null); return box.localToGlobal(sizeToPoint(box.size)); } diff --git a/packages/flutter_tools/lib/src/application_package.dart b/packages/flutter_tools/lib/src/application_package.dart index 2f77972ec2..dc37efd760 100644 --- a/packages/flutter_tools/lib/src/application_package.dart +++ b/packages/flutter_tools/lib/src/application_package.dart @@ -68,7 +68,7 @@ class AndroidApk extends ApplicationPackage { String launchActivity; for (xml.XmlElement category in document.findAllElements('category')) { if (category.getAttribute('android:name') == 'android.intent.category.LAUNCHER') { - xml.XmlElement activity = category.parent.parent as xml.XmlElement; + xml.XmlElement activity = category.parent.parent; String activityName = activity.getAttribute('android:name'); launchActivity = "$id/$activityName"; break; diff --git a/packages/flutter_tools/lib/src/commands/analyze.dart b/packages/flutter_tools/lib/src/commands/analyze.dart index 210d06e7e8..7b5da71ff5 100644 --- a/packages/flutter_tools/lib/src/commands/analyze.dart +++ b/packages/flutter_tools/lib/src/commands/analyze.dart @@ -294,9 +294,10 @@ class AnalyzeCommand extends FlutterCommand { for (String package in packages.keys) packagesBody.writeln('$package:${path.toUri(packages[package])}'); - /// specify analysis options - /// note that until there is a default "all-in" lint rule-set we need - /// to opt-in to all desired lints (https://github.com/dart-lang/sdk/issues/25843) + /// Specify analysis options. + /// Note that until there is a default "all-in" lint rule-set we need + /// to opt-in to all desired lints (https://github.com/dart-lang/sdk/issues/25843). + /// For a list of lints, see: http://dart-lang.github.io/linter/lints/ String optionsBody = ''' analyzer: errors: @@ -307,6 +308,8 @@ analyzer: todo: ignore linter: rules: + # we'll turn on avoid_as as soon as it doesn't complain about "as dynamic" + # - avoid_as - camel_case_types # sometimes we have no choice (e.g. when matching other platforms) # - constant_identifier_names