-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Identical Expression Comparison analyzer #7066
Merged
prylabs-bulldozer
merged 11 commits into
master
from
identical-expression-comparison-analyzer
Aug 21, 2020
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1630c8e
static analyzer with tests
rkapka d2be935
Merge branch 'origin-master' into identical-expression-comparison-ana…
rkapka 34dce13
resolve analyzer errors
rkapka 54685ce
Merge branch 'origin-master' into identical-expression-comparison-ana…
rkapka 71adcdf
change excluded file in nogo_config
rkapka 2fd3f0d
remove tests from bazel file
rkapka 7e7b1c5
exclude test file explicitly
rkapka e9ccea7
extracted common code to helper file
rkapka 505606a
Revert "extracted common code to helper file"
rkapka 0a485f7
Merge refs/heads/master into identical-expression-comparison-analyzer
prylabs-bulldozer[bot] 6768aad
Merge refs/heads/master into identical-expression-comparison-analyzer
prylabs-bulldozer[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
load("@prysm//tools/go:def.bzl", "go_library") | ||
load("@io_bazel_rules_go//go:def.bzl", "go_tool_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["analyzer.go"], | ||
importpath = "github.com/prysmaticlabs/prysm/tools/analyzers/comparesame", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@org_golang_x_tools//go/analysis:go_default_library", | ||
"@org_golang_x_tools//go/analysis/passes/inspect:go_default_library", | ||
"@org_golang_x_tools//go/ast/inspector:go_default_library", | ||
], | ||
) | ||
|
||
go_tool_library( | ||
name = "go_tool_library", | ||
srcs = ["analyzer.go"], | ||
importpath = "github.com/prysmaticlabs/prysm/tools/analyzers/comparesame", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@org_golang_x_tools//go/analysis:go_tool_library", | ||
"@org_golang_x_tools//go/analysis/passes/inspect:go_tool_library", | ||
"@org_golang_x_tools//go/ast/inspector:go_tool_library", | ||
], | ||
) | ||
|
||
# gazelle:exclude analyzer_test.go | ||
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package comparesame | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"go/ast" | ||
"go/printer" | ||
"go/token" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/passes/inspect" | ||
"golang.org/x/tools/go/ast/inspector" | ||
) | ||
|
||
// Doc explaining the tool. | ||
const Doc = "Tool to detect comparison (==, !=, >=, <=, >, <) of identical boolean expressions." | ||
|
||
const messageTemplate = "Boolean expression has identical expressions on both sides. The result is always %v." | ||
|
||
// Analyzer runs static analysis. | ||
var Analyzer = &analysis.Analyzer{ | ||
Name: "comparesame", | ||
Doc: Doc, | ||
Requires: []*analysis.Analyzer{inspect.Analyzer}, | ||
Run: run, | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) | ||
if !ok { | ||
return nil, errors.New("analyzer is not type *inspector.Inspector") | ||
} | ||
|
||
nodeFilter := []ast.Node{ | ||
(*ast.BinaryExpr)(nil), | ||
} | ||
|
||
inspect.Preorder(nodeFilter, func(node ast.Node) { | ||
expr, ok := node.(*ast.BinaryExpr) | ||
if !ok { | ||
return | ||
} | ||
|
||
switch expr.Op { | ||
case token.EQL, token.NEQ, token.GEQ, token.LEQ, token.GTR, token.LSS: | ||
var xBuf, yBuf bytes.Buffer | ||
if err := printer.Fprint(&xBuf, pass.Fset, expr.X); err != nil { | ||
pass.Reportf(expr.X.Pos(), err.Error()) | ||
} | ||
if err := printer.Fprint(&yBuf, pass.Fset, expr.Y); err != nil { | ||
pass.Reportf(expr.Y.Pos(), err.Error()) | ||
} | ||
if xBuf.String() == yBuf.String() { | ||
switch expr.Op { | ||
case token.EQL, token.NEQ, token.GEQ, token.LEQ: | ||
pass.Reportf(expr.OpPos, messageTemplate, true) | ||
case token.GTR, token.LSS: | ||
pass.Reportf(expr.OpPos, messageTemplate, false) | ||
} | ||
} | ||
} | ||
}) | ||
|
||
return nil, nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package comparesame | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func TestAnalyzer(t *testing.T) { | ||
analysistest.Run(t, analysistest.TestData(), Analyzer) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
load("@prysm//tools/go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["compare_len.go"], | ||
importpath = "github.com/prysmaticlabs/prysm/tools/analyzers/comparesame/testdata", | ||
visibility = ["//visibility:public"], | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package testdata | ||
|
||
func Equal() { | ||
x := []string{"a"} | ||
if len(x) == len(x) { // want "Boolean expression has identical expressions on both sides. The result is always true." | ||
} | ||
} | ||
|
||
func NotEqual() { | ||
x := []string{"a"} | ||
if len(x) != len(x) { // want "Boolean expression has identical expressions on both sides. The result is always true." | ||
} | ||
} | ||
|
||
func GreaterThanOrEqual() { | ||
x := []string{"a"} | ||
if len(x) >= len(x) { // want "Boolean expression has identical expressions on both sides. The result is always true." | ||
} | ||
} | ||
|
||
func LessThanOrEqual() { | ||
x := []string{"a"} | ||
if len(x) <= len(x) { // want "Boolean expression has identical expressions on both sides. The result is always true." | ||
} | ||
} | ||
|
||
func GreaterThan() { | ||
x := []string{"a"} | ||
if len(x) > len(x) { // want "Boolean expression has identical expressions on both sides. The result is always false." | ||
} | ||
} | ||
|
||
func LessThan() { | ||
x := []string{"a"} | ||
if len(x) < len(x) { // want "Boolean expression has identical expressions on both sides. The result is always false." | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to ignore tests because of #7074