Skip to content

๐Ÿ’… noUnnecessaryConditions: new RegExp(...).exec() is treated as non-nullableย #11278

Description

@shbernal

Environment information

Details
CLI:
  Version:                      2.5.7
  Color support:                false

Platform:
  CPU Architecture:             x86_64
  OS:                           windows

Environment:
  BIOME_DISTRIBUTION:                npm
  BIOME_LOG_PATH:                    unset
  BIOME_LOG_PREFIX_NAME:             unset
  BIOME_LOG_LEVEL:                   unset
  BIOME_LOG_KIND:                    unset
  BIOME_CONFIG_PATH:                 unset
  BIOME_THREADS:                     unset
  BIOME_WATCHER_KIND:                unset
  BIOME_WATCHER_POLLING_INTERVAL:    unset
  NO_COLOR:                     1
  TERM:                         xterm-256color
  JS_RUNTIME_VERSION:           v24.16.0
  JS_RUNTIME_NAME:              node
  NODE_PACKAGE_MANAGER:         unset

Biome Configuration:
  Status:                       Loaded successfully.
  Path:                         biome.json
  Formatter enabled:            true
  Linter enabled:               true
  Assist enabled:               true
  VCS enabled:                  false
  HTML full support enabled:    unset

Linter:
  JavaScript enabled:           unset
  JSON enabled:                 unset
  CSS enabled:                  unset
  GraphQL enabled:              unset
  Recommended:                  unset
  Enabled rules:
    suspicious/noUnnecessaryConditions

Workspace:
  Open Documents:               0

Rule name

suspicious/noUnnecessaryConditions

Playground link

https://biomejs.dev/playground/?lintRules=all&tab=diagnostics&code=LwAvACAAYABSAGUAZwBFAHgAcAAuAHAAcgBvAHQAbwB0AHkAcABlAC4AZQB4AGUAYwBgACAAcgBlAHQAdQByAG4AcwAgAGAAUgBlAGcARQB4AHAARQB4AGUAYwBBAHIAcgBhAHkAIAB8ACAAbgB1AGwAbABgACwAIABzAG8AIABgAD8ALgBgACAAaQBzACAAcgBlAHEAdQBpAHIAZQBkAC4ACgBlAHgAcABvAHIAdAAgAGYAdQBuAGMAdABpAG8AbgAgAGMAYQBwAHQAdQByAGUAKABpAG4AcAB1AHQAOgAgAHMAdAByAGkAbgBnACkAOgAgAHMAdAByAGkAbgBnACAAfAAgAHUAbgBkAGUAZgBpAG4AZQBkACAAewAKACAAIAByAGUAdAB1AHIAbgAgAC8AYQAoAGIAKQAvAC4AZQB4AGUAYwAoAGkAbgBwAHUAdAApAD8ALgBbADEAXQA7AAoAfQAKAAoAZQB4AHAAbwByAHQAIABmAHUAbgBjAHQAaQBvAG4AIABjAGEAcAB0AHUAcgBlAEQAeQBuAGEAbQBpAGMAKABpAG4AcAB1AHQAOgAgAHMAdAByAGkAbgBnACwAIAB0AGEAZwA6ACAAcwB0AHIAaQBuAGcAKQA6ACAAcwB0AHIAaQBuAGcAIAB7AAoAIAAgAHIAZQB0AHUAcgBuACAAbgBlAHcAIABSAGUAZwBFAHgAcAAoAGAAPAAkAHsAdABhAGcAfQA%2BACgAWwBeADwAXQAqACkAYAApAC4AZQB4AGUAYwAoAGkAbgBwAHUAdAApAD8ALgBbADEAXQAgAD8APwAgACcAJwA7AAoAfQAKAA%3D%3D

Expected result

No diagnostic. RegExp.prototype.exec returns RegExpExecArray | null, so the ?. is required however the RegExp was constructed.

Reproduction

// `RegExp.prototype.exec` returns `RegExpExecArray | null`, so `?.` is required.
export function capture(input: string): string | undefined {
  return /a(b)/.exec(input)?.[1];
}

