Skip to content

Commit

Permalink
fix and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalmahajan15 committed Dec 12, 2023
1 parent 0e7bf2f commit 6480c06
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
56 changes: 55 additions & 1 deletion testdata/src/go.uber.org/errormessage/errormessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func test3(x *int) {

// nilable(f)
type S struct {
f *int
f *int
sPtr *S
}

func test4(x *int) {
Expand Down Expand Up @@ -147,3 +148,56 @@ func test13() *int {
}
return new(int)
}

// below tests check shortening of expressions

// nilable(s, result 0)
func (s *S) bar(i int) *int {
return nil
}

// nilable(result 0)
func (s *S) retNil(a int, b *int, c string, d bool) *S {
return nil
}

func test14(x *int, i int) {
s := &S{}
x = s.retNil(1,
new(int),
"abc",
true).bar(i)
y := x
print(*y) //want "`s.retNil\\(...\\).bar\\(i\\)` to `x`"
}

func test15(x *int) {
var longVarName, anotherLongVarName, yetAnotherLongName int
s := &S{}
x = s.retNil(longVarName, &anotherLongVarName, "abc", true).bar(yetAnotherLongName)
y := x
print(*y) //want "`s.retNil\\(...\\).bar\\(...\\)` to `x`"
}

func test16(mp map[int]*int) {
var aVeryVeryVeryLongIndexVar int
x := mp[aVeryVeryVeryLongIndexVar]
y := x
print(*y) //want "`mp\\[...\\]` to `x`"
}

func test17(x *int, mp map[int]*int) {
var aVeryVeryVeryLongIndexVar int
s := &S{}

x = s.retNil(1, mp[aVeryVeryVeryLongIndexVar], "abc", true).bar(2) //want "deep read"
y := x
print(*y) //want "`s.retNil\\(...\\).bar\\(2\\)` to `x`"
}

func test18(x *int, mp map[int]*int) {
s := &S{}
x = mp[*(s.retNil(1, new(int), "abc", true).bar(2))] //want "dereferenced"
y := x
print(*y) //want "`mp\\[...\\]` to `x`"
}
19 changes: 10 additions & 9 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,30 +516,31 @@ func ExprToString(e ast.Expr, pass *analysis.Pass, isShortenExpr bool) string {
// exprOnLine3)" becomes "foo(...)"
func shortenExpr(expr string) string {
var result strings.Builder
var depth int
var inBrackets bool
var innerExpr strings.Builder
var depth int // helps to keep track of nested brackets
var inBrackets bool // indicates if we are currently inside brackets
var innerExpr strings.Builder // stores the content inside brackets

for _, char := range expr {
switch char {
case '(', '{', '[':
if depth == 0 {
depth++
inBrackets = true
result.WriteRune(char)
result.WriteRune(char) // append the opening bracket
}
depth++
continue

case ')', '}', ']':
depth--
if depth == 0 {
// Replace the content inside brackets with "..." if it is long (more than 3 characters)
// Replace the content inside brackets with "..." if it is long (more than 3 characters), else
// retain the original content as is.
if innerExpr.Len() > 3 {
result.WriteString("...")
} else {
result.WriteString(innerExpr.String())
}
result.WriteRune(char)
result.WriteRune(char) // append the closing bracket

inBrackets = false
innerExpr.Reset()
Expand All @@ -548,10 +549,10 @@ func shortenExpr(expr string) string {
}

if inBrackets {
innerExpr.WriteRune(char)
innerExpr.WriteRune(char) // append the character to the inner expression
continue
}
result.WriteRune(char)
result.WriteRune(char) // append the character to the result if not in brackets, and not a bracket itself
}

return result.String()
Expand Down

0 comments on commit 6480c06

Please sign in to comment.