Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3760

Arithmetic operators should only have numbers as operands

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Convert this operand into a number.
    • Highlighting:
      Hide

      Primary: non numerical operand

      Show
      Primary: non numerical operand
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way recommended
    • Covered Languages:
      JavaScript
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      Expressions with arithmetic (/, *, %, ++, --, -, -=, *=, /=, %=, +=, +), unary (-), or comparison operators (>, <, >=, <=) where one, or both, of the operands is a String, Boolean or Date value rely on implicit conversions. Both the maintainability and reliability levels of such a piece of code are questionable.

      Noncompliant Code Example

      str = "80";
      quarter = str / 4; // Noncompliant
      
      if (str < 10) { // Noncompliant
         // ...
      }
      

      Compliant Solution

      str = "80";
      parsedStr = parseInt(str);
      quarter = parsedStr / 4;
      
      if (parsedStr < 10) {
        // ...
      }
      

      Exceptions

      • Expressions using the binary + operator with at least one String operand are ignored because the + operator will perform a concatenation in that case.
      • Comparisons where both operands are strings are ignored because a lexicographical comparison is performed in that case.

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            jeanchristophe.collet Jean-Christophe Collet (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated: