Skip to contents

vector %in% set is appropriate for matching a vector to a set, but if that set has size 1, == is more appropriate. However, if vector has also size 1 and can be NA, the use of == should be accompanied by extra protection for the missing case (for example, isTRUE(NA == "arg") or !is.na(x) && x == "arg").

Usage

scalar_in_linter(in_operators = NULL)

Arguments

in_operators

Character vector of additional infix operators that behave like the %in% operator, e.g. {data.table}'s %chin% operator.

Details

scalar %in% vector is OK, because the alternative (any(vector == scalar)) is more circuitous & potentially less clear.

See also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = "x %in% 1L",
  linters = scalar_in_linter()
)
#> <text>:1:1: warning: [scalar_in_linter] Use comparison operators (e.g. ==, !=, etc.) to match length-1 scalars instead of %in%. Note that if x can be NA, x == 'arg' is NA whereas x %in% 'arg' is FALSE, so consider extra protection for the missing case in your code.
#> x %in% 1L
#> ^~~~~~~~~

lint(
  text = "x %chin% 'a'",
  linters = scalar_in_linter(in_operators = "%chin%")
)
#> <text>:1:1: warning: [scalar_in_linter] Use comparison operators (e.g. ==, !=, etc.) to match length-1 scalars instead of %chin%. Note that if x can be NA, x == 'arg' is NA whereas x %chin% 'arg' is FALSE, so consider extra protection for the missing case in your code.
#> x %chin% 'a'
#> ^~~~~~~~~~~~

# okay
lint(
  text = "x %in% 1:10",
  linters = scalar_in_linter()
)
#>  No lints found.