
Unneeded concatenation linter
Source:R/unnecessary_concatenation_linter.R
      unnecessary_concatenation_linter.RdCheck that the c() function is not used without arguments nor with a single constant.
Arguments
- allow_single_expression
- Logical, default - TRUE. If- FALSE, one-expression usages of- c()are always linted, e.g.- c(x)and- c(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 readable- as.vector()instead.- as.vector()is not always preferable, for example with environments (especially,- R6objects), in which case- list()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()
)
#> <text>:1:6: style: [unnecessary_concatenation_linter] Replace unnecessary c() by NULL or, whenever possible, vector() seeded with the correct type and/or length.
#> x <- c()
#>      ^~~
lint(
  text = "x <- c(TRUE)",
  linters = unnecessary_concatenation_linter()
)
#> <text>:1:6: style: [unnecessary_concatenation_linter] Remove unnecessary c() of a constant.
#> x <- c(TRUE)
#>      ^~~~~~~
lint(
  text = "x <- c(1.5 + 2.5)",
  linters = unnecessary_concatenation_linter(allow_single_expression = FALSE)
)
#> <text>:1:6: style: [unnecessary_concatenation_linter] Remove unnecessary c() of a constant expression. Replace with as.vector() if c() is used to strip attributes, e.g. in converting an array to a vector.
#> x <- c(1.5 + 2.5)
#>      ^~~~~~~~~~~~
# okay
lint(
  text = "x <- NULL",
  linters = unnecessary_concatenation_linter()
)
#> ℹ No lints found.
# In case the intent here was to seed a vector of known size
lint(
  text = "x <- integer(4L)",
  linters = unnecessary_concatenation_linter()
)
#> ℹ No lints found.
lint(
  text = "x <- TRUE",
  linters = unnecessary_concatenation_linter()
)
#> ℹ No lints found.
lint(
  text = "x <- c(1.5 + 2.5)",
  linters = unnecessary_concatenation_linter(allow_single_expression = TRUE)
)
#> ℹ No lints found.