Useless assignment to local variable¶
ID: rb/useless-assignment-to-localKind: problemSecurity severity: Severity: warningPrecision: highTags: - quality - maintainability - useless-code - external/cwe/cwe-563Query suites: - ruby-security-and-quality.qls
Click to see the query in the CodeQL repository
A value is assigned to a local variable, but either that variable is never read later on, or its value is always overwritten before being read. This means that the original assignment has no effect, and could indicate a logic error or incomplete code.
Recommendation¶
Ensure that you check the control and data flow in the method carefully. If a value is really not needed, consider omitting the assignment. Be careful, though: if the right-hand side has a side-effect (like performing a method call), it is important to keep this to preserve the overall behavior.
Example¶
In the following example, the return value of the call tosend on line 2 is assigned to the local variableresult, but then never used.
deff(x)result=send(x)waitForResponsereturngetResponseend
Assuming thatsend returns a status code indicating whether the operation succeeded or not, the value ofresult should be checked, perhaps like this:
deff(x)result=send(x)# check for errorif(result==-1)raise"Unable to send, check network."endwaitForResponsereturngetResponseend
References¶
Wikipedia:Dead store.
Common Weakness Enumeration:CWE-563.