-
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.
* New empty_assignment_linter * hanging indent * cover right assignments as well; test for := too * Update R/empty_assignment_linter.R Co-authored-by: Indrajeet Patil <patilindrajeet.science@gmail.com>
- Loading branch information
1 parent
bda3739
commit 35f50e6
Showing
10 changed files
with
145 additions
and
2 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
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 and size.", | ||
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
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,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) | ||
}) |