Add bitclear analyzer to suggest &^= A instead of &= ^A#3150
Open
jakebailey wants to merge 2 commits intomainfrom
Open
Add bitclear analyzer to suggest &^= A instead of &= ^A#3150jakebailey wants to merge 2 commits intomainfrom
&^= A instead of &= ^A#3150jakebailey wants to merge 2 commits intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
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 &= ^yusages acrossinternal/withx &^= y. - Add a new
_tools/customlintanalyzer (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}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.