Skip to content

Commit 23ebb17

Browse files
authored
Note potential danger if argument can be NA in scalar_in_linter(). (#2958)
1 parent 975c916 commit 23ebb17

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

R/scalar_in_linter.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#' Block usage like x %in% "a"
22
#'
33
#' `vector %in% set` is appropriate for matching a vector to a set, but if
4-
#' that set has size 1, `==` is more appropriate.
4+
#' that set has size 1, `==` is more appropriate. However, if `vector` has
5+
#' also size 1 and can be `NA`, the use of `==` should be accompanied by extra
6+
#' protection for the missing case (for example, `isTRUE(NA == "arg")` or
7+
#' `!is.na(x) && x == "arg"`).
58
#'
69
#' `scalar %in% vector` is OK, because the alternative (`any(vector == scalar)`)
710
#' is more circuitous & potentially less clear.
@@ -46,7 +49,8 @@ scalar_in_linter <- function(in_operators = NULL) {
4649
in_op <- xml_find_chr(bad_expr, "string(SPECIAL)")
4750
lint_msg <- paste0(
4851
"Use comparison operators (e.g. ==, !=, etc.) to match length-1 scalars instead of ", in_op, ". ",
49-
"Note that comparison operators preserve NA where ", in_op, " does not."
52+
"Note that if x can be NA, x == 'arg' is NA whereas x ", in_op, " 'arg' is FALSE, ",
53+
"so consider extra protection for the missing case in your code."
5054
)
5155

5256
xml_nodes_to_lints(

man/scalar_in_linter.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)