Skip to content

Commit ec9f159

Browse files
mcolMichaelChirico
authored andcommitted
Don't lint T and F if followed by [ (r-lib#2947)
* Don't lint T and F if followed by `[`. * Address review comments. * subsume into same expr[] as $ and @ --------- Co-authored-by: Michael Chirico <[email protected]>
1 parent 1af371c commit ec9f159

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ files in Windows (#2882, @Bisaloo).
7474
* `assignment_linter()` with `operator = "="` does a better job of skipping implicit assignments, which are intended to be governed by `implicit_assignment_linter()` (#2765, @MichaelChirico).
7575
* `expect_true_false_linter()` is pipe-aware, so that `42 |> expect_identical(x, ignore_attr = TRUE)` no longer lints (#1520, @MichaelChirico).
7676
* `T_and_F_symbol_linter()` ignores `T` and `F` used as symbols in formulas (`y ~ T + F`), which can represent variables in data not controlled by the author (#2637, @MichaelChirico).
77+
* `T_and_F_symbol_linter()` ignores `T` and `F` if followed by `[` or `[[` (#2944, @mcol).
7778
* `implicit_assignment_linter()` with `allow_scoped=TRUE` doesn't lint for `if (a <- 1) print(a)` (#2913, @MichaelChirico).
7879

7980
### Lint accuracy fixes: removing false negatives

R/T_and_F_symbol_linter.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
T_and_F_symbol_linter <- function() { # nolint: object_name.
3535
symbol_xpath <- "//SYMBOL[
3636
(text() = 'T' or text() = 'F')
37-
and not(parent::expr[OP-DOLLAR or OP-AT])
37+
and not(parent::expr[OP-DOLLAR or OP-AT or following-sibling::OP-LEFT-BRACKET or following-sibling::LBB])
3838
and (
3939
not(ancestor::expr[OP-TILDE])
4040
or parent::expr/preceding-sibling::*[not(self::COMMENT)][1][self::EQ_SUB]

tests/testthat/test-T_and_F_symbol_linter.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ test_that("T_and_F_symbol_linter skips allowed usages", {
77
expect_no_lint("y ~ T + F", linter)
88
expect_no_lint("y ~ x + I(T^2)", linter)
99
expect_no_lint("y ~ foo(T, F)", linter)
10+
expect_no_lint("T[1]", linter)
11+
expect_no_lint("T[[1]]", linter)
1012
})
1113

1214
test_that("T_and_F_symbol_linter blocks disallowed usages", {
@@ -32,6 +34,8 @@ test_that("T_and_F_symbol_linter blocks disallowed usages", {
3234
expect_lint("DF$bool <- T", msg_true, linter)
3335
expect_lint("S4@bool <- T", msg_true, linter)
3436
expect_lint("sum(x, na.rm = T)", msg_true, linter)
37+
expect_lint("x[T]", msg_true, linter)
38+
expect_lint("x[, cols, drop = T]", msg_true, linter)
3539
expect_lint("y ~ foo(x, arg = T)", msg_true, linter)
3640
expect_lint(
3741
trim_some("

0 commit comments

Comments
 (0)