Skip to content

[bug] unrelated_type_equality_checks unexpected behavior #58593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aoatmon opened this issue Dec 14, 2021 · 4 comments
Closed

[bug] unrelated_type_equality_checks unexpected behavior #58593

aoatmon opened this issue Dec 14, 2021 · 4 comments
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. linter-set-core

Comments

@aoatmon
Copy link

aoatmon commented Dec 14, 2021

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!
@srawlins
Copy link
Member

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

@aoatmon
Copy link
Author

aoatmon commented Jan 27, 2022

@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

@srawlins
Copy link
Member

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. #57962 is related.

I think the linter should exclude abstract classes

Interesting idea. What is the reasoning here?

@srawlins
Copy link
Member

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

@devoncarew devoncarew added devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. labels Nov 19, 2024
@devoncarew devoncarew transferred this issue from dart-archive/linter Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. linter-set-core
Projects
None yet
Development

No branches or pull requests

4 participants