← 返回首页
Self assignment — CodeQL query help documentation CodeQL docs
CodeQL documentation
CodeQL resources

Self assignment

ID: java/redundant-assignment Kind: problem Security severity: Severity: error Precision: very-high Tags: - quality - reliability - correctness - logic Query suites: - java-code-quality.qls - 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 existing MotionEvent 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 of o to the fields of the new event, ev, the statement is clearly a mistake. To correct this, the mFlags value should be assigned to ev.mFlags instead, as shown in the corrected method.

static public MotionEvent obtainNoHistory(MotionEvent o) { MotionEvent ev = obtain(o.mNumPointers, 1); ev.mDeviceId = o.mDeviceId; o.mFlags = o.mFlags; // Variable is assigned to itself ... } static public MotionEvent obtainNoHistory(MotionEvent o) { MotionEvent ev = obtain(o.mNumPointers, 1); ev.mDeviceId = o.mDeviceId; ev.mFlags = o.mFlags; // Variable is assigned correctly ... }

References