-
Notifications
You must be signed in to change notification settings - Fork 195
New redundant_equals_linter #1556
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 1 commit
ea47fa8
371efb8
65435c5
f4f7531
9a69acc
3281af1
a51d143
01f6cdf
5467669
ff3e8d2
5030cd5
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,36 @@ | ||
| #' Block usage of ==, != on logical vectors | ||
| #' | ||
| #' Testing `x == TRUE` is redundant if `x` is a logical vector. Wherever this | ||
| #' is used to improve readability, the solution should instead be to improve | ||
| #' the naming of the object to better indicate that its contents are logical. | ||
| #' | ||
MichaelChirico marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #' @export | ||
| redundant_equals_linter <- function() { | ||
| xpath <- paste0( | ||
| c("//EQ", "//NE"), | ||
| "/parent::expr/expr[NUM_CONST[text() = 'TRUE' or text() = 'FALSE']]/parent::expr", | ||
| collapse = " | " | ||
| ) | ||
|
|
||
| Linter(function(source_expression) { | ||
| if (!is_lint_level(source_expression, "expression")) { | ||
| return(list()) | ||
| } | ||
|
|
||
| xml <- source_expression$xml_parsed_content | ||
|
|
||
| bad_expr <- xml2::xml_find_all(xml, xpath) | ||
| op <- xml2::xml_text(xml2::xml_find_first(bad_expr, "*[2]")) | ||
|
|
||
| xml_nodes_to_lints( | ||
| bad_expr, | ||
| source_expression = source_expression, | ||
| lint_message = paste( | ||
| "Using", op, "on a logical vector is redundant.", | ||
| "Well-named logical vectors can be used directly in filtering.", | ||
| "For data.table's `i` argument, wrap the column name in (), like DT[(is_treatment)]." | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ), | ||
| 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.
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,30 @@ | ||
| test_that("redundant_equals_linter skips allowed usages", { | ||
| # comparisons to non-logical constants | ||
| expect_lint("x == 1", NULL, redundant_equals_linter()) | ||
| # comparison to TRUE as a string | ||
| expect_lint("x != 'TRUE'", NULL, redundant_equals_linter()) | ||
| }) | ||
|
|
||
| test_that("redundant_equals_linter blocks simple disallowed usages", { | ||
| linter <- redundant_equals_linter() | ||
| eq_msg <- rex::rex("Using == on a logical vector is redundant.") | ||
| ne_msg <- rex::rex("Using != on a logical vector is redundant.") | ||
| expect_lint("x == TRUE", eq_msg, linter) | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| expect_lint("x != TRUE", ne_msg, linter) | ||
| expect_lint("x == FALSE", eq_msg, linter) | ||
| expect_lint("x != FALSE", ne_msg, linter) | ||
|
|
||
| # order doesn't matter | ||
| expect_lint("TRUE == x", eq_msg, linter) | ||
MichaelChirico marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }) | ||
|
|
||
| test_that("mutliple lints return correct custom messages", { | ||
| expect_lint( | ||
| "list(x == TRUE, y != TRUE)", | ||
| list( | ||
| "Using == on a logical vector", | ||
| "Using != on a logical vector" | ||
| ), | ||
| redundant_equals_linter() | ||
| ) | ||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.