-
Notifications
You must be signed in to change notification settings - Fork 195
New boolean_arithmetic_linter #1579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
62d2db8
7c64b48
7e122ba
bb0d86c
0c38d51
2ac1ec8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #' Require usage of boolean operators over equivalent arithmetic | ||
| #' | ||
| #' `length(which(x == y)) == 0` is the same as `!any(x == y)`, but the latter | ||
| #' is more readable and more efficient. | ||
| #' | ||
| #' #' @evalRd rd_tags("boolean_arithmetic_linter") | ||
| #' @seealso [linters] for a complete list of linters available in lintr. | ||
| #' @export | ||
| boolean_arithmetic_linter <- function() { | ||
| # TODO(#1580): sum() cases x %in% y, A [&|] B, !A, is.na/is.nan/is.finite/is.infinite/is.element | ||
| # TODO(#1581): extend to include all()-alike expressions | ||
| zero_expr <- | ||
| "(EQ or NE or GT or LE) and expr[NUM_CONST[text() = '0' or text() = '0L']]" | ||
| one_expr <- | ||
| "(LT or GE) and expr[NUM_CONST[text() = '1' or text() = '1L']]" | ||
| length_xpath <- glue::glue(" | ||
| //SYMBOL_FUNCTION_CALL[text() = 'which' or text() = 'grep'] | ||
| /parent::expr | ||
| /parent::expr | ||
| /parent::expr[ | ||
| expr[SYMBOL_FUNCTION_CALL[text() = 'length']] | ||
| and parent::expr[ ({zero_expr}) or ({one_expr})] | ||
| ] | ||
| ") | ||
| sum_xpath <- glue::glue(" | ||
| //SYMBOL_FUNCTION_CALL[text() = 'sum'] | ||
| /parent::expr | ||
| /parent::expr[ | ||
| expr[ | ||
| expr[SYMBOL_FUNCTION_CALL[text() = 'grepl']] | ||
| or (EQ or NE or GT or LT or GE or LE) | ||
| ] and parent::expr[ ({zero_expr}) or ({one_expr})] | ||
| ] | ||
| ") | ||
| any_xpath <- paste(length_xpath, "|", sum_xpath) | ||
|
|
||
| Linter(function(source_expression) { | ||
| if (!is_lint_level(source_expression, "expression")) { | ||
| return(list()) | ||
| } | ||
|
|
||
| xml <- source_expression$xml_parsed_content | ||
|
|
||
| any_expr <- xml2::xml_find_all(xml, any_xpath) | ||
|
|
||
| xml_nodes_to_lints( | ||
| any_expr, | ||
| source_expression = source_expression, | ||
| # TODO(michaelchirico): customize this? | ||
| lint_message = paste( | ||
| "Use any() to express logical aggregations.", | ||
| "For example, replace length(which(x == y)) == 0 with !any(x == y)." | ||
| ), | ||
| type = "warning" | ||
| ) | ||
| }) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| test_that("boolean_arithmetic_linter requires use of any() or !any()", { | ||
| linter <- boolean_arithmetic_linter() | ||
| lint_msg <- rex::rex("Use any() to express logical aggregations.") | ||
|
|
||
| expect_lint("length(which(x == y)) == 0", lint_msg, linter) | ||
| # anything passed to which() can be assumed to be logical | ||
| expect_lint("length(which(is_treatment)) == 0L", lint_msg, linter) | ||
| # regex version | ||
| expect_lint("length(grep(pattern, x)) == 0", lint_msg, linter) | ||
| # sum version | ||
| expect_lint("sum(x == y) == 0L", lint_msg, linter) | ||
| expect_lint("sum(grepl(pattern, x)) == 0", lint_msg, linter) | ||
|
|
||
| # non-== comparisons | ||
| expect_lint("length(which(x == y)) > 0L", lint_msg, linter) | ||
| expect_lint("length(which(is_treatment)) < 1", lint_msg, linter) | ||
| expect_lint("length(grep(pattern, x)) >= 1L", lint_msg, linter) | ||
| expect_lint("sum(x == y) != 0", lint_msg, linter) | ||
| expect_lint("sum(grepl(pattern, x)) > 0L", lint_msg, linter) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.