lintr icon indicating copy to clipboard operation
lintr copied to clipboard

Edge case on special `array` for `lengths_linter()`

Open etiennebacher opened this issue 1 year ago • 1 comments

Found this edge case quite deep in a package and it's hard to come up with a less convoluted example.

lintr suggests using lengths():

lintr::lint(
  text = '
test <- array(
  list(NULL, c("max1", "min1")),
  dim = 2L
) |>
  structure(dimnames = list(c(NA, "data1")))

map_int(test, length)',
  linters = lintr::lengths_linter()
)
#> <text>:8:1: warning: [lengths_linter] Use lengths() to find the length of each element in a list.
#> map_int(test, length)
#> ^~~~~~~~~~~~~~~~~~~~~

But it would give an object with different attributes:

test <- array(
  list(NULL, c("max1", "min1")),
  dim = 2L
) |>
  structure(dimnames = list(c(NA, "data1")))

waldo::compare(
  purrr::map_int(test, length),
  lengths(test)
)
#> `names(old)` is a character vector (NA, 'data1')
#> `names(new)` is absent
#> 
#> `dim(old)` is absent
#> `dim(new)` is an integer vector (2)
#> 
#> `dimnames(old)` is absent
#> `dimnames(new)` is a list

etiennebacher avatar Aug 22 '24 10:08 etiennebacher