Add exception / false-positive for javascript:S1656 when using location.href=location.href

Hi, rule javascript:S1656 (“Variables should not be self‑assigned”) reports a violation for:

location.href = location.href;

This is reported as:

“Variables should not be self-assigned.”

This is a false positive (or misleading positive).

Assigning the same value to location.href is a valid and intentional idiom: browsers treat it as a page reload without adding a history entry. It is not redundant and it has a real side‑effect (rightly or wrongly, it’s what it is)

Please consider adding an exception for location.href = location.href.

Thanks

  • What language is this for?

javascript

  • Which rule?

javascript:S1656 Variables should be self-assigned

  • Why do you believe it’s a false-positive/false-negative?

assigning to location.href is not a “variable” and has the side effect of reloading the page.

  • Are you using
    • SonarQube Cloud?
    • SonarQube Server

2025.04

  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
location.href = location.href;

Hello @marc.free,

We import this rule from eslint no-self-assign. It has an option configuration that would allow all assignments for properties, which would cover this case. However, it only accepts a boolean true/false and can’t be customized. There was an issue on eslint repo to enhance this rule, but the maintainers consider this an edge-case and advise blocking the issue with a comment.

Therefore, I would advise you to disable the rule on the next line, or if it is a common pattern in your codebase, disable the rule in your Quality Profile.

Kind regards,

Michal

Hi,
Thanks for the feedback and referencing the eslint issue.