From e10e3a71a80bc6d6ae3375e8c73c1f379190a308 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sun, 29 Oct 2023 19:30:32 -0700 Subject: [PATCH] need relative, not global descendant --- R/unnecessary_lambda_linter.R | 2 +- tests/testthat/test-unnecessary_lambda_linter.R | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/R/unnecessary_lambda_linter.R b/R/unnecessary_lambda_linter.R index 647ed4224..d57617769 100644 --- a/R/unnecessary_lambda_linter.R +++ b/R/unnecessary_lambda_linter.R @@ -72,7 +72,7 @@ unnecessary_lambda_linter <- function() { count(.//SYMBOL[self::* = preceding::SYMBOL_FORMALS[1]]) = 1 and count(.//SYMBOL_FUNCTION_CALL[text() != 'return']) = 1 and preceding-sibling::SYMBOL_FORMALS = - //expr[ + .//expr[ position() = 2 and preceding-sibling::expr/SYMBOL_FUNCTION_CALL and not(preceding-sibling::*[1][self::EQ_SUB]) diff --git a/tests/testthat/test-unnecessary_lambda_linter.R b/tests/testthat/test-unnecessary_lambda_linter.R index 2c7e6cd59..5032f6fec 100644 --- a/tests/testthat/test-unnecessary_lambda_linter.R +++ b/tests/testthat/test-unnecessary_lambda_linter.R @@ -55,6 +55,16 @@ test_that("unnecessary_lambda_linter skips allowed usages", { 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) + + # Don't include other lambdas, #2249 + expect_lint( + trim_some('{ + lapply(x, function(e) sprintf("%o", e)) + lapply(y, function(e) paste(strlpad(e, "0", width))) + }'), + NULL, + linter + ) }) test_that("unnecessary_lambda_linter blocks simple disallowed usage", {