Skip to content

Commit 7f43b34

Browse files
eernstgCommit Queue
authored andcommitted
Adjust hasObviousType to include negated numeric literals
Bug: #60243 Change-Id: I51509ef84d4b65f68b2ae84cce8ba12894666292 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/413500 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Erik Ernst <[email protected]>
1 parent f702c93 commit 7f43b34

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

pkg/linter/lib/src/util/obvious_types.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/ast/ast.dart';
6+
import 'package:analyzer/dart/ast/token.dart';
67
import 'package:analyzer/dart/element/element2.dart';
78
import 'package:analyzer/dart/element/type.dart';
89
// ignore: implementation_imports
@@ -180,6 +181,11 @@ extension ExpressionExtensions on Expression {
180181
// A non-generic class or extension type.
181182
return true;
182183
}
184+
case PrefixExpression()
185+
when (self.operand is IntegerLiteral ||
186+
self.operand is DoubleLiteral) &&
187+
self.operator.type == TokenType.MINUS:
188+
return self.operand.hasObviousType;
183189
case CascadeExpression():
184190
return self.target.hasObviousType;
185191
case AsExpression():

pkg/linter/test/rules/omit_obvious_local_variable_types_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ f() {
8181
test_forEach_noDeclaredType() async {
8282
await assertNoDiagnostics(r'''
8383
f() {
84-
for (var i in [1, 2, 3]) { }
84+
for (var i in [1, -2, 3]) { }
8585
}
8686
''');
8787
}
@@ -91,15 +91,15 @@ f() {
9191
f() {
9292
for (int i in list) { }
9393
}
94-
var list = [1, 2, 3];
94+
var list = [1, -2, 3];
9595
''');
9696
}
9797

9898
test_forEach_typedList() async {
9999
await assertDiagnostics(
100100
r'''
101101
f() {
102-
for (int i in <int>[1, 2, 3]) { }
102+
for (int i in <int>[1, -2, 3]) { }
103103
}
104104
''',
105105
[lint(13, 3)],

pkg/linter/test/rules/specify_nonobvious_local_variable_types_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ f() {
231231
''');
232232
}
233233

234+
test_literal_double_negated() async {
235+
await assertNoDiagnostics(r'''
236+
f() {
237+
var d = -1.5;
238+
}
239+
''');
240+
}
241+
234242
// The type is not obvious.
235243
test_literal_doubleTypedInt() async {
236244
await assertNoDiagnostics(r'''
@@ -248,6 +256,14 @@ f() {
248256
''');
249257
}
250258

259+
test_literal_int_negated() async {
260+
await assertNoDiagnostics(r'''
261+
f() {
262+
var i = -1;
263+
}
264+
''');
265+
}
266+
251267
// `Null` is not obvious, the inferred type is `dynamic`.
252268
test_literal_null() async {
253269
await assertNoDiagnostics(r'''

0 commit comments

Comments
 (0)