File: test-empty_assignment_linter.R

package info (click to toggle)
r-cran-lintr 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,396 kB
  • sloc: sh: 13; xml: 10; makefile: 2
file content (46 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
test_that("empty_assignment_linter skips valid usage", {
  expect_lint("x <- { 3 + 4 }", NULL, empty_assignment_linter())
  expect_lint("x <- if (x > 1) { 3 + 4 }", NULL, empty_assignment_linter())

  # also triggers assignment_linter
  expect_lint("x = { 3 + 4 }", NULL, empty_assignment_linter())
})

test_that("empty_assignment_linter blocks disallowed usages", {
  linter <- empty_assignment_linter()
  lint_msg <- rex::rex("Assign NULL explicitly or, whenever possible, allocate the empty object")

  expect_lint("xrep <- {}", lint_msg, linter)

  # assignment with equal works as well, and white space doesn't matter
  expect_lint("x = {   }", lint_msg, linter)

  # ditto right assignments
  expect_lint("{} -> x", lint_msg, linter)
  expect_lint("{} ->> x", lint_msg, linter)

  # ditto data.table-style walrus assignments
  expect_lint("x[, col := {}]", lint_msg, linter)

  # newlines also don't matter
  expect_lint("x <- {\n}", lint_msg, linter)

  # LHS of assignment doesn't matter
  expect_lint("env$obj <- {}", lint_msg, linter)
})

test_that("lints vectorize", {
  lint_msg <- rex::rex("Assign NULL explicitly or, whenever possible, allocate the empty object")

  expect_lint(
    trim_some("{
      x <- {}
      y = {}
    }"),
    list(
      list(lint_msg, line_number = 2L),
      list(lint_msg, line_number = 3L)
    ),
    empty_assignment_linter()
  )
})