Skip to content

Commit 2485abc

Browse files
authored
Merge pull request #339 from julia-vscode/sp/fix-overwritten_in_loop
"Fix" is_overwritten_in_loop check
2 parents 556bb79 + 122b2cb commit 2485abc

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/linting/checks.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,9 @@ end
950950
function check_unused_binding(b::Binding, scope::Scope)
951951
if headof(scope.expr) !== :struct && headof(scope.expr) !== :tuple && !all_underscore(valof(b.name))
952952
refs = loose_refs(b)
953-
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)
953+
if (isempty(refs) || length(refs) == 1 && refs[1] == b.name) &&
954+
!is_sig_arg(b.name) && !is_overwritten_in_loop(b.name) &&
955+
!is_overwritten_subsequently(b, scope) && !is_kw_of_macrocall(b)
954956
seterror!(b.name, UnusedBinding)
955957
end
956958
end
@@ -986,9 +988,10 @@ function is_overwritten_in_loop(x)
986988
if s2 isa Scope
987989
prev_binding = parentof(s2).names[valof(x)]
988990
if prev_binding isa Binding
989-
s = ComesBefore(prev_binding.name, s2.expr, 0)
990-
traverse(parentof(s2).expr, s)
991-
return s.result == 1
991+
return true
992+
# s = ComesBefore(prev_binding.name, s2.expr, 0)
993+
# traverse(parentof(s2).expr, s)
994+
# return s.result == 1
992995
# for r in prev_binding.refs
993996
# if r isa EXPR && is_in_fexpr(r, x -> x === loop)
994997
# return true

test/runtests.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,3 +1945,34 @@ end
19451945
""")
19461946
@test length(StaticLint.collect_hints(cst, server)) == 4
19471947
end
1948+
1949+
@testset "assigned but not used with loops" begin
1950+
cst = parse_and_pass("""
1951+
function a!(v)
1952+
next = 0
1953+
for i in eachindex(v)
1954+
current = next
1955+
next = sin(current)
1956+
while true
1957+
current = next
1958+
next = sin(current)
1959+
end
1960+
v[i] = current
1961+
end
1962+
end
1963+
""")
1964+
@test isempty(StaticLint.collect_hints(cst, server))
1965+
cst = parse_and_pass("""
1966+
function f(v)
1967+
next = 0
1968+
for _ in v
1969+
foo = next
1970+
for _ in v
1971+
next = foo
1972+
end
1973+
foo = sin(next)
1974+
end
1975+
end
1976+
""")
1977+
@test isempty(StaticLint.collect_hints(cst, server))
1978+
end

0 commit comments

Comments
 (0)