r-lib / lintr

Static Code Analysis for R
https://lintr.r-lib.org
Other
1.16k stars 184 forks source link

Suggestion: add rule for `!isTRUE` -> `isFALSE` #2607

Closed m-muecke closed 2 weeks ago

MichaelChirico commented 3 weeks ago

Good idea. I think it can be part of comparison_negation_linter().

Here are 10Ks of hits:

https://github.com/search?q=lang%3AR+%2F%5B%21%5D%5Cs*isTRUE%5C%28%2F&type=code

m-muecke commented 3 weeks ago

Ah, I guess I forget of the use-case where NULL or NA wants be checked:

!isTRUE(NULL) == isFALSE(NULL)
#> [1] FALSE
!isTRUE(NA) == isFALSE(NA)
#> [1] FALSE

Created on 2024-06-14 with reprex v2.1.0

MichaelChirico commented 2 weeks ago

Oh, true, more generally,

isTRUE(x) <=> length(x) == 1 && is.logical(x) && !is.na(x) && x
isFALSE(x) <=> length(x) == 1 && is.logical(x) && !is.na(x) && !x

So !isTRUE(x) != isFALSE(x) whenever length(x) != 1 || !is.logical(x) || is.na(x).

So actually I'm not sure there's anything we can do here statically.