diff --git a/NAMESPACE b/NAMESPACE index 5caba4239..43a3eba8c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -142,6 +142,7 @@ export(with_id) export(xml_nodes_to_lints) export(yoda_test_linter) importFrom(cyclocomp,cyclocomp) +importFrom(glue,glue) importFrom(rex,character_class) importFrom(rex,re_matches) importFrom(rex,re_substitutes) diff --git a/NEWS.md b/NEWS.md index b60972245..63322d2f4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,7 +7,9 @@ ## 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). +* `object_usage_linter()`: + + assumes `glue()` is `glue::glue()` when `interpret_glue=TRUE` (#2032, @MichaelChirico). + + finds function usages inside `glue()` calls to avoid false positives for "unused objects" (#2029, @MichaelChirico). # lintr 3.1.0 diff --git a/R/lintr-package.R b/R/lintr-package.R index 7ee7a3115..cb46d5ad9 100644 --- a/R/lintr-package.R +++ b/R/lintr-package.R @@ -8,6 +8,7 @@ "_PACKAGE" ## lintr namespace: start +#' @importFrom glue glue #' @importFrom rex rex regex re_matches re_substitutes character_class #' @importFrom stats na.omit #' @importFrom utils capture.output head getParseData relist diff --git a/R/object_usage_linter.R b/R/object_usage_linter.R index 93341f73d..83949a879 100644 --- a/R/object_usage_linter.R +++ b/R/object_usage_linter.R @@ -170,7 +170,6 @@ extract_glued_symbols <- function(expr) { expr, xpath = paste0( "descendant::SYMBOL_FUNCTION_CALL[text() = 'glue']/", # a glue() call - "preceding-sibling::NS_GET/preceding-sibling::SYMBOL_PACKAGE[text() = 'glue']/", # qualified with glue:: "parent::expr[", # without .envir or .transform arguments "not(following-sibling::SYMBOL_SUB[text() = '.envir' or text() = '.transform']) and", diff --git a/tests/testthat/test-object_usage_linter.R b/tests/testthat/test-object_usage_linter.R index 9eea443eb..4f65b3174 100644 --- a/tests/testthat/test-object_usage_linter.R +++ b/tests/testthat/test-object_usage_linter.R @@ -352,6 +352,15 @@ test_that("interprets glue expressions", { } "), NULL, linter) + # no need for namespace-qualification + expect_lint(trim_some(" + glue <- glue::glue # imitate this being an @import + fun <- function() { + local_var <- 42 + glue('The answer is {local_var}.') + } + "), NULL, linter) + # multiple variables in different interpolations expect_lint(trim_some(" fun <- function() {