-
Notifications
You must be signed in to change notification settings - Fork 186
scalar_in_linter() for x %in% 1 #2084
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 all commits
3dae52e
ca84e2b
e3cb1e3
bf6053f
cb32651
a906398
4b9af2e
30f467e
35f3ed8
89f1fde
335bf6f
89b882a
7481e5d
170d217
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,41 @@ | ||
#' Block usage like x %in% "a" | ||
#' | ||
#' `vector %in% set` is appropriate for matching a vector to a set, but if | ||
#' that set has size 1, `==` is more appropriate. `%chin%` from `data.table` | ||
#' is matched as well. | ||
#' | ||
#' `scalar %in% vector` is OK, because the alternative (`any(vector == scalar)`) | ||
#' is more circuitous & potentially less clear. | ||
#' | ||
#' @evalRd rd_tags("scalar_in_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
scalar_in_linter <- function() { | ||
# TODO(#2085): Extend to include other cases where the RHS is clearly a scalar | ||
# NB: all of logical, integer, double, hex, complex are parsed as NUM_CONST | ||
xpath <- " | ||
//SPECIAL[text() = '%in%' or text() = '%chin%'] | ||
/following-sibling::expr[NUM_CONST[not(starts-with(text(), 'NA'))] or STR_CONST] | ||
/parent::expr | ||
" | ||
|
||
return(Linter(function(source_expression) { | ||
if (!is_lint_level(source_expression, "expression")) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_expression$xml_parsed_content | ||
|
||
bad_expr <- xml_find_all(xml, xpath) | ||
in_op <- xml_find_chr(bad_expr, "string(SPECIAL)") | ||
lint_msg <- | ||
paste0("Use == to match length-1 scalars, not ", in_op, ". Note that == preserves NA where ", in_op, " does not.") | ||
|
||
xml_nodes_to_lints( | ||
bad_expr, | ||
source_expression = source_expression, | ||
lint_message = lint_msg, | ||
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,37 @@ | ||
test_that("scalar_in_linter skips allowed usages", { | ||
linter <- scalar_in_linter() | ||
|
||
expect_lint("x %in% y", NULL, linter) | ||
AshesITR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect_lint("y %in% c('a', 'b')", NULL, linter) | ||
expect_lint("c('a', 'b') %chin% x", NULL, linter) | ||
expect_lint("z %in% 1:3", NULL, linter) | ||
# scalars on LHS are fine (often used as `"col" %in% names(DF)`) | ||
expect_lint("3L %in% x", NULL, linter) | ||
|
||
# this should be is.na(x), but it more directly uses the "always TRUE/FALSE, _not_ NA" | ||
# aspect of %in%, so we delegate linting here to equals_na_linter() | ||
expect_lint("x %in% NA", NULL, linter) | ||
expect_lint("x %in% NA_character_", NULL, linter) | ||
}) | ||
|
||
test_that("scalar_in_linter blocks simple disallowed usages", { | ||
linter <- scalar_in_linter() | ||
lint_in_msg <- rex::rex("Use == to match length-1 scalars, not %in%.") | ||
lint_chin_msg <- rex::rex("Use == to match length-1 scalars, not %chin%.") | ||
|
||
expect_lint("x %in% 1", lint_in_msg, linter) | ||
expect_lint("x %chin% 'a'", lint_chin_msg, linter) | ||
}) | ||
|
||
test_that("multiple lints are generated correctly", { | ||
linter <- scalar_in_linter() | ||
|
||
expect_lint( | ||
trim_some('{ | ||
x %in% 1 | ||
y %chin% "a" | ||
}'), | ||
list("%in%", "%chin%"), | ||
linter | ||
) | ||
}) |
Uh oh!
There was an error while loading. Please reload this page.