implicit_assignment_linter: Avoid implicit assignment in function calls

View source: R/implicit_assignment_linter.R

implicit_assignment_linterR Documentation

Avoid implicit assignment in function calls

Description

Assigning inside function calls makes the code difficult to read, and should be avoided, except for functions that capture side-effects (e.g. capture.output()).

Usage

implicit_assignment_linter(
  except = c("bquote", "expression", "expr", "quo", "quos", "quote"),
  allow_lazy = FALSE,
  allow_scoped = FALSE
)

Arguments

except

A character vector of functions to be excluded from linting.

allow_lazy

logical, default FALSE. If TRUE, assignments that only trigger conditionally (e.g. in the RHS of && or || expressions) are skipped.

allow_scoped

Logical, default FALSE. If TRUE, "scoped assignments", where the object is assigned in the statement beginning a branch and used only within that branch, are skipped.

Tags

best_practices, configurable, readability, style

See Also

Examples

# will produce lints
lint(
  text = "if (x <- 1L) TRUE",
  linters = implicit_assignment_linter()
)

lint(
  text = "mean(x <- 1:4)",
  linters = implicit_assignment_linter()
)

# okay
lines <- "x <- 1L\nif (x) TRUE"
writeLines(lines)
lint(
  text = lines,
  linters = implicit_assignment_linter()
)

lines <- "x <- 1:4\nmean(x)"
writeLines(lines)
lint(
  text = lines,
  linters = implicit_assignment_linter()
)

lint(
  text = "A && (B <- foo(A))",
  linters = implicit_assignment_linter(allow_lazy = TRUE)
)

lines <- c(
  "if (any(idx <- x < 0)) {",
  "  stop('negative elements: ', toString(which(idx)))",
  "}"
)
writeLines(lines)
lint(
  text = lines,
  linters = implicit_assignment_linter(allow_scoped = TRUE)
)


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