File: stopifnot_all_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 (42 lines) | stat: -rw-r--r-- 1,127 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
#' Block usage of all() within stopifnot()
#'
#' `stopifnot(A)` actually checks `all(A)` "under the hood" if `A` is a vector,
#'   and produces a better error message than `stopifnot(all(A))` does.
#'
#' @examples
#' # will produce lints
#' lint(
#'   text = "stopifnot(all(x > 0))",
#'   linters = stopifnot_all_linter()
#' )
#'
#' lint(
#'   text = "stopifnot(y > 3, all(x < 0))",
#'   linters = stopifnot_all_linter()
#' )
#'
#' # okay
#' lint(
#'   text = "stopifnot(is.null(x) || all(x > 0))",
#'   linters = stopifnot_all_linter()
#' )
#'
#' lint(
#'   text = "assert_that(all(x > 0))",
#'   linters = stopifnot_all_linter()
#' )
#'
#' @evalRd rd_tags("stopifnot_all_linter")
#' @seealso [linters] for a complete list of linters available in lintr.
#' @export
stopifnot_all_linter <- make_linter_from_function_xpath(
  function_names = "stopifnot",
  xpath = "
  parent::expr
    /expr[expr/SYMBOL_FUNCTION_CALL[text() = 'all']]
  ",
  lint_message = paste(
    "Use stopifnot(x) instead of stopifnot(all(x)).",
    "stopifnot(x) runs all() 'under the hood' and provides a better error message in case of failure."
  )
)