Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Fix unrelated_type_equality_checks when comparing enums #4282

Merged

Conversation

davidmartos96
Copy link
Contributor

@davidmartos96 davidmartos96 commented Apr 18, 2023

Description

Currently the unrelated_type_equality_checks doesn't work when comparing different enums. This can be a gotcha when comparing similar enums in terms of context.

enum EnumOpKind1 { insert, update, delete }
enum EnumOpKind2 { upsert, delete }

void fun() {
  var x = EnumOpKind1.delete;
  if (x == EnumOpKind1.delete) print('delete'); // OK
  if (x == EnumOpKind2.delete) print('delete'); // This should lint
}

Same thing happens when comparing different Mixins. Although this I've noticed when I looked into the source code of the lint.

@coveralls
Copy link

coveralls commented Apr 18, 2023

Coverage Status

Coverage: 96.43% (+0.0004%) from 96.429% when pulling c73c127 on davidmartos96:feat/fix_enum_unrelated_equality into ca2a9fe on dart-lang:main.

@davidmartos96 davidmartos96 changed the title Fix unrelated_type_equality_checks when comparing enums Fix unrelated_type_equality_checks when comparing enums and/or mixins Apr 18, 2023
@srawlins
Copy link
Contributor

I'd actually prefer we address mixins separately. Mixins can have superconstraints (mixin M on C), and we might want to consider that. I think it is less cut-and-dry than enums. I think two mixins M1 and M2 could be considered related if they each have the same superconstraint... but I could be wrong. I think it is exceedingly rare for an element's static type to be a mixin...

@davidmartos96
Copy link
Contributor Author

@srawlins Should I leave the "basic" Mixins handling or do you prefer them to be handled in a separate commit?

@srawlins
Copy link
Contributor

I think even the || leftElement is MixinElement code would be better done in a separate PR.

@davidmartos96 davidmartos96 requested a review from srawlins April 18, 2023 22:07
Copy link
Contributor

@srawlins srawlins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ Love it. I need to check internal code for new violations, then we can land.

@davidmartos96
Copy link
Contributor Author

Great, thank you!

@davidmartos96 davidmartos96 changed the title Fix unrelated_type_equality_checks when comparing enums and/or mixins Fix unrelated_type_equality_checks when comparing enums Apr 19, 2023
@srawlins
Copy link
Contributor

Internal looks clean. Will check flutter at the linter roll to the Dart SDK.

@srawlins srawlins merged commit 82e7147 into dart-archive:main Apr 19, 2023
copybara-service bot pushed a commit to dart-lang/sdk that referenced this pull request Aug 23, 2023
…/linter#4282)

* Fix unrelated_type_equality_checks when comparing enums

* format

* Fix Mixin

* add test case

* remove true &&

* add more test cases

* format

* remove mixin logic
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants