-
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
New function_return_linter()
#1569
Changes from all commits
fb0116f
8bb900f
fdc559b
1d368c2
396f42f
7641898
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,32 @@ | ||
#' Lint common mistakes/style issues cropping up from return statements | ||
#' | ||
#' `return(x <- ...)` is either distracting (because `x` is ignored), or | ||
#' confusing (because assigning to `x` has some side effect that is muddled | ||
#' by the dual-purpose expression). | ||
#' | ||
#' @evalRd rd_tags("function_return_linter") | ||
#' @seealso [linters] for a complete list of linters available in lintr. | ||
#' @export | ||
function_return_linter <- function() { | ||
xpath <- " | ||
//SYMBOL_FUNCTION_CALL[text() = 'return'] | ||
/parent::expr/parent::expr/expr[LEFT_ASSIGN or RIGHT_ASSIGN] | ||
" | ||
|
||
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 = "Move the assignment outside of the return() clause, or skip assignment altogether.", | ||
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,68 @@ | ||
test_that("function_return_linter skips allowed usages", { | ||
lines_simple <- trim_some(" | ||
foo <- function(x) { | ||
x <- x + 1 | ||
return(x) | ||
} | ||
") | ||
expect_lint(lines_simple, NULL, function_return_linter()) | ||
|
||
# arguably an expression as complicated as this should also be assigned, | ||
# but for now that's out of the scope of this linter | ||
lines_subassignment <- trim_some(" | ||
foo <- function(x) { | ||
return(x[, { | ||
col <- col + 1 | ||
.(grp, col) | ||
}]) | ||
} | ||
") | ||
expect_lint(lines_subassignment, NULL, function_return_linter()) | ||
}) | ||
|
||
test_that("function_return_linter blocks simple disallowed usages", { | ||
linter <- function_return_linter() | ||
lint_msg <- rex::rex("Move the assignment outside of the return() clause") | ||
|
||
expect_lint( | ||
trim_some(" | ||
foo <- function(x) { | ||
return(x <- x + 1) | ||
} | ||
"), | ||
lint_msg, | ||
linter | ||
) | ||
|
||
expect_lint( | ||
trim_some(" | ||
foo <- function(x) { | ||
return(x <<- x + 1) | ||
} | ||
"), | ||
lint_msg, | ||
linter | ||
) | ||
|
||
expect_lint( | ||
trim_some(" | ||
foo <- function(x) { | ||
return(x + 1 -> x) | ||
} | ||
"), | ||
lint_msg, | ||
linter | ||
) | ||
|
||
side_effect_lines <- | ||
expect_lint( | ||
trim_some(" | ||
e <- new.env() | ||
foo <- function(x) { | ||
return(e$val <- x + 1) | ||
} | ||
"), | ||
lint_msg, | ||
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.
Also add this one for the sake of completeness?
Created on 2022-09-30 with reprex v2.0.2