-
Notifications
You must be signed in to change notification settings - Fork 195
New empty_assignment_linter #1637
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 4 commits
b0ca754
22fa266
d94182b
e2defd1
835b969
3463a34
1cbdf8a
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,62 @@ | ||
| #' Block assignment of `{}` | ||
| #' | ||
| #' Assignment of `{}` is the same as assignment of `NULL`; use the latter | ||
| #' for clarity. Closely related: [unneeded_concatenation_linter()]. | ||
| #' | ||
| #' @examples | ||
| #' # will produce lints | ||
| #' lint( | ||
| #' text = "x <- {}", | ||
| #' linters = empty_assignment_linter() | ||
| #' ) | ||
| #' | ||
| #' cat("x = {\n}") | ||
| #' lint( | ||
| #' text = "x = {\n}", | ||
| #' linters = empty_assignment_linter() | ||
| #' ) | ||
| #' | ||
| #' # okay | ||
| #' lint( | ||
| #' text = "x <- { 3 + 4 }", | ||
| #' linters = empty_assignment_linter() | ||
| #' ) | ||
| #' | ||
| #' lint( | ||
| #' text = "x <- NULL", | ||
| #' linters = empty_assignment_linter() | ||
| #' ) | ||
| #' | ||
| #' @evalRd rd_tags("empty_assignment_linter") | ||
| #' @seealso [linters] for a complete list of linters available in lintr. | ||
| #' @export | ||
| empty_assignment_linter <- function() { | ||
| # for some reason, the parent in the `=` case is <equal_assign>, not <expr>, hence parent::expr | ||
| xpath <- " | ||
| //OP-LEFT-BRACE[following-sibling::*[1][self::OP-RIGHT-BRACE]] | ||
| /parent::expr[ | ||
| preceding-sibling::LEFT_ASSIGN | ||
| or preceding-sibling::EQ_ASSIGN | ||
| or following-sibling::RIGHT_ASSIGN | ||
| ] | ||
| /parent::* | ||
| " | ||
|
|
||
| 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) | ||
|
|
||
| xml_nodes_to_lints( | ||
| bad_expr, | ||
| source_expression = source_expression, | ||
| lint_message = | ||
| "Assign NULL explicitly or, whenever possible, allocate the empty object with the right type & size.", | ||
| 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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| test_that("empty_assignment_linter skips valid usage", { | ||
| expect_lint("x <- { 3 + 4 }", NULL, empty_assignment_linter()) | ||
| expect_lint("x <- if (x > 1) { 3 + 4 }", NULL, empty_assignment_linter()) | ||
|
|
||
| # also triggers assignment_linter | ||
| expect_lint("x = { 3 + 4 }", NULL, empty_assignment_linter()) | ||
| }) | ||
|
|
||
| test_that("empty_assignment_linter blocks disallowed usages", { | ||
| linter <- empty_assignment_linter() | ||
| lint_msg <- rex::rex("Assign NULL explicitly or, whenever possible, allocate the empty object") | ||
|
|
||
| expect_lint("xrep <- {}", lint_msg, linter) | ||
|
|
||
| # assignment with equal works as well, and white space doesn't matter | ||
| expect_lint("x = { }", lint_msg, linter) | ||
|
|
||
| # ditto right assignments | ||
| expect_lint("{} -> x", lint_msg, linter) | ||
| expect_lint("{} ->> x", lint_msg, linter) | ||
|
|
||
| # ditto data.table-style walrus assignments | ||
| expect_lint("x[, col := {}]", lint_msg, linter) | ||
|
|
||
| # newlines also don't matter | ||
| expect_lint("x <- {\n}", lint_msg, linter) | ||
|
|
||
| # LHS of assignment doesn't matter | ||
| expect_lint("env$obj <- {}", lint_msg, linter) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.