Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bug: no-unsafe-enum-comparison - False positives with bit shifts #7067

@Zamiell

Description

@Zamiell

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.

Repro Code

enum Foo {
  Bar,
}

const myBitShift = 1 << Foo.Bar

This gives the following error:

The two values in this comparison do not have a shared enum type.

The error message says that we are doing a "comparison", but this is false. We are not doing a comparison at all, we are doing a bit shift.

Thus, I propose that the << operator and the >> operator are exempt from being examined by this rule, and I assume that this was just an oversight when the rule was originally implemented. (Well, I was actually the person to originally implement it, but I guess this behavior got lost in translation somewhere.)


Playground

https://typescript-eslint.io/play/#ts=5.0.4&sourceType=module&code=KYOwrgtgBAYg9nKBvAsAKClAQgQwE4A06AvuugMZwgDOALlBAJ5YCWtAygBYsBm9AvFACMUADyjYCAHS486IA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tieQEMAZonpNYAW1pkOY4nyrIOTdFETRoHaJHBgAviB1A&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA

ESLint Config

I am using the recommend configs, i.e.:

  extends: [
    // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/recommended.ts
    "plugin:@typescript-eslint/recommended",

    // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/recommended-requiring-type-checking.ts
    "plugin:@typescript-eslint/recommended-requiring-type-checking",

    // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/strict.ts
    "plugin:@typescript-eslint/strict",
  ],

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions