Skip to contents

Re-using existing names creates a risk of subtle error best avoided. Avoiding this practice also encourages using better, more descriptive names.

Usage

object_overwrite_linter(
  packages = c("base", "stats", "utils", "tools", "methods", "graphics", "grDevices"),
  allow_names = character()
)

Arguments

packages

Character vector of packages to search for names that should be avoided. Defaults to the most common default packages: base, stats, utils, tools, methods, graphics, and grDevices.

allow_names

Character vector of object names to ignore, i.e., which are allowed to collide with exports from packages.

See also

Examples

# will produce lints
code <- "function(x) {\n  data <- x\n  data\n}"
writeLines(code)
#> function(x) {
#>   data <- x
#>   data
#> }
lint(
  text = code,
  linters = object_overwrite_linter()
)
#> ::warning file=<text>,line=2,col=3::file=<text>,line=2,col=3,[object_overwrite_linter] 'data' is an exported object from package 'utils'. Avoid re-using such symbols.

code <- "function(x) {\n  lint <- 'fun'\n  lint\n}"
writeLines(code)
#> function(x) {
#>   lint <- 'fun'
#>   lint
#> }
lint(
  text = code,
  linters = object_overwrite_linter(packages = "lintr")
)
#> ::warning file=<text>,line=2,col=3::file=<text>,line=2,col=3,[object_overwrite_linter] 'lint' is an exported object from package 'lintr'. Avoid re-using such symbols.

# okay
code <- "function(x) {\n  data('mtcars')\n}"
writeLines(code)
#> function(x) {
#>   data('mtcars')
#> }
lint(
  text = code,
  linters = object_overwrite_linter()
)

code <- "function(x) {\n  data <- x\n  data\n}"
writeLines(code)
#> function(x) {
#>   data <- x
#>   data
#> }
lint(
  text = code,
  linters = object_overwrite_linter(packages = "base")
)

# names in function signatures are ignored
lint(
  text = "function(data) data <- subset(data, x > 0)",
  linters = object_overwrite_linter()
)