linter icon indicating copy to clipboard operation
linter copied to clipboard

[bug] `unrelated_type_equality_checks` unexpected behavior

Open aoatmon opened this issue 4 years ago • 3 comments

Describe the issue

unrelated_type_equality_checks too restrictive

To Reproduce

  1. run the code in dartpad or below
code sample
class Foo<T> {
  final T data;
  const Foo(this.data);
  @override
  bool operator ==(Object other) =>
      other is Foo<T> && hashCode == other.hashCode;

  @override
  int get hashCode => data.hashCode;
}

abstract class BarBase {}

class Bar<T> extends Foo<T> implements BarBase {
  const Bar(T data) : super(data);
  @override
  bool operator ==(Object other) =>
      other is Bar<T> && hashCode == other.hashCode;

  @override
  int get hashCode => data.hashCode;
}

class FooCheck<T extends BarBase> {
  final Foo foo;
  const FooCheck(this.foo);

  // ignore: avoid_print
  void call(T other) => print(foo == other);
}

void main() {
  const check = FooCheck<BarBase>(Foo<String>('hello'));
  const bar = Bar<String>('hello');
  check(bar);
}
  1. modify the code as shown below
class FooCheck<T extends BarBase> {
  final Foo foo;
  const FooCheck(this.foo);

  // ignore: avoid_print
-  void call(T other) => print(foo == other);
+  void call(BarBase other) => print(foo == other);

}

Expected behavior the linter should not change behavior

Actual behavior the linter shows unrelated_type_equality_checks

Additional context

doctor
[✓] Flutter (Channel stable, 2.8.0, on macOS 12.0.1 21A559 darwin-x64, locale en-EE)
    • Flutter version 2.8.0 at /Users/francesco/fvm/versions/stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision cf44000065 (5 days ago), 2021-12-08 14:06:50 -0800
    • Engine revision 40a99c5951
    • Dart version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/francesco/Library/Android/sdk
    • Platform android-31, build-tools 30.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)

[✓] VS Code (version 1.63.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.29.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-x64     • macOS 12.0.1 21A559 darwin-x64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 96.0.4664.93

• No issues found!

aoatmon avatar Dec 14 '21 08:12 aoatmon

Foo and BarBase are unrelated classes. Why should a lint not be reported?

srawlins avatar Dec 15 '21 01:12 srawlins

@srawlins how is Foo any more related to a T that extends the same abstract class BarBase ? I think the linter should exclude abstract classes

aoatmon avatar Jan 27 '22 15:01 aoatmon

how is Foo any more related to a T that extends the same abstract class BarBase?

== with a T is treated differently because T is not a type, but a type variable. https://github.com/dart-lang/linter/issues/1572 is related.

I think the linter should exclude abstract classes

Interesting idea. What is the reasoning here?

srawlins avatar Jan 27 '22 16:01 srawlins

Closing this as stale. Please re-open if you wish to continue the discussion.

srawlins avatar Oct 26 '22 15:10 srawlins