lintr icon indicating copy to clipboard operation
lintr copied to clipboard

Reconsider `unnecessary_lambda_linter()` default behaviour

Open MichaelChirico opened this issue 2 years ago • 6 comments

    @MichaelChirico is this recommending you use `map_vec(x, foo, y = y)` instead?

We actually recommend using the anonymous function form rather than using ... to pass named arguments through to foo() now. Using the anonymous function form makes it a little easier to see which arguments belong to which function (i.e. does y belong to map_vec() or foo()?), and tends to give slightly better error messages. See the new documentation of ... at https://purrr.tidyverse.org/reference/map.html for more.

Originally posted by @DavisVaughan in https://github.com/r-lib/lintr/pull/1866#discussion_r1054319270

MichaelChirico avatar Dec 21 '22 17:12 MichaelChirico

@DavisVaughan would you recommend going still further, and linting the opposite for purrr mappers, i.e. using ... throws a lint?

MichaelChirico avatar Dec 21 '22 17:12 MichaelChirico

Yea I think that sounds good

DavisVaughan avatar Dec 21 '22 18:12 DavisVaughan

Shouldn't the change be a bit more nuanced? I.e., in the lines of what I was proposing in https://github.com/r-lib/lintr/issues/1531.

The linter would still recommend the direct use of the relevant function rather than an anonymous function when no extra arguments are provided. It would switch to recommending an anonymous function when users want to pass extra arguments.

For example, it would recommend:

map_vec(x, sum)

over

map_vec(x, \(i) sum(i))

but still recommend

map_vec(x, \(i) sum(i, na.rm = TRUE))

Not sure what the official tidyverse position is here :thinking:

Bisaloo avatar Dec 23 '22 08:12 Bisaloo

IIUC, that is what @MichaelChirico is suggesting

i.e. using ... throws a lint?

^ to me this implies that map_vec(x, sum) is fine but map_vec(x, sum, na.rm = TRUE) throws a lint because of the na.rm

DavisVaughan avatar Dec 23 '22 19:12 DavisVaughan

I was reacting to the title since the change we're discussing would not result in the complete exclusion of purrr mappers from unnecessary_lambda_linter but in their handling in a subset of cases.

Bisaloo avatar Dec 24 '22 08:12 Bisaloo

From https://github.com/r-lib/lintr/issues/2421:

Just want to flag pre 3.1.1 release that there is a "small" benefit to using lambdas (particularly in a more interactive/scripting setting) in terms of the error message displayed

testy <- function(x) lapply(x, sum)
testy_lambda <- function(x) lapply(x, \(x) sum(x))
testy("bob")
#> Error in FUN(X[[i]], ...): invalid 'type' (character) of argument
testy_lambda("bob")
#> Error in sum(x): invalid 'type' (character) of argument

Only small and still slightly unhelpful due to lapply not being referenced but I wonder if it at least warrants discussion in the "details" section of the docs.

IndrajeetPatil avatar Dec 13 '23 09:12 IndrajeetPatil