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

Unread "private" fields should be removed

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove this unread private field 'xxxx' or refactor the code to use its value.
    • Highlighting:
      • primary: Field name declaration
      • secondary: Statements changing its value
    • Default Severity:
      Critical
    • Impact:
      High
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      C#, C++, Go, Java, Python, Swift, TypeScript
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5 mn
    • Analysis Level:
      Semantic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Common Rule:
      Yes
    • CERT:
      MSC13-C., MSC56-J.
    • CWE:
      CWE-563

      Description

      Private fields only used to store values without reading them later is a case of dead store. So changing the value of such field is useless and most probably indicates a serious error in the code.

      Noncompliant Code Example

      public class Rectangle {
        private int height;
        private int width;  //width is written but never read
      
        public Rectangle(int height, int width) {
          this.height=height;
          this.width = width;
        }
      
        public int getArea() {
          return height * height;
        }
      }
      

      Compliant Solution

      public class Rectangle {
        private int height;
        private int width;
      
        public Rectangle(int height, int width) {
          this.height=height;
          this.width = width;
        }
      
        public int getArea() {
          return height * width;
        }
      }
      

      See

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                freddy.mallet Freddy Mallet (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: