Unneeded concatenation linter
Source:R/unnecessary_concatenation_linter.R
unnecessary_concatenation_linter.Rd
Check that the c()
function is not used without arguments nor with a single constant.
Arguments
- allow_single_expression
Logical, default
TRUE
. IfFALSE
, one-expression usages ofc()
are always linted, e.g.c(x)
andc(matrix(...))
. In some such cases,c()
is being used for its side-effect of stripping non-name attributes; it is usually preferable to use the more readableas.vector()
instead.as.vector()
is not always preferable, for example with environments (especially,R6
objects), in which caselist()
is the better alternative.
See also
linters for a complete list of linters available in lintr.
Examples
# will produce lints
lint(
text = "x <- c()",
linters = unnecessary_concatenation_linter()
)
#> ::warning file=<text>,line=1,col=6::file=<text>,line=1,col=6,[unnecessary_concatenation_linter] Unneeded concatenation without arguments. Replace the "c" call by NULL or, whenever possible, vector() seeded with the correct type and/or length.
lint(
text = "x <- c(TRUE)",
linters = unnecessary_concatenation_linter()
)
#> ::warning file=<text>,line=1,col=6::file=<text>,line=1,col=6,[unnecessary_concatenation_linter] Unneeded concatenation of a constant. Remove the "c" call.
lint(
text = "x <- c(1.5 + 2.5)",
linters = unnecessary_concatenation_linter(allow_single_expression = FALSE)
)
#> ::warning file=<text>,line=1,col=6::file=<text>,line=1,col=6,[unnecessary_concatenation_linter] Unneeded concatenation of a simple expression. Remove the "c" call, replacing with "as.vector" if using "c" to string attributes, e.g. in converting an array to a vector.
# okay
lint(
text = "x <- NULL",
linters = unnecessary_concatenation_linter()
)
# In case the intent here was to seed a vector of known size
lint(
text = "x <- integer(4L)",
linters = unnecessary_concatenation_linter()
)
lint(
text = "x <- TRUE",
linters = unnecessary_concatenation_linter()
)
lint(
text = "x <- c(1.5 + 2.5)",
linters = unnecessary_concatenation_linter(allow_single_expression = TRUE)
)