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

Add potential sequence() lints to seq_linter() #2618

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

## New and improved features

* `seq_linter()` now includes lints to inform about missed opportunities to use the `sequence()` base R function (#2618, @Bisaloo)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think sequence() is not a very well-known function (<500 calls on CRAN vs more than 80K for seq()), here is a good opportunity to quickly introduce it:

"to use the sequence() function, e.g. unlist(lapply(ints, seq))"

* More helpful errors for invalid configs (#2253, @MichaelChirico).
* `library_call_linter()` is extended
+ to encourage all packages to be attached with `library(symbol)`, not `library("symbol", character.only = TRUE)` or "vectorized" approaches looping over package names (part of #884, @MichaelChirico).
Expand Down
31 changes: 30 additions & 1 deletion R/seq_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
#' linters = seq_linter()
#' )
#'
#' lint(
#' text = "unlist(lapply(x, seq_len))",
#' linters = seq_linter()
#' )
#'
#' # okay
#' lint(
#' text = "seq_along(x)",
Expand All @@ -42,6 +47,11 @@
#' linters = seq_linter()
#' )
#'
#' lint(
#' text = "sapply(x, seq_len)",
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved
#' linters = seq_linter()
#' )
#'
#' @evalRd rd_tags("seq_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
Expand All @@ -66,6 +76,15 @@ seq_linter <- function() {
]
")

map_funcs <- xp_text_in_table(c("sapply", "lapply", "map"))
sequence_xpath <- glue("
//SYMBOL_FUNCTION_CALL[ { map_funcs } ]
/parent::expr/parent::expr[
preceding-sibling::expr/SYMBOL_FUNCTION_CALL[text() = 'unlist']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would try and keep the logic in one direction on the AST: check sapply, then check seq_len, then check unlist, roughly

//SYMBOL_FUNCTION_CALL/parent::expr[following-sibling::expr/SYMBOL]/parent::expr/parent::expr[expr/SYMBOL_FUNCTION_CALL]

Also, please switch to use the new find_function_calls approach, which is appropriate here, e.g.

xml_calls <- source_expression$xml_find_function_calls(c("stop", "warning"))

and expr/SYMBOL[text() = 'seq_len']
]"
)

## The actual order of the nodes is document order
## In practice we need to handle length(x):1
get_fun <- function(expr, n) {
Expand Down Expand Up @@ -113,6 +132,16 @@ seq_linter <- function() {
)
)

xml_nodes_to_lints(badx, source_expression, lint_message, type = "warning")
seq_lints <- xml_nodes_to_lints(badx, source_expression, lint_message, type = "warning")

potential_sequence_calls <- xml_find_all(xml, sequence_xpath)
sequence_lints <- xml_nodes_to_lints(
potential_sequence_calls,
source_expression,
"Use sequence() to generate a concatenated sequence of seq_len().",
type = "warning"
)

c(seq_lints, sequence_lints)
})
}
10 changes: 10 additions & 0 deletions man/seq_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions tests/testthat/test-seq_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ test_that("reverse seq is ok", {
)
})

test_that("finds potential sequence() replacements", {
linter <- seq_linter()
lint_msg <- rex::rex("Use sequence()")

expect_lint(
"unlist(lapply(x, seq_len))",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lint now only covers unlist(lapply(x, seq_len)).

What about

  • unlist(lapply(x, seq))
  • unlist(lapply(x, seq, by = 2)), and other variants equivalent to using the from= and/or by= arguments of sequence()?
  • I don't think it's worth trying to find examples like lapply(x, \(xi) seq(xi)) which would be covered by unnecessary_lambda_linter(). But are there any examples that do require looking into a lambda to replicate a sequence()-able call?

(it would be nice to get a sense of how many hits these are getting before investing too much time -- and also feel free to earmark some of this as a TODO in a follow-up issue, though lapply(x,seq) is trivial enough to be done here)

Copy link
Contributor Author

@Bisaloo Bisaloo Jun 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unlist(lapply(x, seq, by = 2)), and other variants equivalent to using the from= and/or by= arguments of sequence()?

I don't think there is a direct equivalence:

  • from has a different behaviour in seq() and sequence()

    unlist(lapply(1:5, seq, from = 2))
    #>  [1] 2 1 2 2 3 2 3 4 2 3 4 5
    
    sequence(1:5, from = 2)
    #>  [1] 2 2 3 2 3 4 2 3 4 5 2 3 4 5 6

    Created on 2024-06-22 with reprex v2.1.0

  • As far as I can tell, by= cannot be used on its own without from=, which brings us back to the previous case

    seq(20, by = 2)
    #> Error in seq.default(20, by = 2): wrong sign in 'by' argument

    Created on 2024-06-22 with reprex v2.1.0

I have edited the xpath to make sure this type of call doesn't lint.

lint_msg,
linter
)

# Even for prefixed purrr:: calls
expect_lint(
"unlist(purrr::map(x, seq_len))",
lint_msg,
linter
)
})

test_that("Message vectorization works for multiple lints", {
linter <- seq_linter()

Expand Down Expand Up @@ -173,6 +191,18 @@ test_that("Message vectorization works for multiple lints", {
),
linter
)

expect_lint(
trim_some("{
1:NROW(x)
unlist(lapply(y, seq_len))
}"),
list(
list(rex::rex("seq_len(NROW(...))", anything, "1:NROW(...)"), line_number = 2L),
list(rex::rex("sequence()"), line_number = 3L)
),
linter
)
})

test_that("Message recommends rev() correctly", {
Expand Down
Loading