Warn/error for empty varargs? (push!, vcat, etc.)

a = [1, 2]
push!(a)

The second line here is effectively a nop, but it runs without any indication that something weird is going on. It’s highly unlikely that this code was intended as is, so it would be useful if we could print a warning here. Catching such mistakes early and informing the user would save a lot of time and bugs. vcat() also allows an empty vararg, and returns a 0-element array (and possibly other inbuilt functions with vararg arguments too).

Is there a significant performance impact from checking whether the given vararg iterator is empty, in such functions? If there is, it can be argued that this should instead go in a linter tool. But it would be much better to have it in the language itself since this is a potential newbie-trap, and so this check is most useful in the REPL, and in environments where the user doesn’t yet have lint tools installed.

I think an issue is that

a = [1,2]
b = []
push!(a, b...)

shouldn’t throw an error (because if you populate b programmatically and it happens to have zero elements, the code should still work). But the same method gets called here as for push!(a).

8 Likes

Some methods of push! are written more carefully to protect against this, eg

julia> d = Dict([:a => 1, :b => 2])
Dict{Symbol, Int64} with 2 entries:
  :a => 1
  :b => 2

julia> push!(d)
ERROR: MethodError: no method matching push!(::Dict{Symbol, Int64})

I think it is worth opening an issue (please check for an existing one first, I couldn’t find one with a quick look).

3 Likes