Skip to content

Commit

Permalink
ensure no previous expression as well (#2320)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico committed Mar 21, 2024
1 parent 8c1522c commit ef79e0d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

### 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).
+ ignores calls on the RHS of operators like `lapply(l, function(x) "a" %in% names(x))` (#2310, @MichaelChirico).

# lintr 3.1.1

Expand Down
4 changes: 4 additions & 0 deletions R/unnecessary_lambda_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ 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[
preceding-sibling::expr[not(SYMBOL_FUNCTION_CALL)]
or 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
]
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-unnecessary_lambda_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ test_that("unnecessary_lambda_linter skips allowed usages", {
NULL,
linter
)

# only call is on RHS of operator, #2310
expect_lint("lapply(l, function(x) 'a' %in% names(x))", NULL, linter)
expect_lint("lapply(l, function(x = 1) 'a' %in% names(x))", NULL, linter)
})

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

0 comments on commit ef79e0d

Please sign in to comment.