Skip to content
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

Merged
merged 6 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -94,6 +94,7 @@ Collate:
'fixed_regex_linter.R'
'function_argument_linter.R'
'function_left_parentheses_linter.R'
'function_return_linter.R'
'get_source_expressions.R'
'ids_with_token.R'
'ifelse_censor_linter.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export(extraction_operator_linter)
export(fixed_regex_linter)
export(function_argument_linter)
export(function_left_parentheses_linter)
export(function_return_linter)
export(get_r_string)
export(get_source_expressions)
export(ids_with_token)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
can be `purrr::map(x, quantile, 0.75, na.rm = TRUE)`. Naming `probs = 0.75` can further improve readability.
* `redundant_equals_linter()` for redundant comparisons to `TRUE` or `FALSE` like `is_treatment == TRUE` (#1500, @MichaelChirico)

* `function_return_linter()` for handling issues in function `return()` statements. Currently handles assignments within the `return()`
clause, 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/function_return_linter.R
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"
)
})
}
1 change: 1 addition & 0 deletions inst/lintr/linters.csv
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extraction_operator_linter,style best_practices
fixed_regex_linter,best_practices readability efficiency
function_argument_linter,style consistency best_practices
function_left_parentheses_linter,style readability default
function_return_linter,readability best_practices
ifelse_censor_linter,best_practices efficiency
implicit_integer_linter,style consistency best_practices
infix_spaces_linter,style readability 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.

19 changes: 19 additions & 0 deletions man/function_return_linter.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.

68 changes: 68 additions & 0 deletions tests/testthat/test-function_return_linter.R
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
)
Comment on lines +37 to +45
Copy link
Collaborator

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?

library(lintr)

lint(
  text = "foo <- function(x) {
        return(x + 1 ->> x)
      }
    ",
  linters = function_return_linter()
)
#> <text>:2:16: warning: [function_return_linter] Move the assignment outside of the return() clause, or skip assignment altogether.
#>         return(x + 1 ->> x)
#>                ^~~~~~~~~~~

Created on 2022-09-30 with reprex v2.0.2


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
)
})