{"record":{"id":"65d8a2b394cc4e6e","repo":"oxc-project/oxc","slug":"unexpected-constant-condition","errorCode":null,"errorMessage":"Unexpected constant condition","messagePattern":"Unexpected constant condition","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_constant_condition.rs","lineNumber":22,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{ArrowFunctionExpression, Expression, Function, YieldExpression},\n};\nuse oxc_ast_visit::VisitJs;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse oxc_syntax::{node::NodeId, scope::ScopeFlags};\n\nuse crate::{\n    AstNode,\n    ast_util::IsConstant,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n};\n\nfn no_constant_condition_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected constant condition\")\n        .with_help(\"Update the condition to not be constant, or remove the condition entirely\")\n        .with_label(span.label(\"this expression will always evaluate to the same value\"))\n}\n\n#[derive(Debug, Default, Clone, PartialEq, JsonSchema, Serialize)]\n#[serde(rename_all = \"camelCase\")]\nenum CheckLoops {\n    All,\n    #[default]\n    AllExceptWhileTrue,\n    None,\n}\n\nimpl<'de> Deserialize<'de> for CheckLoops {\n    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>\n    where\n        D: serde::Deserializer<'de>,\n    {","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_constant_condition.rs#L4-L40","documentation":"Diagnostic from the oxlint rule `no-constant-condition` (crates/oxc_linter/src/rules/eslint/no_constant_condition.rs). It fires when the test of an `if`, ternary, `while`, `do-while`, or `for` is a constant expression (literal, constant arithmetic, always-truthy object, `x ||= true`, etc.), labeled 'this expression will always evaluate to the same value'. Loop tests are governed by the `checkLoops` option: default `allExceptWhileTrue` keeps plain `while (true)` legal; `all`/`true` reports it; `none`/`false` skips loop tests entirely (with extra generator/yield-aware handling for loops that yield).","triggerScenarios":"`if (false) {...}`, `if (new Boolean(x)) {...}`, `x ? a : b` where x is a constant, `do {...} while (x = -1)`; plus `while (true)` only when checkLoops is `all`/`true`. Constants are computed with the shared IsConstant utility, so constant-foldable expressions and always-truthy boxed booleans count.","commonSituations":"Dead feature-flag branches left from `if (false)` debugging; placeholder conditions before wiring real inputs; infinite `while (true)` server loops tripping after someone sets `checkLoops: true`; ternaries over `typeof x === 'undefined'` styles that fold to constants.","solutions":["Replace the constant test with the real condition it was placeholder for.","If the branch is dead, delete it (or gate it behind a named constant read from config so it is no longer statically constant).","For intentional `while (true)` loops keep the default `checkLoops: \"allExceptWhileTrue\"`, or set it explicitly in .oxlintrc.json if your config was tightened to `all`.","For deliberate constant tests (spec tests, benchmarks), suppress with `// oxlint-disable-next-line no-constant-condition`."],"exampleFix":"// before\nif (false) {\n  doSomethingUnfinished();\n}\nwhile (true) { poll(); }\n\n// after\nif (!featureEnabled) {\n  doSomethingUnfinished();\n}\nfor (;;) { poll(); } // or keep checkLoops at \"allExceptWhileTrue\"","handlingStrategy":"validation","validationCode":"// Gate: constant tests in conditions (heuristic; rule handles folding)\nconst constantTest = /\\b(if|while)\\s*\\(\\s*(true|false|null|undefined|\\d+|['\"][^'\"]*['\"])\\s*\\)/.test(src);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Wire real conditions before committing placeholders like if (false).","Pin checkLoops to the value matching your loop style (default allExceptWhileTrue allows while(true)).","Prefer for(;;) when you want an unconditional loop regardless of config."],"tags":["lint","dead-code","condition","infinite-loop","oxlint"],"backgroundTag":"constant-condition","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}