Skip to content

Commit e5fc1b0

Browse files
committed
ruby: add qhelp to rb/useless-assignment-to-local
1 parent a885e61 commit e5fc1b0

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>
7+
A value is assigned to a local variable, but either that variable is never read
8+
later on, or its value is always overwritten before being read. This means
9+
that the original assignment has no effect, and could indicate a logic error or
10+
incomplete code.
11+
</p>
12+
13+
</overview>
14+
<recommendation>
15+
16+
<p>
17+
Ensure that you check the control and data flow in the method carefully.
18+
If a value is really not needed, consider omitting the assignment. Be careful,
19+
though: if the right-hand side has a side-effect (like performing a method call),
20+
it is important to keep this to preserve the overall behavior.
21+
</p>
22+
23+
</recommendation>
24+
<example>
25+
26+
<p>
27+
In the following example, the return value of the call to <code>send</code> on line 2
28+
is assigned to the local variable <code>result</code>, but then never used.
29+
</p>
30+
31+
<sample src="examples/DeadStoreOfLocal.rb" />
32+
33+
<p>
34+
Assuming that <code>send</code> returns a status code indicating whether the operation
35+
succeeded or not, the value of <code>result</code> should be checked, perhaps like this:
36+
</p>
37+
38+
<sample src="examples/DeadStoreOfLocalGood.rb" />
39+
40+
</example>
41+
<references>
42+
43+
44+
<li>Wikipedia: <a href="http://en.wikipedia.org/wiki/Dead_store">Dead store</a>.</li>
45+
46+
47+
48+
</references>
49+
</qhelp>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def f(x)
2+
result = send(x)
3+
waitForResponse
4+
return getResponse
5+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def f(x)
2+
result = send(x)
3+
# check for error
4+
if (result == -1)
5+
raise "Unable to send, check network."
6+
end
7+
waitForResponse
8+
return getResponse
9+
end

0 commit comments

Comments
 (0)