{"record":{"id":"a78e837f5e0c2beb","repo":"oxc-project/oxc","slug":"unexpected-implicit-coercion-to-boolean","errorCode":null,"errorMessage":"Unexpected implicit coercion to boolean","messagePattern":"Unexpected implicit coercion to boolean","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_implicit_coercion.rs","lineNumber":17,"sourceCode":"use oxc_ast::{AstKind, ast::Expression};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse oxc_str::CompactStr;\nuse oxc_syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator};\nuse schemars::JsonSchema;\nuse serde::Deserialize;\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n};\n\nfn boolean_coercion_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected implicit coercion to boolean\")\n        .with_help(\"Use `Boolean(value)` instead\")\n        .with_label(span)\n}\n\nfn number_coercion_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected implicit coercion to number\")\n        .with_help(\"Use `Number(value)` instead\")\n        .with_label(span)\n}\n\nfn string_coercion_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected implicit coercion to string\")\n        .with_help(\"Use `String(value)` instead\")\n        .with_label(span)\n}\n\n/// Type of implicit coercion being detected\n#[derive(Clone, Copy)]","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_implicit_coercion.rs#L1-L35","documentation":"The boolean-kind diagnostic of oxlint's no-implicit-coercion rule (ESLint port). It fires on double negation !!value, the idiomatic-but-implicit way to force a boolean, and asks for the explicit Boolean(value) call so the conversion is visible. Rule options can re-allow the !! shorthand and toggle which coercion kinds are checked.","triggerScenarios":"const flag = !!items;; const ok = !!result; returned from a function; !!document.hidden passed to an API expecting a boolean.","commonSituations":"Enabling the rule on an existing codebase full of !! idioms; style disputes between !!x and Boolean(x); the allow list not yet configured when the rule is first switched on.","solutions":["Replace !!value with Boolean(value).","If the value is only used in a condition, drop the conversion entirely (if (value)).","Configure the rule's allow option to permit !! if that is the accepted team style.","Disable boolean checks via the rule's kind options."],"exampleFix":"// before\nconst hasItems = !!cart.length;\n\n// after\nconst hasItems = Boolean(cart.length);","handlingStrategy":"type-guard","validationCode":"const doubleBang = /(?:^|[^=!])!!(?![!=])/.test(source);","typeGuard":"const asBool = (v: unknown): boolean => (typeof v === 'boolean' ? v : Boolean(v));","tryCatchPattern":null,"preventionTips":["Standardize on Boolean(x) for explicit boolean casts.","Use typeof/instanceof narrowing instead of truthiness casts.","Agree on the rule's allow list in .oxlintrc.json before enabling it repo-wide."],"tags":["eslint","oxlint","lint","coercion","boolean"],"backgroundTag":"implicit-boolean-coercion","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}