Skip to content

Commit

Permalink
Suppress coercion warnings (#2568)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico authored May 5, 2024
1 parent d695796 commit 79d1059
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* `.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`.
* `.lintr` configs set by option `lintr.linter_file` or environment variable `R_LINTR_LINTER_FILE` can point to subdirectories (#2512, @MichaelChirico).
* `indentation_linter()` returns `ranges[1L]==1L` when the offending line has 0 spaces (#2550, @MichaelChirico).
* `literal_coercion_linter()` doesn't surface a warning about NAs during coercion for code like `as.integer("a")` (#2566, @MichaelChirico).

## Changes to default linters

Expand Down
9 changes: 7 additions & 2 deletions R/literal_coercion_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,14 @@ literal_coercion_linter <- function() {
needs_prefix <- is_rlang_coercer & !startsWith(coercion_str, "rlang::")
coercion_str[needs_prefix] <- paste0("rlang::", coercion_str[needs_prefix])
}
# the linter logic & rlang requirement should ensure that it's safe to run eval() here
# the linter logic & rlang requirement should ensure that it's safe to run eval() here;
# suppressWarnings() is for cases like 'as.integer("a")' which have an NA result, #2566.
# TODO(#2473): Avoid a recommendation like '1' that clashes with implicit_integer_linter().
literal_equivalent_str <- vapply(str2expression(coercion_str), function(expr) deparse1(eval(expr)), character(1L))
literal_equivalent_str <- vapply(
str2expression(coercion_str),
function(expr) deparse1(suppressWarnings(eval(expr))),
character(1L)
)
lint_message <- sprintf(
"Use %s instead of %s, i.e., use literals directly where possible, instead of coercion.",
literal_equivalent_str, report_str
Expand Down
16 changes: 14 additions & 2 deletions tests/testthat/test-literal_coercion_linter.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("literal_coercion_linter skips allowed usages", {
linter <- line_length_linter()
linter <- literal_coercion_linter()

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

test_that("literal_coercion_linter skips allowed rlang usages", {
linter <- line_length_linter()
linter <- literal_coercion_linter()

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

test_that("no warnings surfaced by running coercion", {
linter <- literal_coercion_linter()

expect_no_warning(
expect_lint("as.integer('a')", "Use NA_integer_", linter)
)

expect_no_warning(
expect_lint("as.integer(2147483648)", "Use NA_integer_", linter)
)
})

skip_if_not_installed("tibble")
patrick::with_parameters_test_that(
"literal_coercion_linter blocks simple disallowed usages",
Expand Down

0 comments on commit 79d1059

Please sign in to comment.