Skip to content

Commit 27890d5

Browse files
Add examples for unnecessary_lambda_linter() (#1888)
* Add examples for `unnecessary_lambda_linter()` * remove brace lints * Empty commit to trigger CI * more OK examples
1 parent 7209fd8 commit 27890d5

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

R/unnecessary_lambda_linter.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@
44
#' e.g. `lapply(DF, sum)` is the same as `lapply(DF, function(x) sum(x))` and
55
#' the former is more readable.
66
#'
7+
#' @examples
8+
#' # will produce lints
9+
#' lint(
10+
#' text = "lapply(list(1:3, 2:4), function(xi) sum(xi))",
11+
#' linters = unnecessary_lambda_linter()
12+
#' )
13+
#'
14+
#' # okay
15+
#' lint(
16+
#' text = "lapply(list(1:3, 2:4), sum)",
17+
#' linters = unnecessary_lambda_linter()
18+
#' )
19+
#'
20+
#' lint(
21+
#' text = 'lapply(x, function(xi) grep("ptn", xi))',
22+
#' linters = unnecessary_lambda_linter()
23+
#' )
24+
#'
25+
#' lint(
26+
#' text = "lapply(x, function(xi) data.frame(col = xi))",
27+
#' linters = unnecessary_lambda_linter()
28+
#' )
29+
#'
730
#' @evalRd rd_tags("unnecessary_lambda_linter")
831
#' @seealso [linters] for a complete list of linters available in lintr.
932
#' @export

man/unnecessary_lambda_linter.Rd

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-unnecessary_lambda_linter.R

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,36 +64,38 @@ test_that("unnecessary_lambda_linter doesn't apply to keyword args", {
6464
})
6565

6666
test_that("purrr-style anonymous functions are also caught", {
67+
linter <- unnecessary_lambda_linter()
68+
6769
# TODO(michaelchirico): this is just purrr::flatten(x). We should write another
6870
# linter to encourage that usage.
69-
expect_lint("purrr::map(x, ~.x)", NULL, unnecessary_lambda_linter())
70-
expect_lint("purrr::map_df(x, ~lm(y, .x))", NULL, unnecessary_lambda_linter())
71-
expect_lint("map_dbl(x, ~foo(bar = .x))", NULL, unnecessary_lambda_linter())
71+
expect_lint("purrr::map(x, ~.x)", NULL, linter)
72+
expect_lint("purrr::map_df(x, ~lm(y, .x))", NULL, linter)
73+
expect_lint("map_dbl(x, ~foo(bar = .x))", NULL, linter)
7274

7375
expect_lint(
7476
"purrr::map(x, ~foo(.x))",
7577
rex::rex("Pass foo directly as a symbol to map()"),
76-
unnecessary_lambda_linter()
78+
linter
7779
)
7880
expect_lint(
7981
"purrr::map_int(x, ~foo(.x, y))",
8082
rex::rex("Pass foo directly as a symbol to map_int()"),
81-
unnecessary_lambda_linter()
83+
linter
8284
)
8385
expect_lint(
8486
"purrr::map_vec(x, ~foo(.x, y))",
8587
rex::rex("Pass foo directly as a symbol to map_vec()"),
86-
unnecessary_lambda_linter()
88+
linter
8789
)
8890
})
8991

9092
test_that("cases with braces are caught", {
9193
linter <- unnecessary_lambda_linter()
92-
print_msg <- rex::rex("Pass print directly as a symbol to lapply()")
94+
lint_msg <- rex::rex("Pass print directly as a symbol to lapply()")
9395

9496
expect_lint(
9597
"lapply(x, function(xi) { print(xi) })",
96-
print_msg,
98+
lint_msg,
9799
linter
98100
)
99101

@@ -103,7 +105,7 @@ test_that("cases with braces are caught", {
103105
print(xi)
104106
})
105107
"),
106-
print_msg,
108+
lint_msg,
107109
linter
108110
)
109111

0 commit comments

Comments
 (0)