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

Fix false positives for extractions in lambdas #2233

Merged
merged 1 commit into from
Oct 8, 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 @@ -96,6 +96,7 @@
+ finds assignments in call arguments besides the first one (#2136, @MichaelChirico).
+ finds assignments in parenthetical expressions like `if (A && (B <- foo(A))) { }` (#2138, @MichaelChirico).
* `unnecessary_lambda_linter()` checks for cases using explicit returns, e.g. `lapply(x, \(xi) return(sum(xi)))` (#1567, @MichaelChirico).
+ thanks to @Bisaloo for detecting a regression in the original fix (#2231).

# lintr 3.1.0

Expand Down
1 change: 1 addition & 0 deletions R/unnecessary_lambda_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ unnecessary_lambda_linter <- function() {
and preceding-sibling::expr/SYMBOL_FUNCTION_CALL
and not(preceding-sibling::*[1][self::EQ_SUB])
]/SYMBOL
and not(OP-DOLLAR or OP-AT or OP-LEFT-BRACKET or LBB)
]
/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 @@ -43,6 +43,12 @@ test_that("unnecessary_lambda_linter skips allowed usages", {
# would require multiple lapply() loops
expect_lint("lapply(x, function(xi) foo(bar(xi)))", NULL, linter)
expect_lint("lapply(x, function(xi) return(foo(bar(xi))))", NULL, linter)

# extractions, #2231
expect_lint("lapply(l, function(x) rle(x)$values)", NULL, linter)
expect_lint('lapply(l, function(x) rle(x)["values"])', NULL, linter)
expect_lint('lapply(l, function(x) rle(x)[["values"]])', NULL, linter)
expect_lint("lapply(l, function(x) rle(x)@values)", NULL, linter)
})

test_that("unnecessary_lambda_linter blocks simple disallowed usage", {
Expand Down
Loading