Skip to content

Commit 385598d

Browse files
committed
ruby: remove some FPs from rb/useless-assignment-to-local
1 parent b205fed commit 385598d

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

ruby/ql/src/queries/variables/DeadStoreOfLocal.ql

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,20 @@
1212

1313
import codeql.ruby.AST
1414
import codeql.ruby.dataflow.SSA
15+
import codeql.ruby.ApiGraphs
1516

1617
class RelevantLocalVariableWriteAccess extends LocalVariableWriteAccess {
1718
RelevantLocalVariableWriteAccess() {
1819
not this.getVariable().getName().charAt(0) = "_" and
19-
not this = any(Parameter p).getAVariable().getDefiningAccess()
20+
not this = any(Parameter p).getAVariable().getDefiningAccess() and
21+
not API::getTopLevelMember("ERB").getInstance().getAMethodCall("result").asExpr().getScope() =
22+
this.getCfgScope() and
23+
not exists(RetryStmt r | r.getCfgScope() = this.getCfgScope()) and
24+
not exists(MethodCall c |
25+
c.getReceiver() instanceof SelfVariableAccess and
26+
c.getMethodName() = "binding" and
27+
c.getCfgScope() = this.getCfgScope()
28+
)
2029
}
2130
}
2231

Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
11
| DeadStoreOfLocal.rb:2:5:2:5 | y | This assignment to $@ is useless, since its value is never read. | DeadStoreOfLocal.rb:2:5:2:5 | y | y |
2-
| DeadStoreOfLocal.rb:14:9:14:9 | x | This assignment to $@ is useless, since its value is never read. | DeadStoreOfLocal.rb:8:5:8:5 | x | x |
3-
| DeadStoreOfLocal.rb:21:5:21:5 | x | This assignment to $@ is useless, since its value is never read. | DeadStoreOfLocal.rb:21:5:21:5 | x | x |
4-
| TestTemplate.rb:9:1:9:1 | x | This assignment to $@ is useless, since its value is never read. | TestTemplate.rb:9:1:9:1 | x | x |

ruby/ql/test/query-tests/variables/DeadStoreOfLocal/DeadStoreOfLocal.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ def test_retry
1111
raise "error"
1212
end
1313
rescue
14-
x = 2 #$ SPURIOUS: Alert
14+
x = 2 # OK - the retry will allow a later read
1515
retry
1616
end
1717
return 42
1818
end
1919

2020
def test_binding
21-
x = 4 #$ SPURIOUS: Alert
21+
x = 4 # OK - the binding collects the value of x
2222
return binding
2323
end
2424

@@ -30,7 +30,7 @@ def m(x)
3030

3131
class Sub < Sup
3232
def m(y)
33-
y = 3 # OK - the call to `super` sees the value of y
33+
y = 3 # OK - the call to `super` sees the value of `y``
3434
super
3535
end
3636
end

ruby/ql/test/query-tests/variables/DeadStoreOfLocal/TestTemplate.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
\_\_ENCODING\_\_ is <%= \_\_ENCODING\_\_ %>.
77
x is <%= x %>.
88
EOF
9-
x = 5 #$ SPURIOUS: Alert
9+
x = 5 # OK - the template can see the value of x
1010
puts template.result

0 commit comments

Comments
 (0)