Skip to content
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

[require-atomic-updates]: False positive on reused loop variable name #14208

Closed
fluggo opened this issue Mar 12, 2021 · 2 comments · Fixed by #14282
Closed

[require-atomic-updates]: False positive on reused loop variable name #14208

fluggo opened this issue Mar 12, 2021 · 2 comments · Fixed by #14282
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly repro:yes rule Relates to ESLint's core rules
Projects

Comments

@fluggo
Copy link

fluggo commented Mar 12, 2021

Tell us about your environment

Environment Info:

Node version: v14.15.4
npm version: v7.5.3
Local ESLint version: v7.21.0 (Currently used)
Global ESLint version: Not found

Parser: @typescript-eslint/parser

Please show your full configuration:

Configuration
module.exports = {
  env: {
    es2020: true,
    node: true
  },
  root: true,
  parser: '@typescript-eslint/parser',
  parserOptions: {
    'project': './tsconfig.eslint.json',
    'sourceType': 'module',
    'tsconfigRootDir': __dirname,
  },
  plugins: [
    'eslint-plugin-import',
    '@typescript-eslint',
  ],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
  ],
  rules: {
    '@typescript-eslint/no-non-null-assertion': 'warn',
    '@typescript-eslint/no-shadow': ['error', { 'hoist': 'all' }],
    '@typescript-eslint/no-unused-expressions': 'error',
    '@typescript-eslint/require-await': 'warn',
    '@typescript-eslint/require-array-sort-compare': 'error',
    '@typescript-eslint/no-base-to-string': 'error',
    '@typescript-eslint/restrict-plus-operands': 'error',
    'array-callback-return': 'error',
    'eqeqeq': ['error', 'always'],
    'guard-for-in': 'error',
    'no-bitwise': 'error',
    'no-caller': 'error',
    'no-eval': 'error',
    'no-new-wrappers': 'error',
    'no-template-curly-in-string': 'error',
    'no-throw-literal': 'error',
    'no-undef-init': 'error',
    'no-unreachable-loop': 'error',
    'no-useless-backreference': 'error',
    'no-var': 'error',
    'require-atomic-updates': 'error',
    'prefer-const': 'off',      // change to 'error'
    '@typescript-eslint/consistent-type-definitions': ['warn', 'interface'],
    '@typescript-eslint/semi': ['error', 'always'],
    '@typescript-eslint/unified-signatures': 'error',
    '@typescript-eslint/prefer-string-starts-ends-with': 'warn',
    '@typescript-eslint/no-extra-semi': 'off',        // change to 'warn'
    '@typescript-eslint/no-inferrable-types': 'off',    // change to ['warn', {ignoreParameters: true}]
    'arrow-body-style': 'off',          // change to ['error', 'as-needed'],
    '@typescript-eslint/no-explicit-any': 'off',
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    '@typescript-eslint/no-unused-vars': 'off',
    '@typescript-eslint/no-empty-function': 'off',
    '@typescript-eslint/no-unsafe-assignment': 'off',
    '@typescript-eslint/no-unsafe-member-access': 'off',
    '@typescript-eslint/no-unsafe-call': 'off',
    '@typescript-eslint/no-unsafe-return': 'off',
    '@typescript-eslint/no-implicit-any-catch': 'off',
    '@typescript-eslint/restrict-template-expressions': 'off',
    '@typescript-eslint/no-unnecessary-condition': 'off',
    'no-inner-declarations': 'off',
    'no-promise-executor-return': 'off',
  }
};

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

async function foo(e) {
}

async function run() {
  const input = [];
  const props = [];

  for(const entry of input) {
    const prop = props.find(a => a.id === entry.id) || null;
    await foo(entry);
  }

  // Possible race condition: `const entry` might be reassigned based on an outdated value of `const entry`
  for(const entry of input) {
    const prop = props.find(a => a.id === entry.id) || null;
  }

  for(const entry2 of input) {
    const prop = props.find(a => a.id === entry2.id) || null;
  }
}

Command: npx eslint scripts/sample-require-atomic-updates.ts

What did you expect to happen?

Did not expect any errors because the entry variables in the two loops are independent.

What actually happened? Please include the actual, raw output from ESLint.

ESLint seems to think that the variable captured in the second loop's arrow function closure is the entry variable from the first loop, and flags an error.

/......./scripts/sample-require-atomic-updates.ts
  14:3  error  Possible race condition: `const entry` might be reassigned based on an outdated value of `const entry`  require-atomic-updates

✖ 1 problem (1 error, 0 warnings)
@eslint-github-bot eslint-github-bot bot added the triage An ESLint team member will look at this issue soon label Mar 12, 2021
@mdjermanovic mdjermanovic added this to Needs Triage in Triage via automation Mar 15, 2021
@mdjermanovic mdjermanovic moved this from Needs Triage to Triaging in Triage Mar 15, 2021
@mdjermanovic
Copy link
Member

Hi @fluggo, thanks for the issue!

I can reproduce this is our online demo, looks like a bug..

@mdjermanovic mdjermanovic moved this from Triaging to Ready to Implement in Triage Mar 15, 2021
@mdjermanovic mdjermanovic added accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly rule Relates to ESLint's core rules repro:yes and removed triage An ESLint team member will look at this issue soon labels Mar 15, 2021
@patriscus
Copy link
Contributor

patriscus commented Mar 31, 2021

I am currently working on a potential fix. I hope to create a PR within the next two days

patriscus added a commit to patriscus/eslint that referenced this issue Mar 31, 2021
patriscus added a commit to patriscus/eslint that referenced this issue Mar 31, 2021
@mdjermanovic mdjermanovic moved this from Ready to Implement to Pull Request Opened in Triage Mar 31, 2021
patriscus added a commit to patriscus/eslint that referenced this issue Apr 3, 2021
patriscus added a commit to patriscus/eslint that referenced this issue Apr 10, 2021
patriscus added a commit to patriscus/eslint that referenced this issue May 1, 2021
Triage automation moved this from Pull Request Opened to Complete May 8, 2021
btmills pushed a commit that referenced this issue May 8, 2021
…) (#14282)

* Fix: require-atomic-updates false positives in loops (fixes #14208)

* Rework & store variables in segmentInfo instead of variable names

* Skip unresolved references in createReferenceMap
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Nov 5, 2021
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Nov 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly repro:yes rule Relates to ESLint's core rules
Projects
Archived in project
Triage
Complete
Development

Successfully merging a pull request may close this issue.

3 participants