Skip to content

Commit 275ed7d

Browse files
Fix false positive for null coalescing without else clause (#2938)
* Fix false positive for null coalescing without else clause Signed-off-by: Emmanuel Ferdman <[email protected]> * cite in NEWS --------- Signed-off-by: Emmanuel Ferdman <[email protected]> Co-authored-by: Michael Chirico <[email protected]>
1 parent 2b70b52 commit 275ed7d

File tree

3 files changed

+5
-1
lines changed

3 files changed

+5
-1
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
* `download_file_linter()` encourages the use of `mode = "wb"` (or `mode = "ab"`) when using `download.file()`, rather than `mode = "w"` or `mode = "a"`, as the latter can produce broken
5757
files in Windows (#2882, @Bisaloo).
5858
* `lint2df_linter()` encourages the use of the `list2DF()` function, or the `data.frame()` function when recycling is required, over the slower and less readable `do.call(cbind.data.frame, )` alternative (#2834, @Bisaloo).
59-
* `coalesce_linter()` encourages the use of the infix operator `x %||% y`, which is equivalent to `if (is.null(x)) y else x` (#2246, @MichaelChirico). While this has long been used in many tidyverse packages (it was added to {ggplot2} in 2008), it became part of every R installation from R 4.4.0.
59+
* `coalesce_linter()` encourages the use of the infix operator `x %||% y`, which is equivalent to `if (is.null(x)) y else x` (#2246, @MichaelChirico). While this has long been used in many tidyverse packages (it was added to {ggplot2} in 2008), it became part of every R installation from R 4.4.0. Thanks also to @emmanuel-ferdman for fixing a false positive before release.
6060

6161
### Lint accuracy fixes: removing false positives
6262

R/coalesce_linter.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ coalesce_linter <- function() {
6060
parent::expr[
6161
preceding-sibling::OP-EXCLAMATION
6262
and parent::expr/preceding-sibling::IF
63+
and parent::expr/following-sibling::ELSE
6364
and (
6465
expr[2] = parent::expr/following-sibling::expr[1]
6566
or expr[2] = parent::expr/following-sibling::{braced_expr_cond}

tests/testthat/test-coalesce_linter.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ test_that("coalesce_linter skips allowed usage", {
33

44
expect_no_lint("if (is.null(x)) y", linter)
55
expect_no_lint("if (!is.null(x)) y", linter)
6+
expect_no_lint("if (!is.null(x)) x", linter)
7+
expect_no_lint("if (is.null(x)) x", linter)
8+
expect_no_lint("c(if (!is.null(E)) E)", linter)
69
expect_no_lint("if (is.null(x)) y else z", linter)
710
expect_no_lint("if (!is.null(x)) x[1] else y", linter)
811
expect_no_lint("if (is.null(x[1])) y else x[2]", linter)

0 commit comments

Comments
 (0)