Skip to content

Fix false positive for null coalescing without else clause - #2938

Merged
MichaelChirico merged 2 commits into
r-lib:mainfrom
emmanuel-ferdman:main
Oct 7, 2025
Merged

MichaelChirico merged 2 commits into
r-lib:mainfrom
emmanuel-ferdman:main

Conversation

@emmanuel-ferdman

@emmanuel-ferdman emmanuel-ferdman commented Oct 4, 2025 •

Copy link
Copy Markdown
Collaborator

PR Summary

This PR fixes a bug where coalesce_linter() incorrectly warned for if statements without else clauses:

# Before: incorrectly warned
if (!is.null(x)) x
#> Use x %||% y instead of if (!is.null(x)) x else y.
#                                            ^^^^^^^^ (doesn't exist!)

# After: correctly ignores
if (!is.null(x)) x  # no warning

The XPath query had two branches for detecting coalesce patterns. The first branch (for is.null patterns) correctly checked for an ELSE node, but the second branch (for !is.null patterns) was missing this check, causing false positives. Added one line (and parent::expr/following-sibling::ELSE) to ensure both branches only match if-else statements, not plain if statements. Includes 3 regression tests.

Fixes #2936.

@codecov

codecov Bot commented Oct 4, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.24%. Comparing base (2b70b52) to head (0b5fca9).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2938   +/-   ##
=======================================
  Coverage   99.24%   99.24%           
=======================================
  Files         129      129           
  Lines        7282     7283    +1     
=======================================
+ Hits         7227     7228    +1     
  Misses         55       55           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@MichaelChirico

Copy link
Copy Markdown
Collaborator

Thanks! I added your name to the NEWS entry.

We might also consider extended tests of nested cases like if (A) b else if (!is.null(x)) x but I think it's OK here -- the clause you added is pretty straightforward.

@MichaelChirico
MichaelChirico merged commit 275ed7d into r-lib:main Oct 7, 2025
TimTaylor pushed a commit to TimTaylor/lintr that referenced this pull request Oct 29, 2025
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

coalesce_linter() throws in else-less statement

2 participants