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. %chin% from {data.table} is matched as well.

Usage

scalar_in_linter()

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()
)
#> ::warning file=<text>,line=1,col=1::file=<text>,line=1,col=1,[scalar_in_linter] Use == to match length-1 scalars, not %in%. Note that == preserves NA where %in% does not.

lint(
  text = "x %chin% 'a'",
  linters = scalar_in_linter()
)
#> ::warning file=<text>,line=1,col=1::file=<text>,line=1,col=1,[scalar_in_linter] Use == to match length-1 scalars, not %chin%. Note that == preserves NA where %chin% does not.

# okay
lint(
  text = "x %in% 1:10",
  linters = scalar_in_linter()
)