test_that("missing_argument_linter skips allowed usages", { linter <- missing_argument_linter() expect_lint("fun(x, a = 1)", NULL, linter) expect_lint("fun(x = 1, a = 1)", NULL, linter) expect_lint("dt[, 1]", NULL, linter) expect_lint("dt[, 'col']", NULL, linter) expect_lint("array[, , 1]", NULL, linter) expect_lint("switch(a =, b =, c = 1, 0)", NULL, linter) expect_lint("alist(a =, b =, c = 1, 0)", NULL, linter) expect_lint("pairlist(path = quote(expr = ))", NULL, linter) #1889 # always allow this missing usage expect_lint("foo()", NULL, linter) expect_lint("test(a =, b =, c = 1, 0)", NULL, missing_argument_linter("test")) }) test_that("missing_argument_linter blocks disallowed usages", { linter <- missing_argument_linter() lint_msg <- rex::rex("Missing argument in function call.") expect_lint("fun(, a = 1)", list(message = lint_msg), linter) expect_lint("f <- function(x, y) x\nf(, y = 1)\n", list(line = "f(, y = 1)"), linter) expect_lint("fun(a = 1,, b = 2)", list(message = lint_msg), linter) expect_lint("fun(a = 1, b =)", list(message = lint_msg), linter) expect_lint("fun(a = 1,)", list(message = lint_msg), linter) expect_lint("fun(a = )", list(message = lint_msg), linter) expect_lint( trim_some(" list( a = 1, b = 2, ) "), list(message = lint_msg), linter ) expect_lint("stats::median(1:10, na.rm =)", list(message = lint_msg), linter) expect_lint("env$get(1:10, default =)", list(message = lint_msg), linter) # except list can be empty expect_lint("switch(a =, b = 1, 0)", list(message = lint_msg), missing_argument_linter(character())) expect_lint("alist(a =)", list(message = lint_msg), missing_argument_linter(character())) # allow_trailing can allow trailing empty args also for non-excepted functions expect_lint("fun(a = 1,)", NULL, missing_argument_linter(allow_trailing = TRUE)) expect_lint( trim_some(" fun( a = 1, # comment ) "), NULL, missing_argument_linter(allow_trailing = TRUE) ) # ... but not if the final argument is named expect_lint( "fun(a = 1, b = )", list(message = lint_msg), missing_argument_linter(allow_trailing = TRUE) ) # Fixes https://github.com/r-lib/lintr/issues/906 # Comments should be ignored so that missing arguments could be # properly identified in these cases. expect_lint( trim_some(" fun( 1, 2, # comment ) "), list(message = lint_msg), linter ) expect_lint( trim_some(" fun( # comment , 1 ) "), list(message = lint_msg), linter ) expect_lint( trim_some(" fun( a = # comment , 1 ) "), list(message = lint_msg), linter ) }) test_that("lints vectorize", { linter <- missing_argument_linter() linter_trailing <- missing_argument_linter(allow_trailing = TRUE) lint_msg <- rex::rex("Missing argument in function call.") expect_lint( "foo(,,)", list( list(lint_msg, column_number = 5L), list(lint_msg, column_number = 6L), list(lint_msg, column_number = 7L) ), linter ) expect_lint( "foo(,,)", list( list(lint_msg, column_number = 5L), list(lint_msg, column_number = 6L) ), linter_trailing ) expect_lint( trim_some("{ foo(1,) bar(,2) }"), list( list(lint_msg, line_number = 2L), list(lint_msg, line_number = 3L) ), linter ) expect_lint( trim_some("{ foo(1,) bar(,2) }"), list(lint_msg, line_number = 3L), linter_trailing ) })