coalesce_linter: Encourage usage of the null coalescing operator %||%

View source: R/coalesce_linter.R

coalesce_linterR Documentation

Encourage usage of the null coalescing operator ⁠%||%⁠

Description

The x %||% y is equivalent to if (is.null(x)) y else x, but more expressive. It is exported by R since 4.4.0, and equivalents have been available in other tidyverse packages for much longer, e.g. 2008 for ggplot2.

Usage

coalesce_linter()

Tags

best_practices, consistency, readability

See Also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = "if (is.null(x)) y else x",
  linters = coalesce_linter()
)

lint(
  text = "if (!is.null(x)) x else y",
  linters = coalesce_linter()
)

lint(
  text = "if (is.null(x[1])) x[2] else x[1]",
  linters = coalesce_linter()
)

# okay
lint(
  text = "x %||% y",
  linters = coalesce_linter()
)

lint(
  text = "x %||% y",
  linters = coalesce_linter()
)

lint(
  text = "x[1] %||% x[2]",
  linters = coalesce_linter()
)



r-lib/lintr documentation built on June 9, 2025, 7:45 a.m.