Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Fix" is_overwritten_in_loop check #339

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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