From c0ac77246e13ebbf30cd1cb317495c76897a4fd6 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 2 Aug 2023 12:51:57 -0700 Subject: [PATCH] catch function calls in glue interpolation (#2033) * catch function calls in glue interpolation * extend test --- NEWS.md | 1 + R/object_usage_linter.R | 2 +- tests/testthat/test-object_usage_linter.R | 13 +++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 4cddc89bd..b60972245 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,6 +7,7 @@ ## Changes to defaults * `assignment_linter()` lints the {magrittr} assignment pipe `%<>%` (#2008, @MichaelChirico). This can be deactivated by setting the new argument `allow_pipe_assign` to `TRUE`. +* `object_usage_linter()` finds function usages inside `glue()` calls to avoid false positives for "unused objects" (#2029, @MichaelChirico). # lintr 3.1.0 diff --git a/R/object_usage_linter.R b/R/object_usage_linter.R index 856b5e77a..93341f73d 100644 --- a/R/object_usage_linter.R +++ b/R/object_usage_linter.R @@ -223,7 +223,7 @@ symbol_extractor <- function(text, envir, data) { parse_data <- utils::getParseData(parsed_text) # strip backticked symbols; `x` is the same as x. - symbols <- gsub("^`(.*)`$", "\\1", parse_data$text[parse_data$token == "SYMBOL"]) + symbols <- gsub("^`(.*)`$", "\\1", parse_data$text[parse_data$token %in% c("SYMBOL", "SYMBOL_FUNCTION_CALL")]) for (sym in symbols) { assign(sym, NULL, envir = envir) } diff --git a/tests/testthat/test-object_usage_linter.R b/tests/testthat/test-object_usage_linter.R index bb29ecf96..9eea443eb 100644 --- a/tests/testthat/test-object_usage_linter.R +++ b/tests/testthat/test-object_usage_linter.R @@ -410,6 +410,19 @@ test_that("interprets glue expressions", { glue::glue('The answer is {local_var}.') } "), "local_var", object_usage_linter(interpret_glue = FALSE)) + + # call in glue is caught + expect_lint( + trim_some(" + fun <- function() { + local_call <- identity + local_unused_call <- identity + glue::glue('{local_call(1)}') + } + "), + "local_unused_call", + linter + ) }) test_that("errors/edge cases in glue syntax don't fail lint()", {