length_test_linter: Check for a common mistake where a size check like 'length'...

View source: R/length_test_linter.R

length_test_linterR Documentation

Check for a common mistake where a size check like 'length' is applied in the wrong place

Description

Usage like length(x == 0) is a mistake. If you intended to check x is empty, use length(x) == 0. Other mistakes are possible, but running length() on the outcome of a logical comparison is never the best choice.

Usage

length_test_linter()

Details

The linter also checks for similar usage with nrow(), ncol(), NROW(), and NCOL().

Tags

best_practices, consistency, robustness

See Also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = "length(x == 0)",
  linters = length_test_linter()
)

lint(
  text = "nrow(x > 0) || ncol(x > 0)",
  linters = length_test_linter()
)

lint(
  text = "NROW(x == 1) && NCOL(y == 1)",
  linters = length_test_linter()
)

# okay
lint(
  text = "length(x) > 0",
  linters = length_test_linter()
)

lint(
  text = "nrow(x) > 0 || ncol(x) > 0",
  linters = length_test_linter()
)

lint(
  text = "NROW(x) == 1 && NCOL(y) == 1",
  linters = length_test_linter()
)


lintr documentation built on Nov. 27, 2025, 9:06 a.m.