Details
-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
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++, Go, Java, Swift, TypeScript
-
Covered Languages:C#, Python
-
Remediation Function:Constant/Issue
-
Constant Cost:5min
-
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
- MITRE, CWE-563 - Assignment to Variable without Use ('Unused Variable')
- CERT, MSC13-C. - Detect and remove unused values
- CERT, MSC56-J. - Detect and remove superfluous code and values
Attachments
Issue Links
- is implemented by
-
SONARPY-811 S4487: Add a message on the secondary location
-
- Closed
-
-
SONARSLANG-66 Rule S4487: Unread "private" fields should be removed
-
- Open
-
-
SONARPY-482 Rule S4487: Unread "private" attributes should be removed
-
- Closed
-
-
SONARPY-636 FP on rule S4487 when an attribute is referenced indirectly
-
- Open
-
- relates to
-
RSPEC-1144 Unused "private" methods should be removed
- Active
-
RSPEC-1854 Unused assignments should be removed
- Active
1.
|
C# | RSPEC-5477 |
|
Active | Unassigned | |
2.
|
Python: Unread "private" attributes should be removed | RSPEC-5600 |
|
Active | Unassigned |