export function captureDynamic(input: string, tag: string): string {
  return new RegExp(`<${tag}>([^<]*)`).exec(input)?.[1] ?? '';
}
b-regexp-exec.ts:7:10 lint/suspicious/noUnnecessaryConditions โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

  ร— Unnecessary optional chaining.

  > 7 โ”‚   return new RegExp(`<${tag}>([^<]*)`).exec(input)?.[1] ?? '';
      โ”‚          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  i Replace ?. with ..

  i The receiver is guaranteed to be non-nullish.

Found 1 error.

The suggested fix is unsafe: taking it turns a non-match from undefined into a TypeError at runtime.

Analysis

The two functions differ only in how the pattern is built, and only the second is flagged:

  • /a(b)/.exec(input)?.[1] โ€” not flagged, correct.
  • new RegExp(...).exec(input)?.[1] โ€” flagged.

So the regex literal carries a correctly-nullable exec while the RegExp constructor call does not โ€” it looks like the constructed-instance path loses the | null from the return type rather than the rule mishandling the chain.

Possibly a facet of #5977 (globals resolved from hardcoded definitions rather than real lib.d.ts), but the literal/constructor asymmetry suggests it may be fixable ahead of that work.

Context

Found while trialling the type-aware rules on a TypeScript-first OOXML/xlsx library. Dynamically built tag patterns are common in its test assertions, so this is 10 of the 148 noUnnecessaryConditions diagnostics on that tree.

Code of Conduct

  • I agree to follow Biome's Code of Conduct

Activity

  1. github-actions commented on Aug 8, 2026

    @github-actions
    Contributor

    โœ… Organic activity

    No automation signals detected in the analyzed events.

    View full analysis โ†’

    This is an automated analysis by AgentScan

  2. AlbinoGeek commented on Sep 19, 2026

    @AlbinoGeek
    Contributor

    This also reproduces on @biomejs/biome 2.5.14 (current npm release), and it gets worse:
    following the rule's own suggested fix breaks the typechecker, and the type-correct
    alternative to the fix gets a second, different false positive from the same rule.

    Repro (match here is a const holding a regex literal, not new RegExp(...)):

    const BEARER_HEADER = /^Bearer\s+(.+)$/i;
    
    export function bearerToken(header: string): string | null {
     const match = BEARER_HEADER.exec(header);
     return match?.[1] ?? null;
    }
    
    export function bearerTokenGuarded(header: string): string | null {
     const match = BEARER_HEADER.exec(header);
     if (!match) {
      return null;
     }
     return match[1] ?? null;
    }
    src/repro.ts:7:9 lint/suspicious/noUnnecessaryConditions
      ร— Unnecessary optional chaining.
        7 โ”‚  return match?.[1] ?? null;
      i Replace ?. with ..
      i The receiver is guaranteed to be non-nullish.
    
    src/repro.ts:14:6 lint/suspicious/noUnnecessaryConditions
      ร— This condition is always falsy.
       14 โ”‚  if (!match) {
      i The value's type can never be truthy, so this check is redundant.
    
    Found 2 errors.
    

    Applying the suggested fix (?.[1] -> [1]) to bearerToken:

    export function bearerToken(header: string): string | null {
     const match = BEARER_HEADER.exec(header);
     return match[1] ?? null;
    }

    biome lint now reports 0 errors on this function, but tsc fails:

    src/repro-fixed.ts(7,9): error TS18047: 'match' is possibly 'null'.
    

    So on this rule as it stands, none of the three forms satisfies both tools:

    1. match?.[1], the type-correct form, flagged as an unnecessary condition.
    2. match[1], Biome's own suggested fix for (1), passes Biome, fails tsc with TS18047.
    3. if (!match) return null;, the type-correct guard alternative to (1), flagged as
      always falsy.

    Versions: @biomejs/biome 2.5.14, typescript 7.0.2, Node 24.18.0, Linux x86_64.

    Full repro, both files and commands:
    https://github.com/AlbinoGeek/biome-regexp-exec-nullable-noUnnecessaryConditions

  3. added theissue type on Sep 19, 2026
  4. added
    S-Needs triage
    L-JavaScript
    S-Bug-confirmed
    on Sep 19, 2026
  5. removed
    S-Needs triage
    on Sep 19, 2026
  6. added
    A-Type-Inference
    on Sep 19, 2026
  7. self-assigned this
    on Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-Type-InferenceL-JavaScriptS-Bug-confirmed

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions