Skip to content

Bug: [no-unsafe-enum-comparison] does not work with bit masks #6909

Closed as not planned
@mrazauskas

Description

@mrazauskas

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.0.3&sourceType=module&code=KYDwDg9gTgLgBAYwgOwM72MgrgWzgFQE8xgAxAGwEMBzVOAbwCg44BBZQuAXjgEY4APALgAGADTM4AVWQBrZBADuybn0HDeElgGUYUAJbJqq-kLgAmLXAByuAEbAoJ9XADMVgEIQI5YJRU8psIALIwAvoyMoJCwcABmWMgIMPoocAAWlKhEJBQ0ABSoEFhQCMAAXATEZFS0YnAwlFDUwDCVOTU0qACUDJJQrSUqhcWlwHAAZA1NLTC9XAvTza0A3OFAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUFvcgAQBcBPABxQGNoBLEggWhXioDsCB6ZgeztmeQEMAZogbNYAWzrlO4kv2rJOzdFETRonaJHBgAviF1A&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA

Repro Code

export const enum TypeFlags {
  Any = 1 << 0,
  Unknown = 1 << 1,
  String = 1 << 2,
  Number = 1 << 3,
  Boolean = 1 << 4
}

export function hasTypeFlag(source: TypeFlags, target: TypeFlags) {
  return (source & target) === target;
}

ESLint Config

module.exports = {
  "rules": {
        "@typescript-eslint/no-unsafe-enum-comparison": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

TypeScript compiler is using bit mask pattern internally. It is rare, but useful thing. A bitwise operator is used in comparison, but otherwise enum is compared with itself. Should this be allowed or at least have a config option, perhaps? What do you think?


Also possible to rework the code. It felt more readable with ===, but could be like this too:

export function hasTypeFlag(source: TypeFlags, target: TypeFlags) {
  return Boolean(source & target);
}

Actual Result

Good idea to add the no-unsafe-enum-comparison. As you can see in reproduction, currently it does not work with bit mask pattern. Might be it was simply overlooked use case.

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginwontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions