Expression has no effect¶
ID: go/useless-expressionKind: problemSecurity severity: Severity: warningPrecision: very-highTags: - quality - reliability - correctness - external/cwe/cwe-480 - external/cwe/cwe-561Query suites: - go-security-and-quality.qls
Click to see the query in the CodeQL repository
An expression that has no effects (such as changing variable values or producing output) and occurs in a context where its value is ignored possibly indicates missing code or a latent bug.
Recommendation¶
Carefully inspect the expression to ensure it is not a symptom of a bug.
Example¶
The following example shows a named typeTimestamp that is an alias forint, representing time stamps expressed as the number of seconds elapsed since some epoch. TheaddDays method returns a time stamp that is a given number of days after another time stamp, without modifying that time stamp.
However, whenaddDays is used in functiontest, its result is discarded, perhaps because the programmer mistakenly assumed thataddDays updates the time stamp in place.
packagemainimport"fmt"typeTimestampintfunc(tTimestamp)addDays(dint)Timestamp{returnTimestamp(int(t)+d*24*3600)}functest(tTimestamp){fmt.Printf("Before: %s\n",t)t.addDays(7)fmt.Printf("After: %s\n",t)}
Instead, the result ofaddDays should be assigned back intot:
packagemainimport"fmt"functestGood(tTimestamp){fmt.Printf("Before: %s\n",t)t=t.addDays(7)fmt.Printf("After: %s\n",t)}