
Check for a common mistake where a size check like 'length' is applied in the wrong place
Source:R/length_test_linter.R
length_test_linter.RdUsage 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.
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()
)
#> <text>:1:1: warning: [length_test_linter] Checking the length of a logical vector is likely a mistake. Did you mean `length(x) == 0`?
#> length(x == 0)
#> ^~~~~~~~~~~~~~
lint(
text = "nrow(x > 0) || ncol(x > 0)",
linters = length_test_linter()
)
#> <text>:1:1: warning: [length_test_linter] Checking the nrow of a logical vector is likely a mistake. Did you mean `nrow(x) > 0`?
#> nrow(x > 0) || ncol(x > 0)
#> ^~~~~~~~~~~
#> <text>:1:16: warning: [length_test_linter] Checking the ncol of a logical vector is likely a mistake. Did you mean `ncol(x) > 0`?
#> nrow(x > 0) || ncol(x > 0)
#> ^~~~~~~~~~~
lint(
text = "NROW(x == 1) && NCOL(y == 1)",
linters = length_test_linter()
)
#> <text>:1:1: warning: [length_test_linter] Checking the NROW of a logical vector is likely a mistake. Did you mean `NROW(x) == 1`?
#> NROW(x == 1) && NCOL(y == 1)
#> ^~~~~~~~~~~~
#> <text>:1:17: warning: [length_test_linter] Checking the NCOL of a logical vector is likely a mistake. Did you mean `NCOL(y) == 1`?
#> NROW(x == 1) && NCOL(y == 1)
#> ^~~~~~~~~~~~
# okay
lint(
text = "length(x) > 0",
linters = length_test_linter()
)
#> ℹ No lints found.
lint(
text = "nrow(x) > 0 || ncol(x) > 0",
linters = length_test_linter()
)
#> ℹ No lints found.
lint(
text = "NROW(x) == 1 && NCOL(y) == 1",
linters = length_test_linter()
)
#> ℹ No lints found.