Skip to content

Commit fdbb9bf

Browse files
following expr can be other expr-ish nodes (#2454)
1 parent 5b99e6e commit fdbb9bf

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
+ ignores calls on the RHS of operators like `lapply(l, function(x) "a" %in% names(x))` (#2310, @MichaelChirico).
7676
* `vector_logic_linter()` recognizes some cases where bitwise `&`/`|` are used correctly (#1453, @MichaelChirico).
7777
* `expect_comparison_linter()` ignores faulty usage like `expect_true(x, y > z)` (#2083, @MichaelChirico). Note that `y > z` is being passed to the `info=` argument, so this is likely a mistake.
78+
* `consecutive_assertion_linter()` ignores cases where a second asssertion follows assignment with `=` (#2444, @MichaelChirico).
7879

7980
### Lint accuracy fixes: removing false negatives
8081

R/consecutive_assertion_linter.R

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,23 @@
3131
#' @seealso [linters] for a complete list of linters available in lintr.
3232
#' @export
3333
consecutive_assertion_linter <- function() {
34-
stopifnot_xpath <- "
34+
# annoying expr-but-not-really nodes
35+
next_expr <- "following-sibling::*[self::expr or self::expr_or_assign_or_help or self::equal_assign][1]"
36+
37+
stopifnot_xpath <- glue("
3538
parent::expr
3639
/parent::expr[
37-
expr[1]/SYMBOL_FUNCTION_CALL = following-sibling::expr[1]/expr[1]/SYMBOL_FUNCTION_CALL
40+
expr[1]/SYMBOL_FUNCTION_CALL = {next_expr}/expr[1]/SYMBOL_FUNCTION_CALL
3841
]
39-
"
40-
assert_that_xpath <- "
42+
")
43+
assert_that_xpath <- glue("
4144
parent::expr
4245
/parent::expr[
4346
not(SYMBOL_SUB[text() = 'msg'])
4447
and not(following-sibling::expr[1]/SYMBOL_SUB[text() = 'msg'])
45-
and expr[1]/SYMBOL_FUNCTION_CALL = following-sibling::expr[1]/expr[1]/SYMBOL_FUNCTION_CALL
48+
and expr[1]/SYMBOL_FUNCTION_CALL = {next_expr}/expr[1]/SYMBOL_FUNCTION_CALL
4649
]
47-
"
50+
")
4851

4952
Linter(linter_level = "file", function(source_expression) {
5053
# need the full file to also catch usages at the top level

tests/testthat/test-consecutive_assertion_linter.R

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ test_that("Mixing test functions is fine", {
100100
)
101101
})
102102

103-
test_that("lints vectorie", {
103+
test_that("lints vectorize", {
104104
expect_lint(
105105
trim_some("{
106106
stopifnot(A)
@@ -127,3 +127,19 @@ test_that("old name consecutive_stopifnot_linter() is deprecated", {
127127
expect_lint("stopifnot(x); y; stopifnot(z)", NULL, old_linter)
128128
expect_lint("stopifnot(x); stopifnot(y)", "Unify consecutive calls", old_linter)
129129
})
130+
131+
test_that("interceding = assignments aren't linted", {
132+
expect_lint(
133+
trim_some("{
134+
stopifnot(A)
135+
x = 1
136+
stopifnot(B)
137+
138+
assert_that(C)
139+
z = 3
140+
assert_that(D)
141+
}"),
142+
NULL,
143+
consecutive_assertion_linter()
144+
)
145+
})

0 commit comments

Comments
 (0)