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

warn if some NULLs are not in default #1049

Merged
merged 3 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* `T_and_F_symbol_linter` and `semicolon_terminator_linter` are now part of the default linters
(#517, #612, #683, #684, @AshesITR)
* `with_defaults()` no longer duplicates the `lintr_function` class when it is already present (#511, #612, @AshesITR)
* `with_defaults()` now warns if a named argument is `NULL` but its name is not in `default` (#1049, @AshesITR)
* New `backport_linter()` for detecting mismatched R version dependencies (#506, @MichaelChirico)
* `paren_brace_linter` and `no_tab_linter` also use more reliable matching (e.g.,
excluding matches found in comments; #441 and #545, @russHyde)
Expand Down
10 changes: 10 additions & 0 deletions R/with.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ with_defaults <- function(..., default = default_linters) {
)
}

to_null <- vapply(vals, is.null, logical(1L))
if (!all(nms[to_null] %in% names(default))) {
bad_nms <- setdiff(nms[to_null], names(default))
is_are <- if (length(bad_nms) > 1L) "are" else "is"
warning(
"Trying to remove ", glue::glue_collapse(sQuote(bad_nms), sep = ", ", last = " and "),
", which ", is_are, " not in `default`."
AshesITR marked this conversation as resolved.
Show resolved Hide resolved
)
}

is.na(vals) <- nms == vals
default[nms] <- vals

Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-with.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ test_that("with_defaults works as expected with unnamed args", {
expect_named(with_defaults(assignment_linter), names(with_defaults()))
})

test_that("with_defaults warns on unused NULLs", {
expect_warning(with_defaults(not_a_default = NULL), rex::rex("which is not in `default`."))
expect_warning(with_defaults(not_a_default = NULL, also_not_default = NULL), rex::rex("which are not in `default`."))
})

test_that("all default linters are tagged default", {
expect_named(with_defaults(), available_linters(tags = "default")$linter)

Expand Down