-
Notifications
You must be signed in to change notification settings - Fork 186
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
Add repeat_linter
for infinite loops
#2107
Changes from 17 commits
676a35e
6bc66af
296793e
22072a2
d112eec
3836d7f
dfc807e
5870bcc
17e15d5
1a7f510
3ec302f
1d6d1c4
1a42030
4fac026
f6915df
e8887e4
44a1f7d
5d95612
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 @@ | ||
#' Repeat linter | ||
#' | ||
#' Check that `while (TRUE)` is not used for infinite loops. | ||
#' | ||
#' @examples | ||
#' # will produce lints | ||
#' lint( | ||
#' text = "while (TRUE) { }", | ||
#' linters = repeat_linter() | ||
#' ) | ||
#' | ||
#' | ||
#' # okay | ||
#' lint( | ||
#' text = "repeat { }", | ||
#' linters = repeat_linter() | ||
#' ) | ||
#' | ||
#' | ||
#' @evalRd rd_tags("repeat_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
repeat_linter <- function() { | ||
xpath <- "//WHILE[following-sibling::expr[1]/NUM_CONST[text() = 'TRUE']]" | ||
|
||
Linter(function(source_expression) { | ||
if (!is_lint_level(source_expression, "expression")) { | ||
return(list()) | ||
} | ||
xml <- source_expression$xml_parsed_content | ||
lints <- xml_find_all(xml, xpath) | ||
|
||
xml_nodes_to_lints( | ||
lints, | ||
source_expression = source_expression, | ||
lint_message = "Use 'repeat' instead of 'while (TRUE)' for infinite loops.", | ||
range_start_xpath = "number(./@col1)", | ||
range_end_xpath = "number(./following-sibling::*[3]/@col2)" | ||
) | ||
}) | ||
} |
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,27 @@ | ||
test_that("test repeat_linter", { | ||
linter <- repeat_linter() | ||
msg <- rex::rex("Use 'repeat' instead of 'while (TRUE)' for infinite loops.") | ||
|
||
expect_lint("repeat { }", NULL, linter) | ||
expect_lint("while (FALSE) { }", NULL, linter) | ||
expect_lint("while (i < 5) { }", NULL, linter) | ||
expect_lint("while (j < 5) TRUE", NULL, linter) | ||
expect_lint("while (TRUE && j < 5) { ... }", NULL, linter) | ||
|
||
expect_lint("while (TRUE) { }", msg, linter) | ||
expect_lint("for (i in 1:10) { while (TRUE) { if (i == 5) { break } } }", msg, linter) | ||
expect_lint("while (TRUE) { while (TRUE) { } }", list(msg, msg), linter) | ||
expect_lint( | ||
trim_some("{ | ||
while (TRUE) { | ||
} | ||
while (TRUE) { | ||
} | ||
}"), | ||
list( | ||
list(message = msg, line_number = 2L, column_number = 1L, ranges = list(c(1L, 12L))), | ||
list(message = msg, line_number = 4L, column_number = 1L, ranges = list(c(1L, 12L))) | ||
), | ||
linter | ||
) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more suggested test cases:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also should test for lint locations, especially with multiline code and multiple lints in one expression.