-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f4da55
commit 3d22c75
Showing
13 changed files
with
148 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#' Require usage of !any(.) over all(!.), !all(.) over any(!.) | ||
#' | ||
#' `any(!x)` is logically equivalent to `!any(x)`; ditto for the equivalence of | ||
#' `all(!x)` and `!any(x)`. Negating after aggregation only requires inverting | ||
#' one logical value, and is typically more readable. | ||
#' | ||
#' @evalRd rd_tags("outer_negation_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
outer_negation_linter <- function() { | ||
Linter(function(source_file) { | ||
if (length(source_file$xml_parsed_content) == 0L) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_file$xml_parsed_content | ||
|
||
# NB: the double negation is a bity hairy, but it's what we need to check if | ||
# _all_ of the inputs to any(..., na.rm=na.rm) are negated, i.e., there are | ||
# _not_ any entries that are _not_ negated. IINM that's what we're stuck | ||
# with in xpath if we want to guarantee a condition on _all_ <expr> | ||
# coming after any( and before na.rm= . | ||
# NB: requirement that count(expr)>1 is to prevent any() from linting | ||
# e.g. in magrittr pipelines. | ||
xpath <- "//expr[ | ||
expr[SYMBOL_FUNCTION_CALL[text() = 'any' or text() = 'all']] | ||
and count(expr) > 1 | ||
and not(expr[ | ||
position() > 1 | ||
and not(OP-EXCLAMATION) | ||
and not(preceding-sibling::*[2][self::SYMBOL_SUB]) | ||
]) | ||
]" | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
|
||
return(lapply( | ||
bad_expr, | ||
xml_nodes_to_lint, | ||
source_file = source_file, | ||
lint_message = function(expr) { | ||
matched_call <- xml2::xml_text(xml2::xml_find_first(expr, "expr/SYMBOL_FUNCTION_CALL")) | ||
inverse_call <- if (matched_call == "any") "all" else "any" | ||
message <- sprintf("!%s(x) is better than %s(!x).", inverse_call, matched_call) | ||
paste( | ||
message, | ||
"The former applies negation only once after aggregation instead of many times for each element of x." | ||
) | ||
}, | ||
type = "warning" | ||
)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
test_that("outer_negation_linter skips allowed usages", { | ||
expect_lint("x <- any(y)", NULL, outer_negation_linter()) | ||
expect_lint("y <- all(z)", NULL, outer_negation_linter()) | ||
|
||
# extended usage of any is not covered | ||
expect_lint("any(!a & b)", NULL, outer_negation_linter()) | ||
expect_lint("all(a | !b)", NULL, outer_negation_linter()) | ||
|
||
expect_lint("any(a, b)", NULL, outer_negation_linter()) | ||
expect_lint("all(b, c)", NULL, outer_negation_linter()) | ||
expect_lint("any(!a, b)", NULL, outer_negation_linter()) | ||
expect_lint("all(a, !b)", NULL, outer_negation_linter()) | ||
expect_lint("any(a, !b, na.rm = TRUE)", NULL, outer_negation_linter()) | ||
}) | ||
|
||
test_that("outer_negation_linter blocks simple disallowed usages", { | ||
expect_lint( | ||
"any(!x)", | ||
rex::rex("!all(x) is better than any(!x)"), | ||
outer_negation_linter() | ||
) | ||
|
||
expect_lint( | ||
"all(!foo(x))", | ||
rex::rex("!any(x) is better than all(!x)"), | ||
outer_negation_linter() | ||
) | ||
|
||
# na.rm doesn't change the recommendation | ||
expect_lint( | ||
"any(!x, na.rm = TRUE)", | ||
rex::rex("!all(x) is better than any(!x)"), | ||
outer_negation_linter() | ||
) | ||
|
||
# also catch nested usage | ||
expect_lint( | ||
"all(!(x + y))", | ||
rex::rex("!any(x) is better than all(!x)"), | ||
outer_negation_linter() | ||
) | ||
|
||
# catch when all inputs are negated | ||
expect_lint( | ||
"any(!x, !y)", | ||
rex::rex("!all(x) is better than any(!x)"), | ||
outer_negation_linter() | ||
) | ||
|
||
expect_lint( | ||
"all(!x, !y, na.rm = TRUE)", | ||
rex::rex("!any(x) is better than all(!x)"), | ||
outer_negation_linter() | ||
) | ||
}) | ||
|
||
test_that("outer_negation_linter doesn't trigger on empty calls", { | ||
# minimal version of issue | ||
expect_lint("any()", NULL, outer_negation_linter()) | ||
# closer to what was is practically relevant, as another regression test | ||
expect_lint("x %>% any()", NULL, outer_negation_linter()) | ||
}) |