Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include rlang functions in literal coercion #1437

Merged
merged 19 commits into from
Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
* `modify_defaults()` no longer uses the mistaken `"lintr_function"` S3 class, instead applying the
`"linter"` class also common to `Linter()`. `Linter()` also includes `"function"` in the S3
class of its output to facilitate S3 dispatch to `function` methods where appropriate (#1392, @MichaelChirico).

## Changes to defaults

* `seq_linter()` additionally lints on `1:n()` (from dplyr)
and `1:.N` (from data.table) (#1396, @IndrajeetPatil).
* `seq_linter()` additionally lints on `1:n()` (from {dplyr})
and `1:.N` (from {data.table}) (#1396, @IndrajeetPatil).

* `literal_coercion_linter()` lints {rlang}'s atomic vector constructors if
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved
the argument is a scalar (#1437, @IndrajeetPatil).

* `redundant_ifelse_linter()`'s lint message correctly suggests negation when
the `yes` condition is `0` (#1432, @IndrajeetPatil).
Expand Down
44 changes: 31 additions & 13 deletions R/literal_coercion_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,41 @@
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
literal_coercion_linter <- function() {
coercers <- xp_text_in_table(paste0(
"as.",
c("logical", "integer", "numeric", "double", "character")
))
rlang_coercers <- xp_text_in_table(
c("lgl", "int", "dbl", "chr")
)
base_coercers <- xp_text_in_table(
paste0("as.", c("logical", "integer", "numeric", "double", "character"))
)
# notes for clarification:
# - as.integer(1e6) is arguably easier to read than 1000000L
# - in x$"abc", the "abc" STR_CONST is at the top level, so exclude OP-DOLLAR
# - need condition against STR_CONST w/ EQ_SUB to skip quoted keyword arguments (see tests)
# - for {rlang} coercers, both `int(1)` and `int(1, )` need to be linted
xpath <- glue::glue("//expr[
expr[1][SYMBOL_FUNCTION_CALL[ {coercers} ]]
and expr[2][
not(OP-DOLLAR)
and (
NUM_CONST[not(contains(translate(text(), 'E', 'e'), 'e'))]
or STR_CONST[not(following-sibling::*[1][self::EQ_SUB])]
)
]
(
expr[1][SYMBOL_FUNCTION_CALL[ {base_coercers} ]]
and expr[2][
not(OP-DOLLAR)
and (
NUM_CONST[not(contains(translate(text(), 'E', 'e'), 'e'))]
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
or STR_CONST[not(following-sibling::*[1][self::EQ_SUB])]
)
]
) or
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved
(
(
expr[1][SYMBOL_FUNCTION_CALL[ {rlang_coercers} ]]
and expr[2][
not(OP-DOLLAR)
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved
and (
NUM_CONST[not(contains(translate(text(), 'E', 'e'), 'e'))]
or STR_CONST[not(following-sibling::*[1][self::EQ_SUB])]
)]
)
and
count(expr) = 2
)
]")

Linter(function(source_expression) {
Expand All @@ -45,7 +63,7 @@ literal_coercion_linter <- function() {
source_expression = source_expression,
lint_message = paste(
"Use literals directly where possible, instead of coercion.",
"c.f. 1L instead of as.integer(1), or NA_real_ instead of as.numeric(NA)."
"c.f. 1L instead of as.integer(1) or rlang::int(1), or NA_real_ instead of as.numeric(NA)."
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
),
type = "warning"
)
Expand Down
64 changes: 52 additions & 12 deletions tests/testthat/test-literal_coercion_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ test_that("literal_coercion_linter skips allowed usages", {
expect_lint("as.numeric(1:3)", NULL, literal_coercion_linter())
})

test_that("literal_coercion_linter skips allowed rlang usages", {
expect_lint("int(1, 2.0, 3)", NULL, literal_coercion_linter())
expect_lint("chr('e', 'ab', 'xyz')", NULL, literal_coercion_linter())
expect_lint("lgl(0, 1)", NULL, literal_coercion_linter())
expect_lint("lgl(0L, 1)", NULL, literal_coercion_linter())
expect_lint("dbl(1.2, 1e5, 3L, 2E4)", NULL, literal_coercion_linter())
# make sure using namespace (`rlang::`) doesn't create problems
expect_lint("rlang::int(1, 2, 3)", NULL, literal_coercion_linter())
# even if scalar, carve out exceptions for the following
expect_lint("int(1.0e6)", NULL, literal_coercion_linter())
})

skip_if_not_installed("tibble")
skip_if_not_installed("patrick")
patrick::with_parameters_test_that(
Expand All @@ -29,22 +41,50 @@ patrick::with_parameters_test_that(
literal_coercion_linter()
),
.cases = tibble::tribble(
~.test_name, ~out_type, ~input,
"lgl, from int", "logical", "1L",
"lgl, from num", "logical", "1",
"lgl, from chr", "logical", '"true"',
"int, from num", "integer", "1",
"num, from num", "numeric", "1",
"dbl, from num", "double", "1",
"chr, from num", "character", "1",
~.test_name, ~out_type, ~input,
"lgl, from int", "logical", "1L",
"lgl, from num", "logical", "1",
"lgl, from chr", "logical", '"true"',
"int, from num", "integer", "1",
"num, from num", "numeric", "1",
"dbl, from num", "double", "1",
"chr, from num", "character", "1",
"chr, from chr", "character", '"e"',
"chr, from chr", "character", '"E"',
# affirmatively lint as.<type>(NA) should be NA_<type>_
"int, from NA", "integer", "NA",
"num, from NA", "numeric", "NA",
"dbl, from NA", "double", "NA",
"chr, from NA", "character", "NA",
"int, from NA", "integer", "NA",
"num, from NA", "numeric", "NA",
"dbl, from NA", "double", "NA",
"chr, from NA", "character", "NA",
)
)

patrick::with_parameters_test_that(
"literal_coercion_linter blocks rlang disallowed usages",
expect_lint(
sprintf("%s(%s)", out_type, input),
rex::rex("Use literals directly where possible, instead of coercion."),
literal_coercion_linter()
),
# even if `as.character(1)` works, `chr(1)` doesn't, so no corresponding test case
.cases = tibble::tribble(
~.test_name, ~out_type, ~input,
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved
"rlang::lgl", "lgl", "1L",
"rlang::int", "int", "1.0",
"rlang::dbl", "dbl", "1L",
"rlang::chr", "chr", '"e"',
"rlang::chr", "chr", '"E"',
)
)

test_that("literal_coercion_linter blocks scalar rlang list2 construction", {
expect_lint(
"int(1, )",
"Use literals directly where possible, instead of coercion.",
literal_coercion_linter()
)
})

test_that("literal_coercion_linter skips quoted keyword arguments", {
expect_lint("as.numeric(foo('a' = 1))", NULL, literal_coercion_linter())
})