Skip to content

Commit f054572

Browse files
committed
fix(lint): fix unused farg detection nospecialize and kwargs
1 parent 6ab1bf4 commit f054572

File tree

2 files changed

+57
-50
lines changed

2 files changed

+57
-50
lines changed

src/linting/checks.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -552,12 +552,15 @@ function check_farg_unused(x::EXPR)
552552
end
553553

554554
function check_farg_unused_(arg, arg_names)
555-
if hasbinding(arg)
556-
elseif iskwarg(arg) && hasbinding(arg.args[1])
557-
arg = arg.args[1]
558-
elseif is_nospecialize_call(arg) && hasbinding(unwrap_nospecialize(arg))
559-
arg = unwrap_nospecialize(arg)
560-
else
555+
if !hasbinding(arg)
556+
if iskwarg(arg)
557+
arg = arg.args[1]
558+
end
559+
if is_nospecialize_call(arg)
560+
arg = unwrap_nospecialize(arg)
561+
end
562+
end
563+
if !hasbinding(arg)
561564
return false
562565
end
563566
b = bindingof(arg)

test/runtests.jl

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ f(arg) = arg
187187
@test StaticLint.CoreTypes.ismodule(bindingof(parse_and_pass("module A end").args[1]).type)
188188
@test StaticLint.CoreTypes.ismodule(bindingof(parse_and_pass("baremodule A end").args[1]).type)
189189

190-
# @test parse_and_pass("function f(x::Int) x end")[1][2][3].binding.t == StaticLint.getsymbolserver(server)["Core"].vals["Function"]
190+
# @test parse_and_pass("function f(x::Int) x end")[1][2][3].binding.t == StaticLint.getsymbolserver(server)["Core"].vals["Function"]
191191
let cst = parse_and_pass("""
192192
struct T end
193193
function f(x::T) x end""")
@@ -246,9 +246,9 @@ f(arg) = arg
246246
end
247247

248248
let cst = parse_and_pass("""
249-
macro mac_str() end
250-
mac"whatever"
251-
""")
249+
macro mac_str() end
250+
mac"whatever"
251+
""")
252252
@test refof(cst.args[2].args[1]) == bindingof(cst.args[1])
253253
end
254254

@@ -261,50 +261,50 @@ f(arg) = arg
261261
end
262262

263263
let cst = parse_and_pass("""
264-
module Reparse
265-
end
266-
using .Reparse, CSTParser
267-
""")
264+
module Reparse
265+
end
266+
using .Reparse, CSTParser
267+
""")
268268
@test refof(cst.args[2].args[1].args[2]).val == bindingof(cst[1])
269269
end
270270

271271
let cst = parse_and_pass("""
272-
module A
273-
A
274-
end
275-
""")
272+
module A
273+
A
274+
end
275+
""")
276276
@test scopeof(cst).names["A"] == scopeof(cst.args[1]).names["A"]
277277
@test refof(cst.args[1].args[2]) == bindingof(cst.args[1])
278278
@test refof(cst.args[1].args[3].args[1]) == bindingof(cst.args[1])
279279
end
280-
# let cst = parse_and_pass("""
281-
# using Test: @test
282-
# """)
283-
# @test bindingof(cst[1][4]) !== nothing
284-
# end
280+
# let cst = parse_and_pass("""
281+
# using Test: @test
282+
# """)
283+
# @test bindingof(cst[1][4]) !== nothing
284+
# end
285285
let cst = parse_and_pass("""
286-
sin(1,2,3)
287-
""")
286+
sin(1,2,3)
287+
""")
288288
@test errorof(cst.args[1]) === StaticLint.IncorrectCallArgs
289289
end
290290
let cst = parse_and_pass("""
291-
for i in length(1) end
292-
for i in 1.1 end
293-
for i in 1 end
294-
for i in 1:1 end
295-
""")
291+
for i in length(1) end
292+
for i in 1.1 end
293+
for i in 1 end
294+
for i in 1:1 end
295+
""")
296296
@test errorof(cst.args[1].args[1]) === StaticLint.IncorrectIterSpec
297297
@test errorof(cst.args[2].args[1]) === StaticLint.IncorrectIterSpec
298298
@test errorof(cst.args[3].args[1]) === StaticLint.IncorrectIterSpec
299299
@test errorof(cst.args[4].args[1]) === nothing
300300
end
301301

302302
let cst = parse_and_pass("""
303-
[i for i in length(1) end]
304-
[i for i in 1.1 end]
305-
[i for i in 1 end]
306-
[i for i in 1:1 end]
307-
""")
303+
[i for i in length(1) end]
304+
[i for i in 1.1 end]
305+
[i for i in 1 end]
306+
[i for i in 1:1 end]
307+
""")
308308
@test errorof(cst[1][2][3]) === StaticLint.IncorrectIterSpec
309309
@test errorof(cst[2][2][3]) === StaticLint.IncorrectIterSpec
310310
@test errorof(cst[3][2][3]) === StaticLint.IncorrectIterSpec
@@ -319,24 +319,24 @@ f(arg) = arg
319319
end
320320

321321
let cst = parse_and_pass("""
322-
struct Graph
323-
children:: T
324-
end
322+
struct Graph
323+
children:: T
324+
end
325325
326-
function test()
327-
g = Graph()
328-
f = g.children
329-
end""")
326+
function test()
327+
g = Graph()
328+
f = g.children
329+
end""")
330330
@test cst.args[2].args[2].args[2].args[2].args[2].args[1] in bindingof(cst.args[1].args[3].args[1]).refs
331331
end
332332

333333
let cst = parse_and_pass("""
334-
__source__
335-
__module__
336-
macro m()
337-
__source__
338-
__module__
339-
end""")
334+
__source__
335+
__module__
336+
macro m()
337+
__source__
338+
__module__
339+
end""")
340340
@test refof(cst[1]) === nothing
341341
@test refof(cst[2]) === nothing
342342
@test refof(cst[3][3][1]) !== nothing
@@ -1351,9 +1351,13 @@ f(arg) = arg
13511351
@testset "issue 1609" begin
13521352
let
13531353
cst1 = parse_and_pass("function g(@nospecialize(x), y) x + y end")
1354-
cst2 = parse_and_pass("function g(@nospecialize(x), y) y end")
1354+
cst2 = parse_and_pass("function g(@nospecialize(x) = 1) x end")
1355+
cst3 = parse_and_pass("function g(@nospecialize(x) = 1, y = 2) x + y end")
1356+
cst4 = parse_and_pass("function g(@nospecialize(x), y) y end")
13551357
@test !StaticLint.haserror(cst1.args[1].args[1].args[2].args[3])
1356-
@test StaticLint.haserror(cst2.args[1].args[1].args[2].args[3])
1358+
@test !StaticLint.haserror(cst2.args[1].args[1].args[2].args[1])
1359+
@test !StaticLint.haserror(cst3.args[1].args[1].args[2].args[1])
1360+
@test StaticLint.haserror(cst4.args[1].args[1].args[2].args[3])
13571361
end
13581362
end
13591363

0 commit comments

Comments
 (0)