terminal_close_linter: Prohibit close() from terminating a function definition

terminal_close_linterR Documentation

Prohibit close() from terminating a function definition

Description

Functions that end in close(x) are almost always better written by using on.exit(close(x)) close to where x is defined and/or opened.

Usage

terminal_close_linter()

Tags

best_practices, robustness

See Also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
code <- paste(
  "f <- function(fl) {",
  "  conn <- file(fl, open = 'r')",
  "  readLines(conn)",
  "  close(conn)",
  "}",
  sep = "\n"
)
writeLines(code)
lint(
  text = code,
  linters = terminal_close_linter()
)

# okay
code <- paste(
  "f <- function(fl) {",
  "  conn <- file(fl, open = 'r')",
  "  on.exit(close(conn))",
  "  readLines(conn)",
  "}",
  sep = "\n"
)
writeLines(code)
lint(
  text = code,
  linters = terminal_close_linter()
)


jimhester/lintr documentation built on April 24, 2024, 8:21 a.m.