Report the use of undesirable operators, e.g. :::
or
<<-
and suggest an alternative.
Arguments
- op
Named character vector.
names(op)
correspond to undesirable operators, while the values give a description of why the operator is undesirable. IfNA
, no additional information is given in the lint message. Defaults to default_undesirable_operators. To make small customizations to this list, usemodify_defaults()
.
See also
linters for a complete list of linters available in lintr.
Examples
# defaults for which functions are considered undesirable
names(default_undesirable_operators)
#> [1] "->>" ":::" "<<-"
# will produce lints
lint(
text = "a <<- log(10)",
linters = undesirable_operator_linter()
)
#> ::warning file=<text>,line=1,col=3::file=<text>,line=1,col=3,[undesirable_operator_linter] Operator `<<-` is undesirable. It assigns outside the current environment in a way that can be hard to reason about. Prefer fully-encapsulated functions wherever possible, or, if necessary, assign to a specific environment with assign(). Recall that you can create an environment at the desired scope with new.env().
lint(
text = "mtcars$wt",
linters = undesirable_operator_linter(op = c("$" = "As an alternative, use the `[[` accessor."))
)
#> ::warning file=<text>,line=1,col=7::file=<text>,line=1,col=7,[undesirable_operator_linter] Operator `$` is undesirable. As an alternative, use the `[[` accessor.
# okay
lint(
text = "a <- log(10)",
linters = undesirable_operator_linter()
)
lint(
text = 'mtcars[["wt"]]',
linters = undesirable_operator_linter(op = c("$" = NA))
)
lint(
text = 'mtcars[["wt"]]',
linters = undesirable_operator_linter(op = c("$" = "As an alternative, use the `[[` accessor."))
)