Skip to content

Commit

Permalink
fix false positives for extractions (#2233)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Oct 8, 2023
1 parent b49ee9d commit 6e4dbec
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
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

0 comments on commit 6e4dbec

Please sign in to comment.