-
Notifications
You must be signed in to change notification settings - Fork 0
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
False positive: Optional proto-fields being flagged and fixed if checking if the field is nil #5
Comments
Hello, thanks for the issue! Yes, I discovered a problem with optional fields and am trying to solve it. Linter suggests using getter if it exists, because if you try to get a nested field, even one that is not a reference field, since the parent message may be nil. But in this case it is a bug in the linter, I will try to fix it as soon as possible. |
Please check the new linter update https://github.com/ghostiam/protogetter/releases/latest |
The new version works well, no more unnecessary linter warnings 👍 |
Hi, sorry to dig up an old issue, but it sounds like this issue was supposed to address what I'm trying to do, and I'm still having issues with optional fields. I have a message like this: message MyMessage {
optional int32 my_field = 1;
} and I want to write a function that returns func getValue(msg *MyMessage) *int {
if msg != nil && msg.MyField != nil {
value := int(*msg.MyField)
return &value
}
return nil
} but this results in the following error:
But How should I write this code to make the linter happy? I'm using golangci-lint 1.57.2 which I believe uses protogetter 0.3.5. Incidentally, I get the same warning with code like this: var value *int32
if msg != nil {
value = msg.MyField
} |
@arvidfm, Hi, thanks for the question. You can silence the linter using a comment: var value *int32
if msg != nil {
//nolint:protogetter
value = msg.MyField
} Also, the linter does not prohibit you from checking the field for func getValue(msg *MyMessage) *int {
if msg != nil && msg.MyField != nil {
- value := int(*msg.MyField)
+ value := int(msg.GetMyField())
return &value
}
return nil
} |
@ghostiam A-ha! Thank you, the second suggestion did the trick! I didn't realise that the |
I have a multiple proto messages with optional fields defined, for example:
This generates a getter for the optional field like this:
The linter flags and fixes accessing this field to use this getter method as follows:
This obviously doesn't work. Checking for zero here doesn't work, as zero is a valid value for this field and is for that reason marked as optional so that I can check if the field is nil instead.
Should the linter only fix cases where we are directly de-referencing the value from the pointer?
So it could fix this:
But not this:
The text was updated successfully, but these errors were encountered: