From 12b596a7911c399f0f5672c4a89f9f965fd4802a Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Tue, 25 Jul 2023 07:46:57 -0700 Subject: [PATCH] correct message among several lints --- R/assignment_linter.R | 2 +- tests/testthat/test-assignment_linter.R | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/R/assignment_linter.R b/R/assignment_linter.R index 0a567b785..4d5919efa 100644 --- a/R/assignment_linter.R +++ b/R/assignment_linter.R @@ -112,7 +112,7 @@ assignment_linter <- function(allow_cascading_assign = TRUE, } operator <- xml2::xml_text(bad_expr) - lint_message_fmt <- "Use <-, not %s, for assignment." + lint_message_fmt <- rep("Use <-, not %s, for assignment.", length(operator)) lint_message_fmt[operator %in% c("<<-", "->>")] <- "%s can have hard-to-predict behavior; prefer assigning to a specific environment instead (with assign() or <-)." lint_message_fmt[operator == "%<>%"] <- diff --git a/tests/testthat/test-assignment_linter.R b/tests/testthat/test-assignment_linter.R index 79686d7f2..52e132b1f 100644 --- a/tests/testthat/test-assignment_linter.R +++ b/tests/testthat/test-assignment_linter.R @@ -171,3 +171,16 @@ test_that("%<>% throws a lint", { # interaction with allow_trailing expect_lint("x %<>%\n sum()", "Assignment %<>% should not be trailing", assignment_linter(allow_trailing = FALSE)) }) + +test_that("multiple lints throw correct messages", { + expect_lint( + "{ x <<- 1; y ->> 2; z -> 3; x %<>% as.character() }", + list( + list(message = "<<- can have hard-to-predict behavior"), + list(message = "->> can have hard-to-predict behavior"), + list(message = "Use <-, not ->"), + list(message = "Avoid the assignment pipe %<>%") + ), + assignment_linter(allow_cascading_assign = FALSE) + ) +})