all.equal()
returns TRUE
in the absence of differences but return a
character string (not FALSE
) in the presence of differences.
Usage of all.equal()
without wrapping it in isTRUE()
in if
clauses, or
preceded by the negation operator !
, are thus likely to generate unexpected
errors if the compared objects have differences.
An alternative is to use identical()
to compare vector of strings or when
exact equality is expected.
See also
linters for a complete list of linters available in lintr.
Examples
# lints
lint(
text = 'if (all.equal(a, b)) message("equal")',
linters = all_equal_linter()
)
#> <text>:1:5: warning: [all_equal_linter] Wrap all.equal() in isTRUE(), or replace it by identical() if no tolerance is required.
#> if (all.equal(a, b)) message("equal")
#> ^~~~~~~~~~~~~~~
lint(
text = '!all.equal(a, b)',
linters = all_equal_linter()
)
#> <text>:1:2: warning: [all_equal_linter] Wrap all.equal() in isTRUE(), or replace it by identical() if no tolerance is required.
#> !all.equal(a, b)
#> ^~~~~~~~~~~~~~~
lint(
text = 'isFALSE(all.equal(a, b))',
linters = all_equal_linter()
)
#> <text>:1:9: warning: [all_equal_linter] Use !isTRUE() to check for differences in all.equal(). isFALSE(all.equal()) always returns FALSE.
#> isFALSE(all.equal(a, b))
#> ^~~~~~~~~~~~~~~
# okay
lint(
text = 'if (isTRUE(all.equal(a, b))) message("equal")',
linters = all_equal_linter()
)
#> ℹ No lints found.
lint(
text = '!identical(a, b)',
linters = all_equal_linter()
)
#> ℹ No lints found.
lint(
text = "!isTRUE(all.equal(a, b))",
linters = all_equal_linter()
)
#> ℹ No lints found.