oxc-project/oxc · warning · OxcDiagnostic

Bad bitwise operator

Error message

Bad bitwise operator

What it means

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 '&&'?"

Source

Thrown at crates/oxc_linter/src/rules/oxc/bad_bitwise_operator.rs:17

use oxc_ast::{
    AstKind,
    ast::{BinaryExpression, Expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator};

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

fn bad_bitwise_operator_diagnostic(
    bad_operator: &str,
    suggestion: &str,
    span: Span,
) -> OxcDiagnostic {
    OxcDiagnostic::warn("Bad bitwise operator")
        .with_help(format!(
            "Bitwise operator '{bad_operator}' seems unintended. Did you mean logical operator '{suggestion}'?"
        ))
        .with_label(span)
}

fn bad_bitwise_or_operator_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Bad bitwise operator")
        .with_help("Bitwise operator '|=' seems unintended. Did you mean logical operator '||='?")
        .with_label(span)
}

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

declare_oxc_lint!(
    /// ### What it does
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. If boolean logic was intended, switch to the logical operator: '&' → '&&', '|' → '||', '&=' → '&&='
  2. If it is genuine bit manipulation, name the operands as flags/masks (const READ = 1 << 0) so intent is obvious to readers and reviewers
  3. For files that legitimately use bitmasks throughout, disable the rule per-file or per-line: // oxlint-disable-next-line oxc/bad-bitwise-operator

Example fix

// before
if (user.isActive & user.isAdmin) { /* meant logical AND */ }

// after
if (user.isActive && user.isAdmin) { }
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json — keep it on, allow exceptions per bitmask module
"rules": { "oxc/bad-bitwise-operator": "warn" }
// or disable where flags are real:
"overrides": [{ "files": ["src/flags/**"], "rules": { "oxc/bad-bitwise-operator": "off" } }]

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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