Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edge case on special array for lengths_linter() #2649

Open
etiennebacher opened this issue Aug 22, 2024 · 1 comment
Open

Edge case on special array for lengths_linter() #2649

etiennebacher opened this issue Aug 22, 2024 · 1 comment
Labels
false-positive code that shouldn't lint, but does

Comments

@etiennebacher
Copy link

etiennebacher commented Aug 22, 2024

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
@MichaelChirico
Copy link
Collaborator

I would just mark this as # nolint. We might put a note in the documentation... array-of-lists is a strange object. Whether to retain dim() in this case seems unclear.

@MichaelChirico MichaelChirico added the false-positive code that shouldn't lint, but does label Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive code that shouldn't lint, but does
Projects
None yet
Development

No branches or pull requests

2 participants