-
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.
Implement
implicit_assignment_linter()
Closes #1777
- Loading branch information
1 parent
a936cf6
commit 2d4d35c
Showing
11 changed files
with
193 additions
and
3 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,73 @@ | ||
#' Avoid implicit assignment in function calls | ||
#' | ||
#' @examples | ||
#' # will produce lints | ||
#' lint( | ||
#' text = "if (x <- 1L) TRUE", | ||
#' linters = implicit_assignment_linter() | ||
#' ) | ||
#' | ||
#' lint( | ||
#' text = "mean(x <- 1:4)", | ||
#' linters = implicit_assignment_linter() | ||
#' ) | ||
#' | ||
#' # okay | ||
#' writeLines("x <- 1L\nif (x) TRUE") | ||
#' lint( | ||
#' text = "x <- 1L\nif (x) TRUE", | ||
#' linters = implicit_assignment_linter() | ||
#' ) | ||
#' | ||
#' writeLines("x <- 1:4\nmean(x)") | ||
#' lint( | ||
#' text = "x <- 1:4\nmean(x)", | ||
#' linters = implicit_assignment_linter() | ||
#' ) | ||
#' | ||
#' @evalRd rd_tags("implicit_assignment_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
implicit_assignment_linter <- function() { | ||
assignments <- c( | ||
"LEFT_ASSIGN", # mean(x <- 1:4) | ||
"RIGHT_ASSIGN" # mean(1:4 -> x) | ||
) | ||
|
||
controls <- c( | ||
"//IF/following::expr/", # if (x <- 1L) { ... } | ||
"//WHILE/following::expr/" # while (x <- 0L) { ... } | ||
) | ||
xpath_controls <- paste0(rep(controls, each = length(assignments)), assignments, collapse = " | ") | ||
|
||
xpath_fun_call <- paste0( | ||
"//SYMBOL_FUNCTION_CALL/parent::expr/following::expr[1]/", | ||
assignments, | ||
collapse = " | " | ||
) | ||
|
||
xpath <- paste0(c(xpath_controls, xpath_fun_call), collapse = " | ") | ||
|
||
Linter(function(source_expression) { | ||
# need the full file to also catch usages at the top level | ||
if (!is_lint_level(source_expression, "file")) { | ||
return(list()) | ||
} | ||
|
||
xml <- source_expression$full_xml_parsed_content | ||
|
||
bad_expr <- xml2::xml_find_all(xml, xpath) | ||
|
||
lint_message <- paste( | ||
"Avoid implicit assignments in function calls.", | ||
"For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`." | ||
) | ||
|
||
xml_nodes_to_lints( | ||
bad_expr, | ||
source_expression = source_expression, | ||
lint_message = lint_message, | ||
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.
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,65 @@ | ||
test_that("implicit_assignment_linter skips allowed usages", { | ||
linter <- implicit_assignment_linter() | ||
|
||
expect_lint("x <- 1L", NULL, linter) | ||
expect_lint("1L -> x", NULL, linter) | ||
|
||
expect_lint("x <- mean(1:4)", NULL, linter) | ||
|
||
expect_lint( | ||
trim_some(" | ||
x <- 1:4 | ||
mean(x)"), | ||
NULL, | ||
linter | ||
) | ||
|
||
|
||
expect_lint( | ||
trim_some(" | ||
x <- 1L | ||
if (x) TRUE"), | ||
NULL, | ||
linter | ||
) | ||
|
||
expect_lint( | ||
trim_some(" | ||
0L -> abc | ||
while (abc) { | ||
FALSE | ||
}"), | ||
NULL, | ||
linter | ||
) | ||
|
||
expect_lint( | ||
trim_some(" | ||
foo <- function(x) { | ||
x <- x + 1 | ||
return(x) | ||
}"), | ||
NULL, | ||
linter | ||
) | ||
}) | ||
|
||
test_that("implicit_assignment_linter blocks disallowed usages", { | ||
lint_message <- rex::rex("Avoid implicit assignments in function calls.") | ||
linter <- implicit_assignment_linter() | ||
|
||
expect_lint("if (x <- 1L) TRUE", lint_message, linter) | ||
expect_lint("if (1L -> x) TRUE", lint_message, linter) | ||
expect_lint("while (x <- 0L) FALSE", lint_message, linter) | ||
expect_lint("while (0L -> x) FALSE", lint_message, linter) | ||
|
||
expect_lint("mean(x <- 1:4)", lint_message, linter) | ||
expect_lint( | ||
trim_some(" | ||
foo <- function(x) { | ||
return(x <- x + 1) | ||
}"), | ||
lint_message, | ||
linter | ||
) | ||
}) |