lintr icon indicating copy to clipboard operation
lintr copied to clipboard

New linter for unnamed arguments combined with `...`

Open teunbrand opened this issue 1 year ago • 1 comments

I thought it might be handy to have a linter that checks for the pattern fun(x, y, ...) where x and y are unnamed arguments and are matched to fun() based on their position. This is risky to combine with ... as a named argument in ... might supplant x or y and shift subsequent arguments.

To illustrate, this piece of code with named arguments correctly gives the 'matched by multiple arguments' error:

inner <- function(str, num, lgl) {
  stopifnot(is.character(str), is.numeric(num), is.logical(lgl))
  "succes"
}

outer <- function(...) inner(str = "a", num = 4, ...)

outer(TRUE)
#> [1] "succes"
outer(num = 5)
#> Error in inner(str = "a", num = 4, ...): formal argument "num" matched by multiple actual arguments

However, if we don't use named arguments we get into the following trouble. Note that num = 5 is passed through ... which means that the 2nd default argument (4) that is supposed to be the num argument is now shifted to the lgl argument.

outer <- function(...) inner("a", 4, ...)

outer(TRUE)
#> [1] "succes"
outer(num = 5)
#> Error in inner("a", 4, ...): is.logical(lgl) is not TRUE

Created on 2024-07-10 with reprex v2.1.0

For this reason, I think it'd be good to recommend naming arguments when combined with ....

teunbrand avatar Jul 10 '24 12:07 teunbrand