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

Variables should not be self-assigned

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove or correct this useless self-assignment.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Flex, PL/I, VB6
    • Covered Languages:
      ABAP, C#, C, C++, Cobol, Go, Java, JavaScript, Kotlin, Objective-C, PHP, PL/SQL, Python, RPG, Ruby, Scala, T-SQL, TypeScript, VB.Net
    • Irrelevant for Languages:
      HTML, Swift, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      3min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Common Rule:
      Yes
    • CERT:
      MSC12-C.
    • CPPCheck:
      selfAssignment
    • ESLint:
      no-self-assign
    • FindBugs:
      SA_FIELD_DOUBLE_ASSIGNMENT, SA_LOCAL_DOUBLE_ASSIGNMENT, SA_LOCAL_SELF_ASSIGNMENT, SA_FIELD_SELF_ASSIGNMENT, SA_LOCAL_SELF_ASSIGNMENT_INSTEAD_OF_FIELD
    • PMD:
      IdempotentOperations
    • TSLint-SonarTS:
      no-self-assignment

      Description

      There is no reason to re-assign a variable to itself. Either this statement is redundant and should be removed, or the re-assignment is a mistake and some other value or variable was intended for the assignment instead.

      Noncompliant Code Example

      public void setName(String name) {
        name = name;
      }
      

      Compliant Solution

      public void setName(String name) {
        this.name = name;
      }
      

      See

      • CERT, MSC12-C. - Detect and remove code that has no effect or is never executed

        Attachments

          Issue Links

          1.
          C-Family RSPEC-2890 Language-Specification Active Unassigned
          2.
          RPG RSPEC-2914 Language-Specification Active Unassigned
          3.
          JavaScript RSPEC-3174 Language-Specification Active Unassigned
          4.
          PHP RSPEC-3175 Language-Specification Active Unassigned
          5.
          VB.NET RSPEC-3710 Language-Specification Active Unassigned
          6.
          C# RSPEC-3711 Language-Specification Active Unassigned
          7.
          Python RSPEC-3987 Language-Specification Active Unassigned
          8.
          T-SQL RSPEC-4091 Language-Specification Active Unassigned
          9.
          PL/SQL: Variables and columns should not be self-assigned RSPEC-4230 Language-Specification Active Unassigned
          10.
          Cobol RSPEC-4267 Language-Specification Active Unassigned
          11.
          Go RSPEC-4483 Language-Specification Active Unassigned
          12.
          Ruby RSPEC-4776 Language-Specification Active Unassigned
          13.
          Kotlin RSPEC-4791 Language-Specification Active Unassigned
          14.
          Scala RSPEC-4933 Language-Specification Active Unassigned
          15.
          Apex RSPEC-5010 Language-Specification Active Unassigned

            Activity

              People

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

                Dates

                • Created:
                  Updated: