lintr icon indicating copy to clipboard operation
lintr copied to clipboard

Extend unneeded_concatenation_linter for c(<literal>)?

Open MichaelChirico opened this issue 4 years ago • 3 comments

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).

MichaelChirico avatar Oct 23 '21 04:10 MichaelChirico

Ditto for c(1:10)... anything else?

MichaelChirico avatar Oct 23 '21 04:10 MichaelChirico

c() should be replaced by NULL, as does c(recursive = ...) and c(use.names = ...).

AshesITR avatar Feb 15 '22 21:02 AshesITR

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.

jimhester avatar Feb 16 '22 16:02 jimhester

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

IndrajeetPatil avatar Dec 30 '22 16:12 IndrajeetPatil