{"record":{"id":"2ce10cbb8744d8a0","repo":"oxc-project/oxc","slug":"bad-bitwise-operator","errorCode":null,"errorMessage":"Bad bitwise operator","messagePattern":"Bad bitwise operator","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/oxc/bad_bitwise_operator.rs","lineNumber":17,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{BinaryExpression, Expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse oxc_syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator};\n\nuse crate::{AstNode, context::LintContext, fixer::RuleFixer, rule::Rule};\n\nfn bad_bitwise_operator_diagnostic(\n    bad_operator: &str,\n    suggestion: &str,\n    span: Span,\n) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Bad bitwise operator\")\n        .with_help(format!(\n            \"Bitwise operator '{bad_operator}' seems unintended. Did you mean logical operator '{suggestion}'?\"\n        ))\n        .with_label(span)\n}\n\nfn bad_bitwise_or_operator_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Bad bitwise operator\")\n        .with_help(\"Bitwise operator '|=' seems unintended. Did you mean logical operator '||='?\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct BadBitwiseOperator;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/oxc/bad_bitwise_operator.rs#L1-L35","documentation":"Diagnostic from oxlint rule oxc/bad-bitwise-operator (restriction category — restricted because of false positives on TypeScript enum bit flags, per the rule's own comment citing the vscode repo). It warns when a bitwise operator appears where the logical operator was almost certainly intended: '&' coerces operands to int32 and combines bits, while '&&' short-circuits on truthiness — for non-boolean operands the results differ. The help interpolates the exact pair: \"Bitwise operator '&' seems unintended. Did you mean logical operator '&&'?\"","triggerScenarios":"A binary expression using & or | (or the compound &= form) whose operand shape suggests boolean logic rather than bitmask assembly — e.g. if (options.verbose & options.debug), or value |= defaultValue. This generic diagnostic formats the found operator and its logical counterpart; the sibling function in the same file handles the '|=' → '||=' case with its own message.","commonSituations":"Single-vs-double character typos (the JS analogue of = vs ==); boolean flag combination logic; legit bit-flag code (permissions, feature masks, TS enums with 1 << n values) where the warning is a false positive and the reason the rule is opt-in-ish rather than correctness.","solutions":["If boolean logic was intended, switch to the logical operator: '&' → '&&', '|' → '||', '&=' → '&&='","If it is genuine bit manipulation, name the operands as flags/masks (const READ = 1 << 0) so intent is obvious to readers and reviewers","For files that legitimately use bitmasks throughout, disable the rule per-file or per-line: // oxlint-disable-next-line oxc/bad-bitwise-operator"],"exampleFix":"// before\nif (user.isActive & user.isAdmin) { /* meant logical AND */ }\n\n// after\nif (user.isActive && user.isAdmin) { }","handlingStrategy":"validation","validationCode":"// .oxlintrc.json — keep it on, allow exceptions per bitmask module\n\"rules\": { \"oxc/bad-bitwise-operator\": \"warn\" }\n// or disable where flags are real:\n\"overrides\": [{ \"files\": [\"src/flags/**\"], \"rules\": { \"oxc/bad-bitwise-operator\": \"off\" } }]","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When writing boolean conditions, pause on every single & or | — in app logic it is almost always a typo for &&/||","Keep genuine bit manipulation in dedicated modules with named flag constants so the pattern is reviewable","Add tests asserting flag outcomes; bitwise/logical mix-ups usually produce obviously wrong values that tests catch"],"tags":["oxc","operators","bitwise","logic","typo","oxlint"],"backgroundTag":"bitwise-instead-of-logical","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"}