oxc-project/oxc · warning
Unnecessary `else` after `return`.
Error message
Unnecessary `else` after `return`.
What it means
This diagnostic comes from the `no_else_return` rule in oxlint. It reports an `else` block that follows an `if` block whose last statement returns. The `else` adds nothing: without it, the code runs the same way. The rule option `allowElseIf` defaults to `true`, so an `else if` chain after a return stays allowed until you set it to `false`.
Source
Thrown at crates/oxc_linter/src/rules/eslint/no_else_return.rs:18
use schemars::JsonSchema;
use serde::Deserialize;
use oxc_ast::{AstKind, ast::Statement};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::ScopeId;
use oxc_span::{GetSpan, Span};
use oxc_syntax::line_terminator::is_line_terminator;
use crate::{
AstNode,
context::LintContext,
rule::{DefaultRuleConfig, Rule},
};
fn no_else_return_diagnostic(else_keyword: Span, last_return: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Unnecessary `else` after `return`.")
.with_labels([
last_return.label("This consequent block always returns,"),
else_keyword.primary_label("Making this `else` block unnecessary."),
])
.with_help("Remove the `else` block, moving its contents outside of the `if` statement.")
}
#[derive(Debug, Clone, JsonSchema, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct NoElseReturn {
/// Whether to allow `else if` blocks after a return statement.
///
/// Examples of **incorrect** code for this rule with `allowElseIf: false`:
/// ```javascript
/// function foo() {
/// if (error) {
/// return 'It failed';
/// } else if (loading) {View on GitHub (pinned to e1e7af627c)
Solutions
- Delete the `else` keyword and dedent its block.
- Set `"allowElseIf": false` in the rule config when `else if` after a return should also be flagged.
- Disable the rule for the file with an `oxlint-disable` comment when the current shape is the team style.
Example fix
// before
function check(x) {
if (x < 0) {
return 'neg';
} else {
log(x);
return 'pos';
}
}
// after
function check(x) {
if (x < 0) {
return 'neg';
}
log(x);
return 'pos';
} Defensive patterns
Strategy: validation
Prevention
- Use guard clauses: return early and keep the main path flat.
- Remove the `else` in the same edit that adds an early return.
- Fix the `allowElseIf` setting once, to match the team style.
When it happens
Trigger: An `if` consequent that ends with a `return`, followed by a plain `else` block: `if (ok) { return 1; } else { doWork(); }`. With `allowElseIf: false`, a following `else if` is also reported.
Common situations: A guard-clause refactor leaves the old `else` behind. Developers trained to always pair `if` with `else`. A team adopts oxlint and existing code lights up in CI.
Related errors
- Unexpected empty block statements
- Labeled statement is not allowed
- Label in break statement is not allowed
- Label in continue statement is not allowed
- `debugger` statement is not allowed
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/56b15e63c30a8943.
Report an issue: GitHub.