Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(529)

Unified Diff: lib/src/rules/avoid_as.dart

Issue 1415183009: Adds check for `AsExpression`s (Fix #145). (Closed) Base URL: https://github.com/dart-lang/linter.git@master
Patch Set: reorg Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/rules.dart ('k') | test/rule_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/rules/avoid_as.dart
diff --git a/lib/src/rules/avoid_as.dart b/lib/src/rules/avoid_as.dart
new file mode 100644
index 0000000000000000000000000000000000000000..104d18ca04da431e7bbd0ca8c0864e790ab0ce92
--- /dev/null
+++ b/lib/src/rules/avoid_as.dart
@@ -0,0 +1,58 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library linter.src.rules.avoid_as;
+
+import 'package:analyzer/src/generated/ast.dart'
+ show AsExpression, AstVisitor, SimpleAstVisitor;
+import 'package:linter/src/linter.dart';
+
+const desc = r'Avoid using `as`.';
+
+const details = r'''
+From the [flutter style guide]
+(https://github.com/flutter/engine/blob/master/sky/specs/style-guide.md):
+
+**AVOID** using `as`.
+
+If you know the type is correct, use an assertion or assign to a more
+narrowly-typed variable (this avoids the type check in release mode; `as`
+is not compiled out in release mode). If you don't know whether the type is
+correct, check using `is` (this avoids the exception that `as` raises).
+
+**BAD:**
+```
+try {
+ (pm as Person).firstName = 'Seth';
+} on CastError { }
+```
+
+**GOOD:**
+```
+Person person = pm;
+person.firstName = 'Seth';
+```
+''';
+
+class AvoidAs extends LintRule {
+ AvoidAs()
+ : super(
+ name: 'avoid_as',
+ description: desc,
+ details: details,
+ group: Group.style);
+
+ @override
+ AstVisitor getVisitor() => new Visitor(this);
+}
+
+class Visitor extends SimpleAstVisitor {
+ final LintRule rule;
+ Visitor(this.rule);
+
+ @override
+ visitAsExpression(AsExpression node) {
+ rule.reportLint(node);
+ }
+}
« no previous file with comments | « lib/src/rules.dart ('k') | test/rule_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698