Skip to content

Commit 2997038

Browse files
detect functional lambdas in object_usage_linter (#1934)
Co-authored-by: Indrajeet Patil <patilindrajeet.science@gmail.com>
1 parent 01500d8 commit 2997038

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
`R CMD check`, it defaults to `TRUE` (#941, #1458, @IndrajeetPatil).
4444
+ Handles backticked symbols inside {glue} expressions correctly, e.g. ``glue("{`x`}")`` correctly
4545
determines `x` was used (#1619, @MichaelChirico)
46+
+ Detects problems inside R4.1.0+ lambda functions (`\(...)`) (#1933, @MichaelChirico)
4647

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

R/object_usage_linter.R

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ object_usage_linter <- function(interpret_glue = TRUE, skip_with = TRUE) {
3737
# TODO(#1106): use //[...] to capture assignments in more scopes
3838
xpath_function_assignment <- paste(
3939
# direct assignments
40-
"expr[LEFT_ASSIGN or EQ_ASSIGN]/expr[2][FUNCTION]",
41-
"expr_or_assign_or_help[EQ_ASSIGN]/expr[2][FUNCTION]",
42-
"equal_assign[EQ_ASSIGN]/expr[2][FUNCTION]",
40+
"expr[LEFT_ASSIGN or EQ_ASSIGN]/expr[2][FUNCTION or OP-LAMBDA]",
41+
"expr_or_assign_or_help[EQ_ASSIGN]/expr[2][FUNCTION or OP-LAMBDA]",
42+
"equal_assign[EQ_ASSIGN]/expr[2][FUNCTION or OP-LAMBDA]",
4343
# assign() and setMethod() assignments
44-
"//SYMBOL_FUNCTION_CALL[text() = 'assign']/parent::expr/following-sibling::expr[2][FUNCTION]",
45-
"//SYMBOL_FUNCTION_CALL[text() = 'setMethod']/parent::expr/following-sibling::expr[3][FUNCTION]",
44+
"//SYMBOL_FUNCTION_CALL[text() = 'assign']/parent::expr/following-sibling::expr[2][FUNCTION or OP-LAMBDA]",
45+
"//SYMBOL_FUNCTION_CALL[text() = 'setMethod']/parent::expr/following-sibling::expr[3][FUNCTION or OP-LAMBDA]",
4646
sep = " | "
4747
)
4848

tests/testthat/test-object_usage_linter.R

+14
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,17 @@ test_that("messages without a quoted name are caught", {
638638
object_usage_linter()
639639
)
640640
})
641+
642+
test_that("functional lambda definitions are also caught", {
643+
skip_if_not_r_version("4.1.0")
644+
645+
expect_lint(
646+
trim_some("
647+
fun <- \\() {
648+
a <- 1
649+
}
650+
"),
651+
rex::rex("local variable", anything, "assigned but may not be used"),
652+
object_usage_linter()
653+
)
654+
})

0 commit comments

Comments
 (0)