← Back to Pull requests

fix(checker): skip built-in guard narrowing when its callee is rebound #803

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 25, 2026 01:09 UTC
Author

 extract_type_narrowing(cond, scope)
+  if a built-in guard callee in cond (is.null, is.character, inherits,
+     length, missing, ...) may be rebound here      -> Narrowing::None
   built_in = extract_builtin_type_narrowing(cond)   # name-only, unchanged
   ...

 guard_callee_resolves_to_base(name, scope)          # new, resolve.rs
   resolves_to_base_lenient(name)
+  && no top-level binding of name (known_vars, incl. `name` <-)
+  && resolves_to_base_lenient("`name`")             # local quoted binding

 equality_guard_parameter / base_length (RY032)
-  resolves_to_base_lenient(name)
+  guard_callee_resolves_to_base(name)

Built-in guards narrowed by the bare callee name, so is.null <- function(x) FALSE still made !is.null(x) drop a NULL default, hiding the RY001 R raises. One gate at the top of extract_type_narrowing now walks the guard shapes of the condition (!, binary operators, call heads) and returns no narrowing when one of the built-in callees may be rebound. Condition aliases (has_x <- !is.null(x), #788) and the compound path go through the same function. Other callees, the project's own is.<class> predicates, and qualified heads are unchanged. The RY032 length(x) == 1L screen uses the same helper, so a backtick ` length mask is seen. Lenient resolution keeps guards working under library() and in packages, as in #372. A local data binding named missing or length keeps the guard, because R skips non-functions when it looks up a call head.

Evidence (CONTRIBUTING):

  • Mechanism: Checker::extract_type_narrowing (narrow.rs), guard_callee_resolves_to_base (resolve.rs), base_length (binop.rs).

  • Witnesses that change (now RY001): a project is.null redefinition, a local is.null <- inside the function, a top-level length <- function(x) 1L with x <- NULL; if (length(x) == 1). RY032 now fires in a package with the backtick length mask.

  • True positives that stay: the existing shadowed_length_and_dispatch_risk_still_warn_in_package_mode cases, null_default_guards.R RY001 cases.

  • Adjacent idioms that stay quiet: every existing guard case (library(ggplot2) + !is.null(min) && !is.null(max), the length guards), plus a new no-diag case with local missing <- "none"; length <- 0L.

  • Corpus: ecosystem/run.sh --check reports both the tidyverse and the Posit manifests current with the fixed release binary, so no ledger changes and no new diagnostics. In those corpora only roxygen2 rebinds one of these names (a local inherits <- function(type) in R/rd-inherit.R), and none of its findings move.

  • Perf: cargo test -p ry-checker --test perf --release -- --ignored --test-threads=1 (the CI perf gate) passes: 10/10, same slopes. The gate does hash lookups, plus one format! only for a built-in callee name.
  • Proof: the three new null_default_guards.R cases failed before the fix (expected Codes(["RY001"]), got {}) and pass after. The new quoted-length assertion in scalar_guard_packages matches a package repro that went from 0 to 1 RY032 with ry check. The issue's isnull.R went from 0 findings to 4:9 RY001. Under the same redefinition, a has_x <- !is.null(x); if (has_x) alias and an !is.null(a) && !is.null(b) guard each went from silent to one RY001. The new oracle fixture rebound_guard_predicate.R (must-warn RY001) has Rscript --vanilla confirm f() errors with argument is of length zero while f(1) returns. The pre-fix binary is silent on it and the fixed one warns. I ran locally: cargo test -p ry-checker --lib --test corpus --test scalar_guard_packages --test invariants --test metamorphic --test rule_evidence --test project passed. The full oracle matrix (--include-ignored) passed except the known local ops_plain_vector_fallback.R (R 4.5 structure(NULL) deprecation). cargo fmt --all -- --check is clean. I did not run workspace tests or clippy locally; CI covers them. Not covered: the scope-free collection pre-pass (missing in collect_parameter_uses_in_stmt, #773) and the stopifnot scalar-fact screens in scalar_assertion_subject, which still match raw spellings.

    Closes #802

    Claude Opus 5.5 on behalf of Max

    <!-- greptile_comment -->

    <!-- greptile_summary -->

    <h2><a href="https://app.greptile.com/api/retrigger?id=87502838"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img alt="Retrigger" src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2" align="right"></picture></a><a href="https://app.greptile.com/m0hawk/github/sims1253/ry/-/pull/803"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img alt="View in Greptile" src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2" align="right"></picture></a>Confidence Score: 4/5</h2>

    <!-- greptile-risk -->

    The PR appears safe to merge, with two non-blocking false-warning cases worth fixing.

    <h2>Findings</h2>

    1. <img alt="P2" src="https://greptile-static-assets.s3.amazonaws.com/badges/p2.svg?v=9" align="top">&nbsp;Data disables valid guards <a href="https://github.com/sims1253/ry/pull/803#discussion_r4369198108">▶</a>
    2. <img alt="P2" src="https://greptile-static-assets.s3.amazonaws.com/badges/p2.svg?v=9" align="top">&nbsp;Independent NULL facts disappear <a href="https://github.com/sims1253/ry/pull/803#discussion_r4369198112">▶</a>

    <h3>Summary</h3>

    The PR stops trusting built-in guards when a project or local function replaces their callee.

  • Rebound built-in guards no longer narrow values in unsafe branches.

  • RY032 stays visible when a quoted length could lie.
  • Acknowledged scope: sims1253 explicitly deferred scope-free missing collection and scalar_assertion_subject` in the PR description. sims1253 also explicitly left custom and qualified predicate heads unchanged.

    <!-- greptile_confidence_score:4 -->

    <sub>Reviews (1) · Last reviewed commit: "fix(checker): skip built-in guard narrow..." · Reviewed by Greptile</sub>

    <!-- /greptile_comment -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: github-actions[bot] Review duration: 9m
    All Vouch review confidence checks passed
    Residual risk 0.37 = Change Risk (0.52) × (1 − Review Confidence (0.28)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 58% · Reviewer familiarity: 35%
    28%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #803"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 28% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 28% · Lower = rushed or shallow review
    28%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 37.4% · Moderate risk
    37.4%
    Repository