From 678de9f45bb32af0fb4f9aa2451144e4f8dc845c Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Sat, 22 Jun 2024 09:37:55 +0200 Subject: [PATCH] Do not lint seq() calls with extra arguments --- R/seq_linter.R | 12 ++++++++---- tests/testthat/test-seq_linter.R | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/R/seq_linter.R b/R/seq_linter.R index 3291de2bc1..d07b8cdb67 100644 --- a/R/seq_linter.R +++ b/R/seq_linter.R @@ -27,7 +27,7 @@ #' ) #' #' lint( -#' text = "unlist(lapply(x, seq_len))", +#' text = "unlist(lapply(x, seq, by = 2))", #' linters = seq_linter() #' ) #' @@ -78,10 +78,14 @@ seq_linter <- function() { map_funcs <- c("sapply", "lapply", "map") seq_funcs <- xp_text_in_table(c("seq_len", "seq")) + # count(expr) = 3 because we only want seq() calls without extra arguments sequence_xpath <- glue(" - parent::expr[following-sibling::expr/SYMBOL[ {seq_funcs} ]] - /parent::expr[preceding-sibling::expr/SYMBOL_FUNCTION_CALL[text() = 'unlist']]" - ) + parent::expr/parent::expr[ + count(expr) = 3 + and expr/SYMBOL[ {seq_funcs} ] + and preceding-sibling::expr/SYMBOL_FUNCTION_CALL[text() = 'unlist'] + ] + ") ## The actual order of the nodes is document order ## In practice we need to handle length(x):1 diff --git a/tests/testthat/test-seq_linter.R b/tests/testthat/test-seq_linter.R index 0717e56f1e..be92fc7eb7 100644 --- a/tests/testthat/test-seq_linter.R +++ b/tests/testthat/test-seq_linter.R @@ -147,6 +147,16 @@ test_that("finds potential sequence() replacements", { ) }) +test_that("sequence() is not recommended for complex seq() calls", { + linter <- seq_linter() + + expect_lint( + "unlist(lapply(x, seq, from = 2))", + NULL, + linter + ) +}) + test_that("Message vectorization works for multiple lints", { linter <- seq_linter()