[bug] `unrelated_type_equality_checks` unexpected behavior
Describe the issue
unrelated_type_equality_checks too restrictive
To Reproduce
- 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);
}
- 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!
Foo and BarBase are unrelated classes. Why should a lint not be reported?
@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
how is
Fooany more related to aTthat extends the same abstract classBarBase?
== 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?
Closing this as stale. Please re-open if you wish to continue the discussion.