Skip to content

Commit d3b0e34

Browse files
authored
Support cases of stop(sprintf( in condition_call_linter (#2890)
1 parent 175065c commit d3b0e34

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* `Lint()`, and thus all linters, ensures that the returned object's `message` attribute is consistently a simple character string (and not, for example, an object of class `"glue"`; #2740, @MichaelChirico).
1919
* Files with encoding inferred from settings read more robustly under `lint(parse_settings = TRUE)` (#2803, @MichaelChirico).
2020
* `assignment_linter()` no longer errors if `"%<>%"` is an allowed operator (#2850, @AshesITR).
21+
* `condition_call_linter()` no longer covers cases where the object type in the ellipsis cannot be determined with certainty (#2888, #2890, @Bisaloo). In particular, this fixes the known false positive of custom conditions created via `errorCondition()` or `warningCondition()` not being compatible with the `call.` argument in `stop()` or `warning()`.
2122

2223
## Changes to default linters
2324

R/condition_call_linter.R

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,23 @@
5656
#' - <https://design.tidyverse.org/err-call.html>>
5757
#' @export
5858
condition_call_linter <- function(display_call = FALSE) {
59+
60+
str_fct <- c("sprintf", "gettextf", "paste", "paste0", "format", "tr_")
61+
str_literal_or_fct <- glue::glue("
62+
STR_CONST
63+
or expr/SYMBOL_FUNCTION_CALL[ { xp_text_in_table(str_fct) }]
64+
")
65+
5966
call_xpath <- glue::glue("
60-
following-sibling::expr/STR_CONST
67+
following-sibling::expr[ {str_literal_or_fct} ]
6168
and following-sibling::SYMBOL_SUB[text() = 'call.']
6269
/following-sibling::expr[1]
6370
/NUM_CONST[text() = '{!display_call}']
6471
")
65-
no_call_xpath <- "
66-
following-sibling::expr/STR_CONST
72+
no_call_xpath <- glue::glue("
73+
following-sibling::expr[ {str_literal_or_fct} ]
6774
and parent::expr[not(SYMBOL_SUB[text() = 'call.'])]
68-
"
75+
")
6976

7077
if (is.na(display_call)) {
7178
call_cond <- no_call_xpath

tests/testthat/test-condition_call_linter.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ patrick::with_parameters_test_that(
2828

2929
expect_lint(paste0(call_name, "('test')"), lint_message, linter)
3030
expect_lint(paste0(call_name, "('test', call. = TRUE)"), lint_message, linter)
31+
expect_lint(paste0(call_name, "(sprintf('test'))"), lint_message, linter)
32+
expect_lint(paste0(call_name, "(gettextf('test'))"), lint_message, linter)
3133

3234
linter <- condition_call_linter(display_call = TRUE)
3335
lint_message <- rex::rex(call_name, anything, "to display the call")

0 commit comments

Comments
 (0)