Block usage of comparison operators with known-list() functions like lapply
Source:R/list_comparison_linter.R
list_comparison_linter.Rd
Usage like lapply(x, sum) > 10
is awkward because the list must first
be coerced to a vector for comparison. A function like vapply()
should be preferred.
See also
linters for a complete list of linters available in lintr.
Examples
# will produce lints
lint(
text = "lapply(x, sum) > 10",
linters = list_comparison_linter()
)
#> <text>:1:1: warning: [list_comparison_linter] The output of lapply(), a list(), is being coerced for comparison by `>`. Instead, use a mapper that generates a vector with the correct type directly, for example vapply(x, FUN, character(1L)) if the output is a string.
#> lapply(x, sum) > 10
#> ^~~~~~~~~~~~~~~~~~~
# okay
lint(
text = "unlist(lapply(x, sum)) > 10",
linters = list_comparison_linter()
)
#> ℹ No lints found.