Skip to content

Edge case on special array for lengths_linter() #2649

@etiennebacher

Description

@etiennebacher

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    false-positivecode that shouldn't lint, but does

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions