Skip to content

Commit

Permalink
further fixes for new false positives in unnecessary_lambda_linter (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored Oct 29, 2023
1 parent 4a8b931 commit a6e20bb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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).
+ thanks to @Bisaloo and @strengejacke for detecting a regression in the original fix (#2231, #2247).

# lintr 3.1.0

Expand Down
2 changes: 1 addition & 1 deletion R/unnecessary_lambda_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +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)
and count(OP-LEFT-PAREN) + count(OP-LEFT-BRACE/following-sibling::expr/OP-LEFT-PAREN) = 1
]
/parent::expr
")
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-unnecessary_lambda_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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)
expect_lint("lapply(l, function(x) 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)
expect_lint("lapply(l, function(x) foo(x) ^ 3)", NULL, linter)
expect_lint("lapply(l, function(x) foo(x) %% 4)", NULL, linter)
})

test_that("unnecessary_lambda_linter blocks simple disallowed usage", {
Expand Down Expand Up @@ -170,6 +176,10 @@ test_that("cases with braces are caught", {
NULL,
linter
)

# false positives like #2231, #2247 are avoided with braces too
expect_lint("lapply(x, function(xi) { foo(xi)$bar })", NULL, linter)
expect_lint("lapply(x, function(xi) { foo(xi) - 1 })", NULL, linter)
})

test_that("function shorthand is handled", {
Expand Down

0 comments on commit a6e20bb

Please sign in to comment.