From 1698e8fecc5e0020664cafdb4f69fef2c9ccf7fa Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sun, 8 Oct 2023 20:09:29 +0000 Subject: [PATCH] fix false positives for extractions --- NEWS.md | 1 + R/unnecessary_lambda_linter.R | 1 + tests/testthat/test-unnecessary_lambda_linter.R | 6 ++++++ 3 files changed, 8 insertions(+) diff --git a/NEWS.md b/NEWS.md index d75bf5f2d..9210f6829 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/unnecessary_lambda_linter.R b/R/unnecessary_lambda_linter.R index a09073c9a..003caee0d 100644 --- a/R/unnecessary_lambda_linter.R +++ b/R/unnecessary_lambda_linter.R @@ -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 ") diff --git a/tests/testthat/test-unnecessary_lambda_linter.R b/tests/testthat/test-unnecessary_lambda_linter.R index dc58a5c5d..078f8f937 100644 --- a/tests/testthat/test-unnecessary_lambda_linter.R +++ b/tests/testthat/test-unnecessary_lambda_linter.R @@ -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", {