lintr
lintr copied to clipboard
Extend unneeded_concatenation_linter for c(<literal>)?
c(1)
Seems like it would fit under unneeded_concatenation_linter, right?
Ditto for any literal value. When c() only has one argument, there's ambiguity when (1) the input is named or (2) the input is a name, when c() might be used to convert a matrix to vector (or other c methods).
Ditto for c(1:10)... anything else?
c() should be replaced by NULL, as does c(recursive = ...) and c(use.names = ...).
I realize the documentation of c() shows the one argument use case as a way to drop attributes except the names, but it seems to me writing a helper function for this case is a better option than relying on this side effect of c(), so linting it as well seems appropriate.
We lint all of these cases by now:
library(lintr)
linter <- unnecessary_concatenation_linter(allow_single_expression = FALSE)
lint(
text = "c()",
linters = linter
)
#> <text>:1:1: style: [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.
#> c()
#> ^~~
lint(
text = "c(1)",
linters = linter
)
#> <text>:1:1: style: [unnecessary_concatenation_linter] Unneeded concatenation of a constant. Remove the "c" call.
#> c(1)
#> ^~~~
lint(
text = "c(1:10)",
linters = linter
)
#> <text>:1:1: style: [unnecessary_concatenation_linter] Unneeded concatenation of a constant. Remove the "c" call.
#> c(1:10)
#> ^~~~~~~
Created on 2022-12-30 with reprex v2.0.2
Only these two are remaining:
library(lintr)
linter <- unnecessary_concatenation_linter(allow_single_expression = FALSE)
lint(
text = "c(recursive = TRUE)",
linters = linter
)
lint(
text = "c(use.names = TRUE)",
linters = linter
)
Created on 2022-12-30 with reprex v2.0.2