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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « lib/src/rules.dart ('k') | test/rule_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library linter.src.rules.avoid_as;
6
7 import 'package:analyzer/src/generated/ast.dart'
8 show AsExpression, AstVisitor, SimpleAstVisitor;
9 import 'package:linter/src/linter.dart';
10
11 const desc = r'Avoid using `as`.';
12
13 const details = r'''
14 From the [flutter style guide]
15 (https://github.com/flutter/engine/blob/master/sky/specs/style-guide.md):
16
17 **AVOID** using `as`.
18
19 If you know the type is correct, use an assertion or assign to a more
20 narrowly-typed variable (this avoids the type check in release mode; `as`
21 is not compiled out in release mode). If you don't know whether the type is
22 correct, check using `is` (this avoids the exception that `as` raises).
23
24 **BAD:**
25 ```
26 try {
27 (pm as Person).firstName = 'Seth';
28 } on CastError { }
29 ```
30
31 **GOOD:**
32 ```
33 Person person = pm;
34 person.firstName = 'Seth';
35 ```
36 ''';
37
38 class AvoidAs extends LintRule {
39 AvoidAs()
40 : super(
41 name: 'avoid_as',
42 description: desc,
43 details: details,
44 group: Group.style);
45
46 @override
47 AstVisitor getVisitor() => new Visitor(this);
48 }
49
50 class Visitor extends SimpleAstVisitor {
51 final LintRule rule;
52 Visitor(this.rule);
53
54 @override
55 visitAsExpression(AsExpression node) {
56 rule.reportLint(node);
57 }
58 }
OLDNEW
« 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