Skip to content

Commit

Permalink
rules.go: add exprUnparen rule
Browse files Browse the repository at this point in the history
Refs go-critic/go-critic#836

Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
  • Loading branch information
quasilyte committed Oct 11, 2020
1 parent 1d66c40 commit 60c6e23
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions analyzer/testdata/src/extra/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,16 @@ func testYodaExpr() {
if nil != clusterContext.PostInstallData.AnotherNestedStruct.DeeplyNestedField { // want `\Qsuggestion: $s != nil`
}
}

func superfluousParens() {
f := func(xs ...interface{}) int { return 0 }
xs := []int{1, 2}

f(0)
f((1)) // want `\Qthe parentheses around 1 are superfluous`
f((xs[0] + 2)) // want `\Qthe parentheses around xs[0] + 2 are superfluous`
f(0, (1 + 2)) // want `\Qthe parentheses around 1 + 2 are superfluous`
f(0, 1, (xs[0] + xs[1])) // want `\Qthe parentheses around xs[0] + xs[1] are superfluous`
f(0, (f(0)), 0) // want `\Qthe parentheses around f(0) are superfluous`
f((0), (1)) // want `\Qthe parentheses around 0 are superfluous`
}
3 changes: 3 additions & 0 deletions analyzer/testdata/src/extra/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func _(m fluent.Matcher) {
// that ||, the result would not be functionally identical.
m.Match(`($a) || ($b)`).Report(`rewrite as '$a || $b'`)
m.Match(`($a) && ($b)`).Report(`rewrite as '$a && $b'`)
m.Match(`$f($*_, ($x), $*_)`).
Report(`the parentheses around $x are superfluous`).
Suggest(`$f($x)`)

m.Match(`context.TODO()`).Report(`might want to replace context.TODO()`)

Expand Down
6 changes: 6 additions & 0 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ func _(m fluent.Matcher) {
Suggest(`$a+$b`)
}

func exprUnparen(m fluent.Matcher) {
m.Match(`$f($*_, ($x), $*_)`).
Report(`the parentheses around $x are superfluous`).
Suggest(`$f($x)`)
}

func osFilepath(m fluent.Matcher) {
// path/filepath package forwards path separators so if
// the file already uses filepath-related API it might be
Expand Down

0 comments on commit 60c6e23

Please sign in to comment.