oxc-project/oxc · warning · OxcDiagnostic

This generator function does not have `yield`

Error message

This generator function does not have `yield`

What it means

oxlint `eslint/require-yield`: a generator function (`function*`) contains no `yield` expression, which per the rule's doc block makes the generator misleading — it produces values only from `return` and its body runs lazily. `require_yield_diagnostic` (require_yield.rs:14) suggests adding a `yield` or converting to a regular function. Nested generators are tracked separately via ScopeFlags, so a yield in an inner function does not count for the outer one.

Source

Thrown at crates/oxc_linter/src/rules/eslint/require_yield.rs:14

use oxc_ast::{
    AstKind,
    ast::{ArrowFunctionExpression, Function, YieldExpression},
};
use oxc_ast_visit::VisitJs;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_syntax::scope::ScopeFlags;

use crate::{AstNode, context::LintContext, rule::Rule};

fn require_yield_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("This generator function does not have `yield`")
        .with_help("Add a `yield` expression inside the generator body, or convert it to a regular function if iteration behavior is not needed.")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct RequireYield;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// This rule generates warnings for generator functions that do not have the yield keyword.
    ///
    /// ### Why is this bad?
    ///
    /// Probably a mistake.
    ///
    /// ### Examples
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Remove the `*` so it is a plain function if iteration is not needed.
  2. Add the intended `yield` (or `yield*` delegation) if the generator is incomplete.
  3. If the shape must stay (e.g. library contract requiring an iterator), suppress with `// oxlint-disable-next-line require-yield`.

Example fix

// before
function* loadItems() {
  return [];
}

// after
function loadItems() {
  return [];
}
Defensive patterns

Strategy: validation

Validate before calling

# generators with no yield (heuristic)
rg -n --pcre2 'function\s*\*[^{]*\{(?:(?!yield)[^}])*\}' src/

Prevention

When it happens

Trigger: `function* emptyGen() { console.log('hi'); }` — any Function or arrow-style generator whose own body has no YieldExpression. Includes generators delegating only via `yield*`? No: `yield*` is a yield expression and satisfies the rule; only truly yield-free generators are flagged.

Common situations: Saga-style functions refactored until all yields were removed; scaffolding for an iterator protocol where the generator marker was left behind; copy-paste of a generator shell.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/ceb9c39b04ab0a90. Report an issue: GitHub.