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

"=+" should not be used instead of "+="

    XMLWordPrintable

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
      None
    • Message:
      Hide
      * for +/-: Was "x=" meant instead?
      * for !: Add a space between "=" and "!" to avoid confusion.
      Show
      * for +/-: Was "x=" meant instead? * for !: Add a space between "=" and "!" to avoid confusion.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Flex, PL/I, RPG, Rust, Scala, Solidity, T-SQL
    • Covered Languages:
      C#, C, C++, Go, Java, JavaScript, Kotlin, Objective-C, PHP, Python, Ruby, TypeScript, VB.Net
    • Irrelevant for Languages:
      ABAP, Cobol, HTML, Kotlin, PL/SQL, Swift, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Common Rule:
      Yes
    • PVS-Studio:
      V588, V589, V3034, V3035, V3036
    • TSLint-SonarTS:
      no-misspelled-operator

      Description

      The use of operators pairs ( =+, =- or =! ) where the reversed, single operator was meant (+=, -= or !=) will compile and run, but not produce the expected results.

      This rule raises an issue when =+, =-, or =! is used without any spacing between the two operators and when there is at least one whitespace character after.

      Noncompliant Code Example

      int target = -5;
      int num = 3;
      
      target =- num;  // Noncompliant; target = -3. Is that really what's meant?
      target =+ num; // Noncompliant; target = 3
      

      Compliant Solution

      int target = -5;
      int num = 3;
      
      target = -num;  // Compliant; intent to assign inverse value of num is clear
      target += num;
      

        Attachments

          Issue Links

          1.
          C-family RSPEC-3550 Language-Specification Active Unassigned
          2.
          JavaScript: Non-existent operators '=+', '=-' and '=!' should not be used RSPEC-3775 Language-Specification Active Unassigned
          3.
          PHP RSPEC-4321 Language-Specification Active Unassigned
          4.
          Go RSPEC-4559 Language-Specification Active Unassigned
          5.
          Ruby RSPEC-4788 Language-Specification Active Unassigned
          6.
          Kotlin RSPEC-4789 Language-Specification Active Unassigned
          7.
          VB.Net RSPEC-4878 Language-Specification Active Unassigned
          8.
          Apex RSPEC-5011 Language-Specification Active Unassigned
          9.
          Python RSPEC-5578 Language-Specification Active Unassigned

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ann.campbell.2 Ann Campbell
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: