lintr icon indicating copy to clipboard operation
lintr copied to clipboard

Throw lint for grepl("^", x) in string_boundary_linter

Open MichaelChirico opened this issue 3 years ago • 1 comments

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

MichaelChirico avatar May 30 '22 20:05 MichaelChirico

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.

MichaelChirico avatar May 30 '22 20:05 MichaelChirico

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

MichaelChirico avatar Sep 18 '23 20:09 MichaelChirico