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.

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 comparison operators preserve NA where %in% does not.
#> 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 comparison operators preserve NA where %chin% does not.
#> x %chin% 'a'
#> ^~~~~~~~~~~~

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