Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Useless assignment to field

ID: go/useless-assignment-to-fieldKind: problemSecurity severity: Severity: warningPrecision: very-highTags:   - quality   - maintainability   - useless-code   - external/cwe/cwe-563Query suites:   - go-code-quality.qls   - go-security-and-quality.qls

Click to see the query in the CodeQL repository

A value is assigned to a field, but its value is never read. This means that the assignment has no effect, and could indicate a logic error or incomplete code.

Recommendation

Examine the assignment closely to determine whether it is redundant, or whether it is perhaps a symptom of another bug.

Example

The following example shows a simplestruct type wrapping an integer counter with a methodreset that sets the counter to zero.

packagemaintypecounterstruct{valint}func(ccounter)reset(){c.val=0}

However, the receiver variable ofreset is declared to be of typecounter, not*counter, so the receiver value is passed into the method by value, not by reference. Consequently, the method does not actually mutate its receiver as intended.

To fix this, change the type of the receiver variable to*counter:

packagemainfunc(c*counter)resetGood(){c.val=0}

References


[8]ページ先頭

©2009-2026 Movatter.jp