SonarQube JavaScript
  1. SonarQube JavaScript
  2. SONARJS-146

Rule: Avoid doing assignments in the condition part of if/while/for statements

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.0
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      Issue Links

        Activity

        Hide
        Evgeny Mandrikov added a comment -

        Freddy, could you please suggest description for this rule?

        Show
        Evgeny Mandrikov added a comment - Freddy, could you please suggest description for this rule?
        Hide
        Freddy Mallet added a comment - - edited
        <p>Doing an assignment in the condition part of a 'if', 'while' or 'for' statement is highly not recommended for the two following reasons :/p>
        
        <ul>
        <li>The assignment is perhaps a bug as the '=' operator might have been used in place of the '==' operator</li>
        <li>If this is not a bug, this is highly error prone as another developer might read '==' instead of '=' while trying to maintain the source code</li>
        </ul>
        
        <pre>
        if(dayNumber = getClickedDayNumber(dayInfo))   //BUG
        {
            alert("day number found : " + dayNumber);
        }
        </pre>
        
        Show
        Freddy Mallet added a comment - - edited <p>Doing an assignment in the condition part of a 'if', 'while' or 'for' statement is highly not recommended for the two following reasons :/p> <ul> <li>The assignment is perhaps a bug as the '=' operator might have been used in place of the '==' operator</li> <li>If this is not a bug, this is highly error prone as another developer might read '==' instead of '=' while trying to maintain the source code</li> </ul> <pre> if(dayNumber = getClickedDayNumber(dayInfo)) //BUG { alert("day number found : " + dayNumber); } </pre>
        Hide
        Evgeny Mandrikov added a comment -

        Done.

        Show
        Evgeny Mandrikov added a comment - Done.
        Hide
        Freddy Mallet added a comment -

        Works well but the violation message should be "Remove this assignment from the expression"

        Show
        Freddy Mallet added a comment - Works well but the violation message should be "Remove this assignment from the expression"
        Hide
        Evgeny Mandrikov added a comment -

        Done.

        Show
        Evgeny Mandrikov added a comment - Done.
        Hide
        Freddy Mallet added a comment -

        Manually tested

        Show
        Freddy Mallet added a comment - Manually tested

          People

          • Assignee:
            Evgeny Mandrikov
            Reporter:
            Evgeny Mandrikov
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: