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

Improve handling of operators inside lambdas #2278

Merged
merged 3 commits into from
Nov 13, 2023
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 NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
### Lint accuracy fixes: removing false positives

* `unreachable_code_linter()` ignores reachable code in inline functions like `function(x) if (x > 2) stop() else x` (#2259, @MEO265).
* `unnecessary_lambda_linter()` ignores extractions with explicit returns like `lapply(l, function(x) foo(x)$bar)` (#2258, @MichaelChirico).

# lintr 3.1.1

Expand Down
3 changes: 1 addition & 2 deletions R/unnecessary_lambda_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ unnecessary_lambda_linter <- function() {
# c. that call's _first_ argument is just the function argument (a SYMBOL)
# - and it has to be passed positionally (not as a keyword)
# d. the function argument doesn't appear elsewhere in the call
# TODO(#1703): handle explicit returns too: function(x) return(x)
default_fun_xpath <- glue("
//SYMBOL_FUNCTION_CALL[ {apply_funs} ]
/parent::expr
Expand All @@ -76,8 +75,8 @@ unnecessary_lambda_linter <- function() {
position() = 2
and preceding-sibling::expr/SYMBOL_FUNCTION_CALL
and not(preceding-sibling::*[1][self::EQ_SUB])
and not(parent::expr/following-sibling::*[not(self::OP-RIGHT-PAREN or self::OP-RIGHT-BRACE)])
]/SYMBOL
and count(OP-LEFT-PAREN) + count(OP-LEFT-BRACE/following-sibling::expr/OP-LEFT-PAREN) = 1
]
/parent::expr
")
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-unnecessary_lambda_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ test_that("unnecessary_lambda_linter skips allowed usages", {
expect_lint('lapply(l, function(x) rle(x)[["values"]])', NULL, linter)
expect_lint("lapply(l, function(x) rle(x)@values)", NULL, linter)

# return() extractions, #2258
expect_lint("lapply(l, function(x) return(foo(x)$bar))", NULL, linter)
expect_lint('lapply(l, function(x) return(rle(x)["values"]))', NULL, linter)
expect_lint('lapply(l, function(x) return(rle(x)[["values"]]))', NULL, linter)
expect_lint("lapply(l, function(x) return(rle(x)@values))", NULL, linter)

# Other operators, #2247
expect_lint("lapply(l, function(x) foo(x) - 1)", NULL, linter)
expect_lint("lapply(l, function(x) foo(x) * 2)", NULL, linter)
Expand Down
Loading