Check for writes to enclosing/global environments via <<- or ->>
Source:R/check-global-env-write.R
cl_check_global_env_write.RdFlags <<-/->> usage in R/. This operator writes to the nearest
enclosing environment with an existing binding of the same name, or to
.GlobalEnv if none exists – which the CRAN Repository Policy
explicitly forbids. Package code that only uses <<- to update a
variable in a known parent scope (e.g. a closure factory) is technically
safe, but this check can't distinguish that case from an accidental
global write without deeper scope analysis, so every use is flagged for
review.
Examples
pkg_dir <- cl_example_pkg(
r_files = list(foo.R = c(
"set_flag <- function() {",
" flag <<- TRUE",
"}"
))
)
cl_check_global_env_write(pkg_dir)
#> # A tibble: 1 × 6
#> check file line severity message policy_reference
#> <chr> <chr> <int> <ord> <chr> <chr>
#> 1 global_env_write R/foo.R 2 must_fix `<<-` used. This wri… https://contrib…
unlink(pkg_dir, recursive = TRUE)