Skip to content
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

ruleguard: ignore match expr width limit for suggestions #63

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions analyzer/testdata/src/extra/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,21 @@ func testEmptyVarBlock() {
type () // want `\Qempty type() block`
const () // want `\Qempty const() block`
}

func testYodaExpr() {
var clusterContext struct {
PostInstallData struct {
CoreDNSUpdateFunction func()
AnotherNestedStruct struct {
DeeplyNestedField *int
}
}
}

// This is on the boundary of being too long to be displayed in the CLI output.
if nil != clusterContext.PostInstallData.CoreDNSUpdateFunction { // want `\Qsuggestion: clusterContext.PostInstallData.CoreDNSUpdateFunction != nil`
}
// This is far too long, so it's shortened in the output.
if nil != clusterContext.PostInstallData.AnotherNestedStruct.DeeplyNestedField { // want `\Qsuggestion: $s != nil`
}
}
2 changes: 2 additions & 0 deletions analyzer/testdata/src/extra/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,6 @@ func _(m fluent.Matcher) {

m.Match(`byte($x)`).Where(m["x"].Type.Is("byte")).Suggest(`$x`)
m.Match(`rune($x)`).Where(m["x"].Type.Is("rune")).Suggest(`$x`)

m.Match(`nil != $s`).Where(!m["s"].Const).Suggest(`$s != nil`)
}
8 changes: 4 additions & 4 deletions ruleguard/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ func (rr *rulesRunner) handleMatch(rule goRule, m gogrep.MatchData) bool {
if rule.severity != "" {
prefix = rule.severity + ": "
}
message := prefix + rr.renderMessage(rule.msg, m.Node, m.Values)
message := prefix + rr.renderMessage(rule.msg, m.Node, m.Values, true)
node := m.Node
if rule.location != "" {
node = m.Values[rule.location]
}
var suggestion *Suggestion
if rule.suggestion != "" {
suggestion = &Suggestion{
Replacement: []byte(rr.renderMessage(rule.suggestion, m.Node, m.Values)),
Replacement: []byte(rr.renderMessage(rule.suggestion, m.Node, m.Values, false)),
From: node.Pos(),
To: node.End(),
}
Expand All @@ -162,7 +162,7 @@ func (rr *rulesRunner) handleMatch(rule goRule, m gogrep.MatchData) bool {
return true
}

func (rr *rulesRunner) renderMessage(msg string, n ast.Node, nodes map[string]ast.Node) string {
func (rr *rulesRunner) renderMessage(msg string, n ast.Node, nodes map[string]ast.Node, truncate bool) string {
var buf strings.Builder
if strings.Contains(msg, "$$") {
buf.Write(rr.nodeText(n))
Expand All @@ -180,7 +180,7 @@ func (rr *rulesRunner) renderMessage(msg string, n ast.Node, nodes map[string]as
buf.Write(rr.nodeText(n))
// Don't interpolate strings that are too long.
var replacement string
if buf.Len() > 40 {
if truncate && buf.Len() > 60 {
replacement = key
} else {
replacement = buf.String()
Expand Down