6262# ' @export
6363unnecessary_nesting_linter <- function (allow_assignment = TRUE ) {
6464 exit_calls <- c(" stop" , " return" , " abort" , " quit" , " q" )
65- # These calls can be called in the sibling branch and not trigger a lint,
66- # allowing for cleanly parallel code, where breaking it would often harm readability:
67- # > if (A) {
68- # > stop()
69- # > } else {
70- # > warning()
71- # > }
72- # NB: print() is intentionally excluded since its usage is usually a mistake (?print_linter)
73- signal_calls <- c(
74- exit_calls ,
75- " warning" , " warn" , " message" , " cat" , " LOG" , " stopifnot"
76- )
7765 exit_call_expr <- glue("
78- expr[SYMBOL_FUNCTION_CALL[{xp_text_in_table(exit_calls)}]]
66+ expr[SYMBOL_FUNCTION_CALL[{xp_text_in_table(exit_calls)}]]
7967 " )
8068 # block IF here for cases where a nested if/else is entirely within
8169 # one of the branches.
8270 # TODO(michaelchirico): we could try and make the parallel exits requirement
8371 # more recursive, but it's a pain to do so.
84- no_signal_call_expr <- glue("
72+ no_exit_call_expr <- glue("
8573 expr[
8674 OP-LEFT-BRACE
8775 and expr[
8876 position() = last()
8977 and not(IF)
90- and not(expr[SYMBOL_FUNCTION_CALL[{xp_text_in_table(signal_calls )}]])
78+ and not(expr[SYMBOL_FUNCTION_CALL[{xp_text_in_table(exit_calls )}]])
9179 ]
9280 ]
9381 " )
@@ -107,8 +95,8 @@ unnecessary_nesting_linter <- function(allow_assignment = TRUE) {
10795 OP-LEFT-BRACE
10896 and expr[position() = last() and {exit_call_expr}]
10997 and (
110- following-sibling::{no_signal_call_expr }
111- or preceding-sibling::{no_signal_call_expr }
98+ following-sibling::{no_exit_call_expr }
99+ or preceding-sibling::{no_exit_call_expr }
112100 )
113101 ]
114102 ]
0 commit comments