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()
)
#> <text>:2:3: warning: [object_overwrite_linter] 'data' is an exported object from package 'utils'. Avoid re-using such symbols.
#>   data <- x
#>   ^~~~

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")
)
#> <text>:2:3: warning: [object_overwrite_linter] 'lint' is an exported object from package 'lintr'. Avoid re-using such symbols.
#>   lint <- 'fun'
#>   ^~~~

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

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")
)
#>  No lints found.

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