nzchar_linter: Require usage of nzchar where appropriate

View source: R/nzchar_linter.R

nzchar_linterR Documentation

Require usage of nzchar where appropriate

Description

nzchar() efficiently determines which of a vector of strings are empty (i.e., are ""). It should in most cases be used instead of constructions like string == "" or nchar(string) == 0.

Usage

nzchar_linter()

Details

One crucial difference is in the default handling of NA_character_, i.e., missing strings. nzchar(NA_character_) is TRUE, while NA_character_ == "" and nchar(NA_character_) == 0 are both NA. Therefore, for strict compatibility, use nzchar(x, keepNA = TRUE). If the input is known to be complete (no missing entries), this argument can be dropped for conciseness.

Tags

best_practices, consistency, efficiency

See Also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = "x[x == '']",
  linters = nzchar_linter()
)

lint(
  text = "x[nchar(x) > 0]",
  linters = nzchar_linter()
)

# okay
lint(
  text = "x[!nzchar(x, keepNA = TRUE)]",
  linters = nzchar_linter()
)

lint(
  text = "x[nzchar(x, keepNA = TRUE)]",
  linters = nzchar_linter()
)


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