Skip to content

New function_return_linter() #1569

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

Merged
merged 6 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Collate:
'redundant_equals_linter.R'
'redundant_ifelse_linter.R'
'regex_subset_linter.R'
'return_assignment_linter.R'
'semicolon_linter.R'
'seq_linter.R'
'settings.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export(pipe_continuation_linter)
export(redundant_equals_linter)
export(redundant_ifelse_linter)
export(regex_subset_linter)
export(return_assignment_linter)
export(sarif_output)
export(semicolon_linter)
export(semicolon_terminator_linter)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

* `redundant_equals_linter()` for redundant comparisons to `TRUE` or `FALSE` like `is_treatment == TRUE` (#1500, @MichaelChirico)

* `return_assignment_linter()` for assignments within the `return()` clause of a function, e.g. `return(x <- foo())` (@MichaelChirico)

# lintr 3.0.1

* Skip multi-byte tests in non UTF-8 locales (#1504)
Expand Down
32 changes: 32 additions & 0 deletions R/return_assignment_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' Block assignments inside function returns
#'
#' `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("return_assignment_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
return_assignment_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"
)
})
}
1 change: 1 addition & 0 deletions inst/lintr/linters.csv
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pipe_continuation_linter,style readability default
redundant_equals_linter,best_practices readability efficiency common_mistakes
redundant_ifelse_linter,best_practices efficiency consistency
regex_subset_linter,best_practices efficiency
return_assignment_linter,readability best_practices
semicolon_linter,style readability default configurable
semicolon_terminator_linter,style readability deprecated configurable
seq_linter,robustness efficiency consistency best_practices default
Expand Down
1 change: 1 addition & 0 deletions man/best_practices_linters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/linters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/readability_linters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/return_assignment_linter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions tests/testthat/test-return_assignment_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
test_that("return_assignment_linter skips allowed usages", {
lines_simple <- trim_some("
foo <- function(x) {
x <- x + 1
return(x)
}
")
expect_lint(lines_simple, NULL, return_assignment_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, return_assignment_linter())
})

test_that("return_assignment_linter blocks simple disallowed usages", {
linter <- return_assignment_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
)
})