-
Notifications
You must be signed in to change notification settings - Fork 425
uncheckedtypeassertion: recognize safe comma-ok form in var init and parenthesized assertions #39774
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
uncheckedtypeassertion: recognize safe comma-ok form in var init and parenthesized assertions #39774
Changes from all commits
d307820
08be1f2
fd0d486
8148795
d46167e
5709918
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,54 @@ func GoodTwoValueBlankOk(v interface{}) string { | |
| return s | ||
| } | ||
|
|
||
| // Good: two-value var declaration is safe. | ||
| func GoodTwoValueVarDecl(v interface{}) { | ||
| var s, ok = v.(string) | ||
| if ok { | ||
| fmt.Println(s) | ||
| } | ||
| } | ||
|
|
||
| // Good: parenthesized two-value assignment is safe. | ||
| func GoodTwoValueParen(v interface{}) { | ||
| s, ok := (v.(string)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] Consider adding a multi-level parens test: 💡 Suggested fixture// Good: double-parenthesized two-value assignment is safe.
func GoodTwoValueDoubleParen(v interface{}) {
s, ok := ((v.(string)))
if ok {
fmt.Println(s)
}
}Without this, if the loop were accidentally changed to only strip a single
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
💡 Suggested additionBoth Add: // Good: parenthesized two-value re-assignment is safe.
func GoodTwoValueParenAssign(v interface{}) {
var s string
var ok bool
s, ok = (v.(string))
if ok {
fmt.Println(s)
}
} |
||
| if ok { | ||
| fmt.Println(s) | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] Missing combined test: 💡 Suggested fixture// Good: parenthesized two-value var declaration is safe.
func GoodTwoValueParenVarDecl(v interface{}) {
var s, ok = (v.(string))
if ok {
fmt.Println(s)
}
}This is the intersection of |
||
|
|
||
| // Good: parenthesized two-value re-assignment is safe. | ||
| func GoodTwoValueParenAssign(v interface{}) { | ||
| var s string | ||
| var ok bool | ||
| s, ok = (v.(string)) | ||
| if ok { | ||
| fmt.Println(s) | ||
| } | ||
| } | ||
|
|
||
| // Good: parenthesized two-value var declaration is safe. | ||
| func GoodTwoValueVarDeclParen(v interface{}) { | ||
| var s, ok = (v.(string)) | ||
| if ok { | ||
| fmt.Println(s) | ||
| } | ||
| } | ||
|
|
||
| // Good: double-parenthesized two-value assignment is safe. | ||
| func GoodTwoValueDoubleParen(v interface{}) { | ||
| s, ok := ((v.(string))) | ||
| if ok { | ||
| fmt.Println(s) | ||
| } | ||
| } | ||
|
|
||
| // Bad: single-value var declaration may panic. | ||
| func BadSingleValueVarDecl(v interface{}) { | ||
| var s = v.(string) // want `type assertion x\.\(string\) is unchecked and may panic` | ||
| fmt.Println(s) | ||
| } | ||
|
|
||
| func SuppressedPreviousLine(v interface{}) string { | ||
| //nolint:uncheckedtypeassertion | ||
| return v.(string) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Paren +
ValueSpeccombination is not tested, so the paren-unwrapping path for the newValueSpecbranch has zero coverage.💡 Suggested addition
GoodTwoValueVarDecltestsvar s, ok = v.(string)(ValueSpec branch) andGoodTwoValueParentestss, ok := (v.(string))(paren→AssignStmt). Neither tests the cross-product:var s, ok = (v.(string)), which goes through the paren-unwrapping loop and then lands onValueSpec.If the loop were ever changed to stop early (e.g. breaking on nil before reaching the real parent), this combination would silently produce a false positive and no existing test would catch it.
Add: