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

Values should not be uselessly incremented

    XMLWordPrintable

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove this [inc|dec]rement or correct the code not to waste it.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Flex, Swift
    • Covered Languages:
      C#, C, C++, Java, JavaScript, Objective-C, PHP, TypeScript
    • Irrelevant for Languages:
      ABAP, Cobol, Go, HTML, PL/I, PL/SQL, Python, RPG, T-SQL, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Level:
      Control-flow Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Implementation details:
    • Common Rule:
      Yes
    • FindBugs:
      DLS_OVERWRITTEN_INCREMENT, DLS_DEAD_LOCAL_INCREMENT_IN_RETURN
    • TSLint-SonarTS:
      no-useless-increment

      Description

      A value that is incremented or decremented and then not stored is at best wasted code and at worst a bug.

      Noncompliant Code Example

      public int pickNumber() {
        int i = 0;
        int j = 0;
      
        i = i++; // Noncompliant; i is still zero
      
        return j++; // Noncompliant; 0 returned
      }
      

      Compliant Solution

      public int pickNumber() {
        int i = 0;
        int j = 0;
      
        i++; 
        return ++j; 
      }
      

        Attachments

          Issue Links

          1.
          JavaScript RSPEC-2707 Language-Specification Active Unassigned
          2.
          C# RSPEC-3789 Language-Specification Active Unassigned
          3.
          C-Family RSPEC-4256 Language-Specification Active Unassigned
          4.
          PHP RSPEC-4317 Language-Specification Active Unassigned

            Activity

              People

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

                Dates

                Created:
                Updated: