Self assignment¶
ID: java/redundant-assignmentKind: problemSecurity severity: Severity: errorPrecision: very-highTags: - quality - reliability - correctness - logicQuery suites: - java-security-and-quality.qls
Click to see the query in the CodeQL repository
Assigning a variable to itself does not have any effect. Therefore, such an assignment is either completely unnecessary, or it indicates a typo or a similar mistake.
Recommendation¶
If the assignment is unnecessary, remove it. If the assignment indicates a typo or a similar mistake, correct the mistake.
Example¶
The following example shows part of a method that is intended to make a copy of an existingMotionEvent without preserving its history. On line 8,o.mFlags is assigned to itself. Given that the statement is surrounded by statements that transfer information from the fields ofo to the fields of the new event,ev, the statement is clearly a mistake. To correct this, themFlags value should be assigned toev.mFlags instead, as shown in the corrected method.
staticpublicMotionEventobtainNoHistory(MotionEvento){MotionEventev=obtain(o.mNumPointers,1);ev.mDeviceId=o.mDeviceId;o.mFlags=o.mFlags;// Variable is assigned to itself...}staticpublicMotionEventobtainNoHistory(MotionEvento){MotionEventev=obtain(o.mNumPointers,1);ev.mDeviceId=o.mDeviceId;ev.mFlags=o.mFlags;// Variable is assigned correctly...}
References¶
Help - Eclipse Platform:Java Compiler Errors/Warnings Preferences.