Skip to content

Commit

Permalink
Add special cases for fixnum to unrelated_type_equality_checks. (#996)
Browse files Browse the repository at this point in the history
* Add special cases for fixnum to unrelated_type_equality_checks.

* Address review comments.

* Address review comments.
  • Loading branch information
davidmorgan authored and pq committed May 18, 2018
1 parent a9f55b5 commit 8f946f3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.1.51-dev

* unrelated_type_equality_checks now allows comparison between `Int64` or `Int32` and `int`

# 0.1.50

* migration of rules to use analyzer package `NodeLintRule` and `UnitLintRule` yielding significant performance gains all around
Expand Down
29 changes: 23 additions & 6 deletions lib/src/rules/unrelated_type_equality_checks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/standard_resolution_map.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:linter/src/analyzer.dart';
import 'package:linter/src/util/dart_type_utilities.dart';

Expand All @@ -19,6 +20,9 @@ const _details = r'''
Comparing references of a type where neither is a subtype of the other most
likely will return `false` and might not reflect programmer's intent.
`Int64` and `Int32` from `package:fixnum` allow comparing to `int` provided
the `int` is on the right hand side. The lint allows this as a special case.
**BAD:**
```
void someFunction() {
Expand Down Expand Up @@ -126,11 +130,25 @@ class DerivedClass2 extends ClassBase with Mixin {}
''';

bool _hasNonComparableOperands(BinaryExpression node) =>
!DartTypeUtilities.isNullLiteral(node.leftOperand) &&
!DartTypeUtilities.isNullLiteral(node.rightOperand) &&
DartTypeUtilities.unrelatedTypes(
node.leftOperand.bestType, node.rightOperand.bestType);
const String _dartCoreLibraryName = 'dart.core';

bool _isCoreInt(DartType type) =>
type.name == 'int' && type.element?.library?.name == _dartCoreLibraryName;

bool _isFixNumIntX(DartType type) =>
(type.name == 'Int32' || type.name == 'Int64') &&
type.element?.library?.name == 'fixnum';

bool _hasNonComparableOperands(BinaryExpression node) {
var left = node.leftOperand;
var leftType = left.bestType;
var right = node.rightOperand;
var rightType = right.bestType;
return !DartTypeUtilities.isNullLiteral(left) &&
!DartTypeUtilities.isNullLiteral(right) &&
DartTypeUtilities.unrelatedTypes(leftType, rightType) &&
!(_isFixNumIntX(leftType) && _isCoreInt(rightType));
}

class UnrelatedTypeEqualityChecks extends LintRule implements NodeLintRule {
UnrelatedTypeEqualityChecks()
Expand All @@ -148,7 +166,6 @@ class UnrelatedTypeEqualityChecks extends LintRule implements NodeLintRule {
}

class _Visitor extends SimpleAstVisitor<void> {
static const String _dartCoreLibraryName = 'dart.core';
static const String _boolClassName = 'bool';

final LintRule rule;
Expand Down
26 changes: 26 additions & 0 deletions test/rules/unrelated_type_equality_checks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

// test w/ `pub run test -N unrelated_type_equality_checks`

import '../util/fake_fixnum.dart';

void someFunction() {
var x = '1';
if (x == 1) print('someFunction'); // LINT
Expand Down Expand Up @@ -96,6 +98,30 @@ void someFunction14(DerivedClass4 instance) {
if (other == instance) print('someFunction15'); // LINT
}

void someFunction15() {
var x = new Int32();

if (x == 0) print('someFunction15'); // OK
}

void someFunction16() {
var x = new Int32();

if (0 == x) print('someFunction16'); // LINT
}

void someFunction17() {
var x = new Int64();

if (x == 0) print('someFunction17'); // OK
}

void someFunction18() {
var x = new Int64();

if (0 == x) print('someFunction18'); // LINT
}

class ClassBase {}

class DerivedClass1 extends ClassBase {}
Expand Down
5 changes: 5 additions & 0 deletions test/util/fake_fixnum.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library fixnum;

class Int32 {}

class Int64 {}

0 comments on commit 8f946f3

Please sign in to comment.