Skip to content

Commit

Permalink
Merge pull request #339 from julia-vscode/sp/fix-overwritten_in_loop
Browse files Browse the repository at this point in the history
"Fix" is_overwritten_in_loop check
  • Loading branch information
pfitzseb authored Jun 16, 2022
2 parents 556bb79 + 122b2cb commit 2485abc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/linting/checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,9 @@ end
function check_unused_binding(b::Binding, scope::Scope)
if headof(scope.expr) !== :struct && headof(scope.expr) !== :tuple && !all_underscore(valof(b.name))
refs = loose_refs(b)
if (isempty(refs) || length(refs) == 1 && refs[1] == b.name) && !is_sig_arg(b.name) && !is_overwritten_in_loop(b.name) && !is_overwritten_subsequently(b, scope) && !is_kw_of_macrocall(b)
if (isempty(refs) || length(refs) == 1 && refs[1] == b.name) &&
!is_sig_arg(b.name) && !is_overwritten_in_loop(b.name) &&
!is_overwritten_subsequently(b, scope) && !is_kw_of_macrocall(b)
seterror!(b.name, UnusedBinding)
end
end
Expand Down Expand Up @@ -986,9 +988,10 @@ function is_overwritten_in_loop(x)
if s2 isa Scope
prev_binding = parentof(s2).names[valof(x)]
if prev_binding isa Binding
s = ComesBefore(prev_binding.name, s2.expr, 0)
traverse(parentof(s2).expr, s)
return s.result == 1
return true
# s = ComesBefore(prev_binding.name, s2.expr, 0)
# traverse(parentof(s2).expr, s)
# return s.result == 1
# for r in prev_binding.refs
# if r isa EXPR && is_in_fexpr(r, x -> x === loop)
# return true
Expand Down
31 changes: 31 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1945,3 +1945,34 @@ end
""")
@test length(StaticLint.collect_hints(cst, server)) == 4
end

@testset "assigned but not used with loops" begin
cst = parse_and_pass("""
function a!(v)
next = 0
for i in eachindex(v)
current = next
next = sin(current)
while true
current = next
next = sin(current)
end
v[i] = current
end
end
""")
@test isempty(StaticLint.collect_hints(cst, server))
cst = parse_and_pass("""
function f(v)
next = 0
for _ in v
foo = next
for _ in v
next = foo
end
foo = sin(next)
end
end
""")
@test isempty(StaticLint.collect_hints(cst, server))
end

0 comments on commit 2485abc

Please sign in to comment.