-
Notifications
You must be signed in to change notification settings - Fork 170
/
void_checks.dart
187 lines (164 loc) · 5.56 KB
/
void_checks.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) 2018, 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.
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import '../analyzer.dart';
const _desc = r"Don't assign to void.";
const _details = r'''
**DON'T** assign to void.
**BAD:**
```dart
class A<T> {
T value;
void test(T arg) { }
}
void main() {
A<void> a = A<void>();
a.value = 1; // LINT
a.test(1); // LINT
}
```
''';
class VoidChecks extends LintRule {
static const LintCode code = LintCode(
'void_checks', "Assignment to a variable of type 'void'.",
correctionMessage:
'Try removing the assignment or changing the type of the variable.');
VoidChecks()
: super(
name: 'void_checks',
description: _desc,
details: _details,
group: Group.style);
@override
LintCode get lintCode => code;
@override
void registerNodeProcessors(
NodeLintRegistry registry, LinterContext context) {
var visitor = _Visitor(this, context);
registry.addAssignedVariablePattern(this, visitor);
registry.addAssignmentExpression(this, visitor);
registry.addInstanceCreationExpression(this, visitor);
registry.addMethodInvocation(this, visitor);
registry.addReturnStatement(this, visitor);
}
}
class _Visitor extends SimpleAstVisitor<void> {
final LintRule rule;
final TypeSystem typeSystem;
_Visitor(this.rule, LinterContext context) : typeSystem = context.typeSystem;
bool isTypeAcceptableWhenExpectingFutureOrVoid(DartType type) {
if (type is DynamicType) return true;
if (isTypeAcceptableWhenExpectingVoid(type)) return true;
if (type.isDartAsyncFutureOr ||
type.isDartAsyncFuture &&
type is InterfaceType &&
isTypeAcceptableWhenExpectingFutureOrVoid(
type.typeArguments.first)) {
return true;
}
return false;
}
bool isTypeAcceptableWhenExpectingVoid(DartType type) {
if (type is VoidType) return true;
if (type.isDartCoreNull) return true;
if (type is NeverType) return true;
if (type.isDartAsyncFuture &&
type is InterfaceType &&
isTypeAcceptableWhenExpectingVoid(type.typeArguments.first)) {
return true;
}
return false;
}
@override
void visitAssignedVariablePattern(AssignedVariablePattern node) {
var valueType = node.matchedValueType;
var element = node.element;
if (element is! VariableElement) return;
_check(element.type, valueType, node);
}
@override
void visitAssignmentExpression(AssignmentExpression node) {
var type = node.writeType;
_check(type, node.rightHandSide.staticType, node,
checkedNode: node.rightHandSide);
}
@override
void visitInstanceCreationExpression(InstanceCreationExpression node) {
var args = node.argumentList.arguments;
var parameters = node.constructorName.staticElement?.parameters;
if (parameters != null) {
_checkArgs(args, parameters);
}
}
@override
void visitMethodInvocation(MethodInvocation node) {
var type = node.staticInvokeType;
if (type is FunctionType) {
var args = node.argumentList.arguments;
var parameters = type.parameters;
_checkArgs(args, parameters);
}
}
@override
void visitReturnStatement(ReturnStatement node) {
var parent = node.thisOrAncestorMatching((e) =>
e is FunctionExpression ||
e is MethodDeclaration ||
e is FunctionDeclaration);
if (parent is FunctionExpression) {
var type = parent.staticType;
if (type is FunctionType) {
_check(type.returnType, node.expression?.staticType, node,
checkedNode: node.expression);
}
} else if (parent is MethodDeclaration) {
_check(
parent.declaredElement?.returnType, node.expression?.staticType, node,
checkedNode: node.expression);
} else if (parent is FunctionDeclaration) {
_check(
parent.declaredElement?.returnType, node.expression?.staticType, node,
checkedNode: node.expression);
}
}
void _check(DartType? expectedType, DartType? type, AstNode node,
{AstNode? checkedNode}) {
checkedNode ??= node;
if (expectedType == null || type == null) {
return;
}
if (expectedType is VoidType &&
type is! DynamicType &&
node is ReturnStatement) {
return;
}
if (expectedType is VoidType && !isTypeAcceptableWhenExpectingVoid(type)) {
rule.reportLint(node);
} else if (expectedType.isDartAsyncFutureOr &&
(expectedType as InterfaceType).typeArguments.first is VoidType &&
!isTypeAcceptableWhenExpectingFutureOrVoid(type)) {
rule.reportLint(node);
} else if (checkedNode is FunctionExpression &&
checkedNode.body is! ExpressionFunctionBody &&
expectedType is FunctionType &&
type is FunctionType) {
_check(expectedType.returnType, type.returnType, node,
checkedNode: checkedNode);
}
}
void _checkArgs(
NodeList<Expression> args, List<ParameterElement> parameters) {
for (var arg in args) {
var parameterElement = arg.staticParameterElement;
if (parameterElement != null) {
var type = parameterElement.type;
var expression = arg is NamedExpression ? arg.expression : arg;
_check(type, expression.staticType, expression);
}
}
}
}