lintr
lintr copied to clipboard
Throw lint for grepl("^", x) in string_boundary_linter
grepl("^", x) doesn't have a strict startsWith() equivalent, but it is quite poor usage -- !is.na(x) is clearly far superior.
x = c(NA, "", "abc")
cbind(
grepl = grepl("^", x),
starts_with = startsWith(x, ""),
is_na = !is.na(x)
)
# grepl starts_with is_na
# [1,] FALSE NA FALSE
# [2,] TRUE TRUE TRUE
# [3,] TRUE TRUE TRUE
Same goes for grepl("$", x)
Originally posted by @AshesITR in https://github.com/r-lib/lintr/pull/1311#discussion_r884246795
BTW, stringr::str_detect() I guess should be handled separately -- str_detect(x, "^") returns NA for NA input, so it's indeed equivalent with the startsWith() case.
FWIW, there's no code doing this on GitHub: https://github.com/search?q=%2Fgrep%5Bl%5D%3F%5B(%5D%2F%20%2F%5B%22%5D%5B%5E%5D%5B%22%5D%2F%20lang%3AR&type=code