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

Error when applying to pointer field in fix mode #11

Open
eyasy1217 opened this issue Jul 23, 2024 · 4 comments
Open

Error when applying to pointer field in fix mode #11

eyasy1217 opened this issue Jul 23, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@eyasy1217
Copy link

Hi.

When I apply a pointer field in fix mode, I get a compile error(IncompatibleAssign).
Because the getter in the pointer field returns a value.
For example, protoc generates the following code.

type Foo struct {
	// ...

	CancelPolicy *string `protobuf:"bytes,3,opt,name=cancel_policy,json=cancelPolicy,proto3,oneof" json:"cancel_policy,omitempty"`
}

func (x *Foo) GetCancelPolicy() string {
	if x != nil && x.CancelPolicy != nil {
		return *x.CancelPolicy
	}
	return ""
}

version

@ghostiam
Copy link
Owner

ghostiam commented Jul 23, 2024

Hello, thank you for your interest in the project.

Could you show the code (before the fix), which after the fix no longer works?
Since you provided code from a generated proto file, which should not be processed by the linter.

You might also want to look at: #5 (comment)

@ghostiam ghostiam added bug Something isn't working question Further information is requested labels Jul 23, 2024
@eyasy1217
Copy link
Author

Could you show the code (before the fix), which after the fix no longer works?

See bellow.

type MyFoo struct 
	CancelPolicy *string
}

// f's type is *Foo

m := MyFoo{
-	CancelPolicy: f.CancelPolicy,
+	CancelPolicy: f.GetCancelPolicy(), // cannot use f.GetCancelPolicy() (value of type string) as *string value in struct literal compiler(IncompatibleAssign)
}

Therefore, I modified it myself as follows.

+c := res.GetCancelPolicy()
m := MyFoo{
-	CancelPolicy: f.CancelPolicy,
+	CancelPolicy: &c,
}

@ghostiam
Copy link
Owner

Now I understand.
I’ll think about how to solve this problem, but here the field is already beyond what protogetter scans.

At this point, if you really need nil in a given field (rather than zero value), I would suggest adding
//nolint:protogetter

m := MyFoo{
	CancelPolicy: f.CancelPolicy, //nolint:protogetter
}

@ghostiam ghostiam added enhancement New feature or request and removed bug Something isn't working question Further information is requested labels Jul 24, 2024
@jalaziz
Copy link

jalaziz commented Aug 20, 2024

I've run into this and would love to see it work out of the box.

It seems to me that this linter should allow assigning the field directly when the destination type is known to be a pointer type. A potentially trickier thing to solve for is accessing a nested field safely but then assigning the raw field to a pointer field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants