Skip to content

Commit 79d1059

Browse files
Suppress coercion warnings (#2568)
1 parent d695796 commit 79d1059

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* `.lintr` config validation correctly accepts regular expressions which only compile under `perl = TRUE` (#2375, @MichaelChirico). These have always been valid (since `rex::re_matches()`, which powers the lint exclusion logic, also uses this setting), but the new up-front validation in v3.1.1 incorrectly used `perl = FALSE`.
2323
* `.lintr` configs set by option `lintr.linter_file` or environment variable `R_LINTR_LINTER_FILE` can point to subdirectories (#2512, @MichaelChirico).
2424
* `indentation_linter()` returns `ranges[1L]==1L` when the offending line has 0 spaces (#2550, @MichaelChirico).
25+
* `literal_coercion_linter()` doesn't surface a warning about NAs during coercion for code like `as.integer("a")` (#2566, @MichaelChirico).
2526

2627
## Changes to default linters
2728

R/literal_coercion_linter.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,14 @@ literal_coercion_linter <- function() {
9797
needs_prefix <- is_rlang_coercer & !startsWith(coercion_str, "rlang::")
9898
coercion_str[needs_prefix] <- paste0("rlang::", coercion_str[needs_prefix])
9999
}
100-
# the linter logic & rlang requirement should ensure that it's safe to run eval() here
100+
# the linter logic & rlang requirement should ensure that it's safe to run eval() here;
101+
# suppressWarnings() is for cases like 'as.integer("a")' which have an NA result, #2566.
101102
# TODO(#2473): Avoid a recommendation like '1' that clashes with implicit_integer_linter().
102-
literal_equivalent_str <- vapply(str2expression(coercion_str), function(expr) deparse1(eval(expr)), character(1L))
103+
literal_equivalent_str <- vapply(
104+
str2expression(coercion_str),
105+
function(expr) deparse1(suppressWarnings(eval(expr))),
106+
character(1L)
107+
)
103108
lint_message <- sprintf(
104109
"Use %s instead of %s, i.e., use literals directly where possible, instead of coercion.",
105110
literal_equivalent_str, report_str

tests/testthat/test-literal_coercion_linter.R

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
test_that("literal_coercion_linter skips allowed usages", {
2-
linter <- line_length_linter()
2+
linter <- literal_coercion_linter()
33

44
# naive xpath includes the "_f0" here as a literal
55
expect_lint('as.numeric(x$"_f0")', NULL, linter)
@@ -23,7 +23,7 @@ test_that("literal_coercion_linter skips allowed usages", {
2323
})
2424

2525
test_that("literal_coercion_linter skips allowed rlang usages", {
26-
linter <- line_length_linter()
26+
linter <- literal_coercion_linter()
2727

2828
expect_lint("int(1, 2.0, 3)", NULL, linter)
2929
expect_lint("chr('e', 'ab', 'xyz')", NULL, linter)
@@ -40,6 +40,18 @@ test_that("literal_coercion_linter skips quoted keyword arguments", {
4040
expect_lint("as.numeric(foo('a' = 1))", NULL, literal_coercion_linter())
4141
})
4242

43+
test_that("no warnings surfaced by running coercion", {
44+
linter <- literal_coercion_linter()
45+
46+
expect_no_warning(
47+
expect_lint("as.integer('a')", "Use NA_integer_", linter)
48+
)
49+
50+
expect_no_warning(
51+
expect_lint("as.integer(2147483648)", "Use NA_integer_", linter)
52+
)
53+
})
54+
4355
skip_if_not_installed("tibble")
4456
patrick::with_parameters_test_that(
4557
"literal_coercion_linter blocks simple disallowed usages",

0 commit comments

Comments
 (0)