Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions R/scalar_in_linter.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#' Block usage like x %in% "a"
#'
#' `vector %in% set` is appropriate for matching a vector to a set, but if
#' that set has size 1, `==` is more appropriate.
#' 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"`).
#'
#' `scalar %in% vector` is OK, because the alternative (`any(vector == scalar)`)
#' is more circuitous & potentially less clear.
Expand Down Expand Up @@ -46,7 +49,8 @@ scalar_in_linter <- function(in_operators = NULL) {
in_op <- xml_find_chr(bad_expr, "string(SPECIAL)")
lint_msg <- paste0(
"Use comparison operators (e.g. ==, !=, etc.) to match length-1 scalars instead of ", in_op, ". ",
"Note that comparison operators preserve NA where ", in_op, " does not."
"Note that if x can be NA, x == 'arg' is NA whereas x ", in_op, " 'arg' is FALSE, ",
"so consider extra protection for the missing case in your code."
)

xml_nodes_to_lints(
Expand Down
5 changes: 4 additions & 1 deletion man/scalar_in_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading