Check that closures have the proper usage using codetools::checkUsage()
.
Note that this runs base::eval()
on the code, so do not use with untrusted code.
Usage
object_usage_linter(
interpret_glue = NULL,
interpret_extensions = c("glue", "rlang"),
skip_with = TRUE
)
Arguments
- interpret_glue
(Deprecated) If
TRUE
, interpretglue::glue()
calls to avoid false positives caused by local variables which are only used in a glue expression. Provideinterpret_extensions
instead, see below.- interpret_extensions
Character vector of extensions to interpret. These are meant to cover known cases where variables may be used in ways understood by the reader but not by
checkUsage()
to avoid false positives. Currently"glue"
and"rlang"
are supported, both of which are in the default.For
glue
, examineglue::glue()
calls.For
rlang
, examine.env$key
usages.
- skip_with
A logical. If
TRUE
(default), code inwith()
expressions will be skipped. This argument will be passed toskipWith
argument ofcodetools::checkUsage()
.
See also
linters for a complete list of linters available in lintr.
Examples
# will produce lints
lint(
text = "foo <- function() { x <- 1 }",
linters = object_usage_linter()
)
#> <text>:1:21: warning: [object_usage_linter] local variable 'x' assigned but may not be used
#> foo <- function() { x <- 1 }
#> ^
# okay
lint(
text = "foo <- function(x) { x <- 1 }",
linters = object_usage_linter()
)
#> ℹ No lints found.
lint(
text = "foo <- function() { x <- 1; return(x) }",
linters = object_usage_linter()
)
#> ℹ No lints found.