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

JavaScript rule useless-assign reports false positive with require and object destructuring #2862

Open
1 of 3 tasks
aarongoldenthal opened this issue Apr 9, 2023 · 1 comment

Comments

@aarongoldenthal
Copy link

aarongoldenthal commented Apr 9, 2023

Update: the bug is now tracked at semgrep/semgrep#7531

Describe the bug

JavaScript rule useless-assign reports a false positive for sequential statements with object destructuring and at least one with a require statement, specifically an error with assigning a value to const twice. I'm reporting it here because it was found with one specific rule, but it does appear to be a parsing error rather than a rule error.

The following cases all report the warnings indicated:

// Message: `const` is assigned twice; the first assignment is useless
// Same error occurs wth `var` or `let` instead of `const`
const { test, expect } = require("@playwright/test");
const { pathToFileURL } = require("url");
// Message: `const` is assigned twice; the first assignment is useless
const { test, expect } = require("@playwright/test");
const { pathToFileURL } = url;
// Message: `const` is assigned twice; the first assignment is useless
const { test, expect } = playwright;
const { pathToFileURL } = require("url");

The following cases report no warning:

const playwright = require("@playwright/test");
const url = require("url");
const { test, expect } = playwright;
const { pathToFileURL } = url;
const test = require("@playwright/test").test;
const expect = require("@playwright/test").expect;
const pathToFileURL = require("url").pathToFileURL;
const playwright = require("@playwright/test");
const { pathToFileURL } = require("url");

To Reproduce

See example here or above.

Expected behavior

No warnings should be reported.

Priority
How important is this to you?

  • P0: blocking me from making progress
  • P1: this will block me in the near future
  • P2: annoying but not blocking me

Desktop (please complete the following information):

  • OS: Windows 11 22H2 (and reproduced at semgrep playground)
  • Semgrep Version: 1.17.1
@aarongoldenthal aarongoldenthal added the bug Something isn't working label Apr 9, 2023
@aarongoldenthal aarongoldenthal changed the title JavaScript rules useless-assign reports false positive with require and object destructuring JavaScript rule useless-assign reports false positive with require and object destructuring Apr 9, 2023
@aarongoldenthal aarongoldenthal changed the title JavaScript rule useless-assign reports false positive with require and object destructuring JavaScript rule useless-assign reports false positive with require and object destructuring Apr 9, 2023
@mjambon
Copy link
Member

mjambon commented Apr 13, 2023

Thank you!

Here's another illustration of the bug:
image

There are two matches on line 2, the first one being wrong:

⬆Line 2
X is `const`, Y is `const {b} = 2`
⬆Line 2
X is `{b}`, Y is `2`

https://semgrep.dev/s/pOp0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants
@mjambon @LewisArdern @aarongoldenthal and others