Skip to contents

as.integer(1) (or rlang::int(1)) is the same as 1L but the latter is more concise and gets typed correctly at compilation.

Usage

literal_coercion_linter()

Details

The same applies to missing sentinels like NA -- typically, it is not necessary to specify the storage type of NA, but when it is, prefer using the typed version (e.g. NA_real_) instead of a coercion (like as.numeric(NA)).

See also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = "int(1)",
  linters = literal_coercion_linter()
)
#> ::warning file=<text>,line=1,col=1::file=<text>,line=1,col=1,[literal_coercion_linter] Use 1L instead of int(1), i.e., use literals directly where possible, instead of coercion.

lint(
  text = "as.character(NA)",
  linters = literal_coercion_linter()
)
#> ::warning file=<text>,line=1,col=1::file=<text>,line=1,col=1,[literal_coercion_linter] Use NA_character_ instead of as.character(NA), i.e., use literals directly where possible, instead of coercion.

lint(
  text = "rlang::lgl(1L)",
  linters = literal_coercion_linter()
)
#> ::warning file=<text>,line=1,col=1::file=<text>,line=1,col=1,[literal_coercion_linter] Use TRUE instead of rlang::lgl(1L), i.e., use literals directly where possible, instead of coercion.

# okay
lint(
  text = "1L",
  linters = literal_coercion_linter()
)

lint(
  text = "NA_character_",
  linters = literal_coercion_linter()
)

lint(
  text = "TRUE",
  linters = literal_coercion_linter()
)