Warning for unused type variables in where

I am wondering whether it would make sense to warn unused extra type variables for cases like

foo(a::AbstractVector{T}) where {T,S} = 1

where it is very likely that S is a typo.

It is certainly innocuous as long as it is not used (and then one gets an error), this would just be a convenience feature.

(asking here because I could not find an issue about this)

Like all other similar features, it should either

  1. Off by default, turn on with cmdline arg or env, or
  2. Have a way to turn it off in the code, or
  3. Be an error

As of now, an external tool like linter is a much better place for it.

5 Likes

How about using Test.detect_unbound_args?

julia> module Demo
       bad(a::AbstractVector{T}) where {T,S} = 1
       good(a::AbstractVector{T}) where {T} = 1
       end
Main.Demo

julia> detect_unbound_args(Demo)
[1] bad(a::AbstractArray{T,1}) where {T, S} in Main.Demo at REPL[121]:2

FYI I wrote a convenient wrapper for this and similar automatable checks: https://github.com/tkf/Aqua.jl (required to workaround https://github.com/JuliaLang/julia/pull/31972)

3 Likes

That’s very helpful, thanks.

From this discussion, I realized that functionality like this should live in a linter, and user who want this are best served by making linting automated and easy to use, possibly as part of CI. You package does exactly this.