Skip to content

Commit f07fbcc

Browse files
Error on empty character vector args in undesirable linters
Closes #1867
1 parent 0d1526a commit f07fbcc

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

R/undesirable_function_linter.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
undesirable_function_linter <- function(fun = default_undesirable_functions,
5959
symbol_is_undesirable = TRUE) {
6060
stopifnot(is.logical(symbol_is_undesirable))
61+
if (is.null(names(fun)) || !all(nzchar(names(fun))) || length(fun) == 0L) {
62+
stop("'fun' should be a non-empty named character vector; use missing elements to indicate default messages.")
63+
}
6164

6265
xp_condition <- xp_and(
6366
xp_text_in_table(names(fun)),

R/undesirable_operator_linter.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
#' @seealso [linters] for a complete list of linters available in lintr.
4444
#' @export
4545
undesirable_operator_linter <- function(op = default_undesirable_operators) {
46-
if (is.null(names(op)) || !all(nzchar(names(op)))) {
47-
stop("'op' should be a named character vector; use missing elements to indicate default messages.")
46+
if (is.null(names(op)) || !all(nzchar(names(op))) || length(op) == 0L) {
47+
stop("'op' should be a non-empty named character vector; use missing elements to indicate default messages.")
4848
}
4949
undesirable_operator_metadata <- merge(
5050
# infix must be handled individually below; non-assignment `=` are always OK

tests/testthat/test-undesirable_function_linter.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,30 @@ test_that("Line numbers are extracted correctly", {
5252
lines <- c(rep(letters, 10L), "tmp <- tempdir()")
5353
expect_lint(paste(lines, collapse = "\n"), "undesirable", undesirable_function_linter(c(tempdir = NA)))
5454
})
55+
56+
test_that("invalid inputs fail correctly", {
57+
error_msg <- "'fun' should be a non-empty named character vector"
58+
59+
expect_error(
60+
undesirable_function_linter("***"),
61+
error_msg,
62+
fixed = TRUE
63+
)
64+
expect_error(
65+
undesirable_function_linter(c("***" = NA, NA)),
66+
error_msg,
67+
fixed = TRUE
68+
)
69+
expect_error(
70+
undesirable_function_linter(fun = NULL),
71+
error_msg,
72+
fixed = TRUE
73+
)
74+
75+
expect_error(
76+
undesirable_function_linter(symbol_is_undesirable = 1.0),
77+
"is.logical(symbol_is_undesirable) is not TRUE",
78+
fixed = TRUE
79+
)
80+
})
81+

tests/testthat/test-undesirable_operator_linter.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,24 @@ test_that("undesirable_operator_linter vectorizes messages", {
5151
})
5252

5353
test_that("invalid inputs fail correctly", {
54+
error_msg <- "'op' should be a non-empty named character vector"
55+
5456
expect_error(
5557
undesirable_operator_linter("***"),
56-
"'op' should be a named character vector",
58+
error_msg,
5759
fixed = TRUE
5860
)
5961
expect_error(
6062
undesirable_operator_linter(c("***" = NA, NA)),
61-
"'op' should be a named character vector",
63+
error_msg,
6264
fixed = TRUE
6365
)
66+
expect_error(
67+
undesirable_operator_linter(op = NULL),
68+
error_msg,
69+
fixed = TRUE
70+
)
71+
6472
expect_error(
6573
undesirable_operator_linter(c("***" = NA)),
6674
"Did not recognize any valid operators in request for: ***",

0 commit comments

Comments
 (0)