Skip to content

Commit fd75a3d

Browse files
committed
ruby: remove some FPs from rb/useless-assignment-to-local
1 parent e9021f0 commit fd75a3d

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,27 @@
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 exists(SuperCall s |
22+
s.getEnclosingCallable().getAParameter().getAVariable().getAnAccess() = this
23+
|
24+
// a call to 'super' without any arguments will pass on the parameter.
25+
// thus, the parameter is used, and the assignment is not useless.
26+
not exists(s.getAnArgument())
27+
) and
28+
not API::getTopLevelMember("ERB").getInstance().getAMethodCall("result").asExpr().getScope() =
29+
this.getCfgScope() and
30+
not exists(RetryStmt r | r.getCfgScope() = this.getCfgScope()) and
31+
not exists(MethodCall c |
32+
c.getReceiver() instanceof SelfVariableAccess and
33+
c.getMethodName() = "binding" and
34+
c.getCfgScope() = this.getCfgScope()
35+
)
2036
}
2137
}
2238

Lines changed: 0 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

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

Lines changed: 1 addition & 1 deletion
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)