Block usage of paste()
and paste0()
with messaging functions using ...
Source: R/condition_message_linter.R
condition_message_linter.Rd
This linter discourages combining condition functions like stop()
with string concatenation
functions paste()
and paste0()
. This is because
stop(paste0(...))
is redundant as it is exactly equivalent tostop(...)
stop(paste(...))
is similarly equivalent tostop(...)
with separators (see examples)
The same applies to the other default condition functions as well, i.e., warning()
, message()
,
and packageStartupMessage()
.
See also
linters for a complete list of linters available in lintr.
Examples
# will produce lints
lint(
text = 'stop(paste("a string", "another"))',
linters = condition_message_linter()
)
#> <text>:1:1: warning: [condition_message_linter] Don't use paste to build stop strings. Instead use the fact that these functions build condition message strings from their input (using "" as a separator). For translatable strings, prefer using gettextf().
#> stop(paste("a string", "another"))
#> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lint(
text = 'warning(paste0("a string", " another"))',
linters = condition_message_linter()
)
#> <text>:1:1: warning: [condition_message_linter] Don't use paste0 to build warning strings. Instead use the fact that these functions build condition message strings from their input (using "" as a separator). For translatable strings, prefer using gettextf().
#> warning(paste0("a string", " another"))
#> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# okay
lint(
text = 'stop("a string", " another")',
linters = condition_message_linter()
)
#> ℹ No lints found.
lint(
text = 'warning("a string", " another")',
linters = condition_message_linter()
)
#> ℹ No lints found.
lint(
text = 'warning(paste("a string", "another", sep = "-"))',
linters = condition_message_linter()
)
#> ℹ No lints found.