All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.S905.html Maven / Gradle / Ivy

There is a newer version: 5.0.0.6962
Show newest version

Statements with no side effects and no change of control flow do not contribute to the functionality of the code and can indicate a programming error.

Why is this an issue?

When writing code, it is important to ensure that each statement serves a purpose and contributes to the overall functionality of the program. When they have no side effects or do not change the control flow, they can either indicate a programming error or be redundant:

  1. The code does not behave as intended: The statements are expected to have an effect but they do not. This can be caused by mistyping, copy-and-paste errors, etc.
  2. The statements are residual after a refactoring.

Exceptions

The rule does not raise an issue on statements containing only a semicolon (;).

How to fix it

Identify statements that do not contribute to the functionality of the code and verify if they are intended to be part of the logic. If they are, there is a bug to be fixed. If they are not, then they are redundant and should be removed.

Code examples

Noncompliant code example

function getResult() {
    let result = 42;
    if (shouldBeZero()) {
        result == 0; // Noncompliant: no side effect, was an assignment intended?
    }
    return result;
}
var msg = "Hello, "
  "World!"; // Noncompliant; have we forgotten '+' operator on previous line?

Compliant solution

function getResult() {
    let result = 42;
    if (shouldBeZero()) {
        result = 0; // Compliant
    }
    return result;
}
var msg = "Hello, " +
  "World!"; // Compliant

Resources

Standards





© 2015 - 2025 Weber Informatics LLC | Privacy Policy