Skip to content

Commit

Permalink
warn if some NULLs are not in default (#1049)
Browse files Browse the repository at this point in the history
* warn if some NULLs are not in default

* add PR number to NEWS

* commas_linter

Co-authored-by: Michael Chirico <[email protected]>
  • Loading branch information
AshesITR and MichaelChirico authored Apr 4, 2022
1 parent 9af16af commit 05d5bd4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
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`."
)
}

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

0 comments on commit 05d5bd4

Please sign in to comment.