forked from r-lib/lintr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lengths_linter.R
37 lines (37 loc) · 1 KB
/
lengths_linter.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#' Require usage of `lengths()` where possible
#'
#' [lengths()] is a function that was added to base R in version 3.2.0 to
#' get the length of each element of a list. It is equivalent to
#' `sapply(x, length)`, but faster and more readable.
#'
#' @examples
#' # will produce lints
#' lint(
#' text = "sapply(x, length)",
#' linters = lengths_linter()
#' )
#'
#' lint(
#' text = "vapply(x, length, integer(1L))",
#' linters = lengths_linter()
#' )
#'
#' lint(
#' text = "purrr::map_int(x, length)",
#' linters = lengths_linter()
#' )
#'
#' # okay
#' lint(
#' text = "lengths(x)",
#' linters = lengths_linter()
#' )
#'
#' @evalRd rd_tags("lengths_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
lengths_linter <- make_linter_from_function_xpath(
function_names = c("sapply", "vapply", "map_int", "map_dbl"),
xpath = "parent::expr/parent::expr[expr/SYMBOL[text() = 'length']]",
lint_message = "Use lengths() to find the length of each element in a list."
)