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

detect functional lambdas in object_usage_linter #1934

Merged
merged 2 commits into from
Apr 2, 2023
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 @@ -43,6 +43,7 @@
`R CMD check`, it defaults to `TRUE` (#941, #1458, @IndrajeetPatil).
+ Handles backticked symbols inside {glue} expressions correctly, e.g. ``glue("{`x`}")`` correctly
determines `x` was used (#1619, @MichaelChirico)
+ Detects problems inside R4.1.0+ lambda functions (`\(...)`) (#1933, @MichaelChirico)

* `spaces_inside_linter()` allows terminal missing keyword arguments (e.g. `alist(arg = )`; #540, @MichaelChirico)

Expand Down
10 changes: 5 additions & 5 deletions R/object_usage_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ object_usage_linter <- function(interpret_glue = TRUE, skip_with = TRUE) {
# TODO(#1106): use //[...] to capture assignments in more scopes
xpath_function_assignment <- paste(
# direct assignments
"expr[LEFT_ASSIGN or EQ_ASSIGN]/expr[2][FUNCTION]",
"expr_or_assign_or_help[EQ_ASSIGN]/expr[2][FUNCTION]",
"equal_assign[EQ_ASSIGN]/expr[2][FUNCTION]",
"expr[LEFT_ASSIGN or EQ_ASSIGN]/expr[2][FUNCTION or OP-LAMBDA]",
"expr_or_assign_or_help[EQ_ASSIGN]/expr[2][FUNCTION or OP-LAMBDA]",
"equal_assign[EQ_ASSIGN]/expr[2][FUNCTION or OP-LAMBDA]",
# assign() and setMethod() assignments
"//SYMBOL_FUNCTION_CALL[text() = 'assign']/parent::expr/following-sibling::expr[2][FUNCTION]",
"//SYMBOL_FUNCTION_CALL[text() = 'setMethod']/parent::expr/following-sibling::expr[3][FUNCTION]",
"//SYMBOL_FUNCTION_CALL[text() = 'assign']/parent::expr/following-sibling::expr[2][FUNCTION or OP-LAMBDA]",
"//SYMBOL_FUNCTION_CALL[text() = 'setMethod']/parent::expr/following-sibling::expr[3][FUNCTION or OP-LAMBDA]",
sep = " | "
)

Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-object_usage_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -638,3 +638,17 @@ test_that("messages without a quoted name are caught", {
object_usage_linter()
)
})

test_that("functional lambda definitions are also caught", {
skip_if_not_r_version("4.1.0")

expect_lint(
trim_some("
fun <- \\() {
a <- 1
}
"),
rex::rex("local variable", anything, "assigned but may not be used"),
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved
object_usage_linter()
)
})