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

Fix FP S6594 (sonar-prefer-regexp-exec): Ignore match count use case #4532

Closed
yassin-kammoun-sonarsource opened this issue Jan 26, 2024 · 0 comments · Fixed by #4540
Closed
Assignees
Labels
mmf-2934 https://sonarsource.atlassian.net/browse/MMF-2934
Milestone

Comments

@yassin-kammoun-sonarsource
Copy link
Contributor

yassin-kammoun-sonarsource commented Jan 26, 2024

The rule should not suggest using Regex.exec if the returned value of String.match is eventually used to get the number of matches. It only involves getting the .length of the value, while a loop is needed in the case of Regex.exec.

const regex = new RegExp(pattern);
const matches = 'foo'.match(regex); // Currently false positive, should be compliant

/* ... */

matches.length;

/* or */

matches?.length;
@yassin-kammoun-sonarsource yassin-kammoun-sonarsource added the mmf-2934 https://sonarsource.atlassian.net/browse/MMF-2934 label Jan 26, 2024
@yassin-kammoun-sonarsource yassin-kammoun-sonarsource changed the title Fix FP S6594 (sonar-prefer-regexp-exec): Ignore match count use case and global RegExp instances Fix FP S6594 (sonar-prefer-regexp-exec): Ignore match count use case Jan 31, 2024
@yassin-kammoun-sonarsource yassin-kammoun-sonarsource added this to the 10.12 milestone Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mmf-2934 https://sonarsource.atlassian.net/browse/MMF-2934
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants