-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gocritic: add support for variable substitution in ruleguard path set…
…tings (#2308) * Add variable for ruleguard config directory * Add variable for ruleguard config directory * Add variable for ruleguard config directory * Add variable for ruleguard config directory * Add unit tests * Add unit tests for ruleguard * Add unit tests for ruleguard * Add unit tests for ruleguard * Add unit tests for ruleguard, fix package name
- Loading branch information
1 parent
f9f6486
commit 2c01ea7
Showing
16 changed files
with
98 additions
and
9 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 @@ | ||
This directory contains ruleguard files that are used in functional tests. |
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,22 @@ | ||
// go:build ruleguard | ||
package ruleguard | ||
|
||
import "github.com/quasilyte/go-ruleguard/dsl" | ||
|
||
// Suppose that we want to report the duplicated left and right operands of binary operations. | ||
// | ||
// But if the operand has some side effects, this rule can cause false positives: | ||
// `f() && f()` can make sense (although it's not the best piece of code). | ||
// | ||
// This is where *filters* come to the rescue. | ||
func DupSubExpr(m dsl.Matcher) { | ||
// All filters are written as a Where() argument. | ||
// In our case, we need to assert that $x is "pure". | ||
// It can be achieved by checking the m["x"] member Pure field. | ||
m.Match(`$x || $x`, | ||
`$x && $x`, | ||
`$x | $x`, | ||
`$x & $x`). | ||
Where(m["x"].Pure). | ||
Report(`suspicious identical LHS and RHS`) | ||
} |
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,18 @@ | ||
// go:build ruleguard | ||
package ruleguard | ||
|
||
import "github.com/quasilyte/go-ruleguard/dsl" | ||
|
||
func StringsSimplify(m dsl.Matcher) { | ||
// Some issues have simple fixes that can be expressed as | ||
// a replacement pattern. Rules can use Suggest() function | ||
// to add a quickfix action for such issues. | ||
m.Match(`strings.Replace($s, $old, $new, -1)`). | ||
Report(`this Replace call can be simplified`). | ||
Suggest(`strings.ReplaceAll($s, $old, $new)`) | ||
|
||
// Suggest() can be used without Report(). | ||
// It'll print the suggested template to the user. | ||
m.Match(`strings.Count($s1, $s2) == 0`). | ||
Suggest(`!strings.Contains($s1, $s2)`) | ||
} |
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