Skip to content

Add bitclear analyzer to suggest &^= A instead of &= ^A#3150

Open
jakebailey wants to merge 2 commits intomainfrom
jabaile/and-not-analyzer
Open

Add bitclear analyzer to suggest &^= A instead of &= ^A#3150
jakebailey wants to merge 2 commits intomainfrom
jabaile/and-not-analyzer

Conversation

@jakebailey
Copy link
Member

Go has a bit clear operator, &^= A. We've used this nearly everywhere in place of &= ^A, but some stragglers remained.

Add an analyzer that finds these cases, then allows for easy --fix.

Copilot AI review requested due to automatic review settings March 18, 2026 19:10
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a custom Go lint analyzer to standardize bit-clearing patterns by suggesting &^= (bit clear) in place of &= ^, and updates remaining occurrences in the compiler code to use the idiomatic operator.

Changes:

  • Replace several x &= ^y usages across internal/ with x &^= y.
  • Add a new _tools/customlint analyzer (bitclear) that reports &= ^ patterns and provides an autofix.
  • Add golden test coverage for the new analyzer diagnostics.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
internal/transformers/tstransforms/runtimesyntax.go Convert modifier mask clearing to &^=.
internal/transformers/jsxtransforms/jsx.go Use &^= when clearing synthesized node flags.
internal/printer/printer.go Use &^= to clear multi-line/indent formatting flags.
internal/parser/parser.go Use &^= to clear parser context flags.
internal/checker/nodebuilderimpl.go Use &^= to clear nodebuilder context flags.
internal/checker/jsx.go Use &^= to clear enum flag from symbol flags.
internal/checker/checker.go Use &^= for JSX factory symbol flag adjustments.
internal/binder/binder.go Use &^= for clearing various node flags.
internal/ast/subtreefacts.go Use &^= for subtree facts bit clearing.
_tools/customlint/plugin.go Register the new bitclear analyzer in the plugin.
_tools/customlint/bitclear.go New analyzer to detect &= ^ and provide --fix edits.
_tools/customlint/testdata/bitclear/bitclear.go New test inputs for the analyzer.
_tools/customlint/testdata/bitclear/bitclear.go.golden Golden output validating reported diagnostics/ranges/messages.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +51 to +65
// 2. Remove `^` (and parens if present)
edits := []analysis.TextEdit{
{Pos: stmt.TokPos, End: stmt.TokPos + token.Pos(len("&=")), NewText: []byte("&^=")},
}

if paren, ok := unary.X.(*ast.ParenExpr); ok {
// Remove `^(` and trailing `)`
edits = append(edits,
analysis.TextEdit{Pos: unary.Pos(), End: paren.X.Pos(), NewText: nil},
analysis.TextEdit{Pos: paren.X.End(), End: paren.End(), NewText: nil},
)
} else {
// Remove `^`
edits = append(edits,
analysis.TextEdit{Pos: unary.Pos(), End: unary.X.Pos(), NewText: nil},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants