8000 st1018: don't suggest escape sequences inside raw string literals by Eljees · Pull Request #1731 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content
Draft
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
32 changes: 28 additions & 4 deletions stylecheck/st1018/st1018.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func run(pass *analysis.Pass) (any, error) {
return
}

// Escape sequences are not interpreted inside raw string literals: replacing an
// invisible character with \u200b there would change the string's value to the
// six characters of the escape sequence. For raw strings we therefore only offer
// to delete the character, and we don't tell the user to use escape sequences.
isRaw := lit.Value[0] == '`'

type invalid struct {
r rune
off int
Expand Down Expand Up @@ -93,7 +99,12 @@ func run(pass *analysis.Pass) (any, error) {
}

r := invalids[0]
msg := fmt.Sprintf("string literal contains the Unicode %s character %U, consider using the %q escape sequence instead", kind, r.r, r.r)
var msg string
if isRaw {
msg = fmt.Sprintf("raw string literal contains the Unicode %s character %U, consider using an interpreted string literal and the %q escape sequence instead", kind, r.r, r.r)
} else {
msg = fmt.Sprintf("string literal contains the Unicode %s character %U, consider using the %q escape sequence instead", kind, r.r, r.r)
}

replacement := strconv.QuoteRune(r.r)
replacement = replacement[1 : len(replacement)-1]
Expand All @@ -112,7 +123,11 @@ func run(pass *analysis.Pass) (any, error) {
End: lit.Pos() + token.Pos(r.off) + token.Pos(utf8.RuneLen(r.r)),
}},
}
report.Report(pass, lit, msg, report.Fixes(edit, delete))
if isRaw {
report.Report(pass, lit, msg, report.Fixes(delete))
} else {
report.Report(pass, lit, msg, report.Fixes(edit, delete))
}
default:
var kind string
if hasFormat && hasControl {
Expand All @@ -125,7 +140,12 @@ func run(pass *analysis.Pass) (any, error) {
panic("unreachable")
}

msg := fmt.Sprintf("string literal contains Unicode %s characters, consider using escape sequences instead", kind)
var msg string
if isRaw {
msg = fmt.Sprintf("raw string literal contains Unicode %s characters, consider using an interpreted string literal and escape sequences instead", kind)
} else {
msg = fmt.Sprintf("string literal contains Unicode %s characters, consider using escape sequences instead", kind)
}
var edits []analysis.TextEdit
var deletions []analysis.TextEdit
for _, r := range invalids {
Expand All @@ -149,7 +169,11 @@ func run(pass *analysis.Pass) (any, error) {
Message: fmt.Sprintf("delete all %s characters", kind),
TextEdits: deletions,
}
report.Report(pass, lit, msg, report.Fixes(edit, delete))
if isRaw {
report.Report(pass, lit, msg, report.Fixes(delete))
} else {
report.Report(pass, lit, msg, report.Fixes(edit, delete))
}
}
}
code.Preorder(pass, fn, (*ast.BasicLit)(nil))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,6 @@ est`
k = "🏴󠁧󠁢󠁷󠁬󠁳󠁿"
)

-- replace format character U+200B with '\u200b' --
// Package pkg ...
package pkg

var (
a = "" //@ diag(`Unicode control character U+0007`)
b = "" //@ diag(`Unicode control characters`)
c = "Test test"
d = `T
est`
e = `Zero\u200bWidth` //@ diag(`Unicode format character U+200B`)
f = "\u200b"
g = "👩🏽‍🔬" //@ diag(`Unicode control character U+0007`)
h = "👩🏽‍🔬​" //@ diag(`Unicode format and control characters`)
i = "👨‍👩‍👦"
j = "🏳️‍🌈"
k = "🏴󠁧󠁢󠁷󠁬󠁳󠁿"
)

-- delete all control characters --
// Package pkg ...
package pkg
Expand Down
0