-
Notifications
You must be signed in to change notification settings - Fork 285
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
modifies-value-receiver: miss modification by setting something in a sub structure #1117
Comments
The head version of type B struct {
whatever bool
}
func (b B) Foo() B {
b.whatever = true
return b
}
type A struct {
B B
}
func (a A) Foo() A {
a.B.whatever = true
return a
} For the second code sample type B struct {
whatever bool
}
func (b B) Foo() {
b.whatever = true
}
type A struct {
B
}
func (a A) Foo(){
a.B.whatever = true
}
type B struct {
whatever bool
}
func (b B) Foo() {
b.whatever = true
}
type A struct {
B B
}
func (a A) Foo() {
a.B.whatever = true
} But if we define type C struct {
whatever bool
}
func (c C) Foo() {
c.whatever = true
}
type B *C
type A struct {
B B
}
func (a A) Foo() {
a.B.whatever = true
} the assignment is legit The definition of other types than the one for which we check the methods (A in the example) are not always available in the same file thus it will require the rule to, at least, use type-checker information. Currently, the rule is based on syntax-level analysis of the method (it simply checks if the receiver's identifier appears in the LHS of an assignment expression + some patches to not warn in some corner cases) Extending the rule to cope with the cases shown in the original post is possible. |
Thanks for your reply and the detail you dropped. The example of I agree it can become a nightmare to detect, also I know the rule about linter should avoid false-positive For records, I opened a PR yesterday with examples of what fails and what does |
A current false positive that I think it could be fixed is: func (c RateLimitsConfig) MarshalJSON() ([]byte, error) {
type alias RateLimitsConfig
// serialize as empty array
if c.Rules == nil {
c.Rules = []RateLimitRule{} // suspicious modification...
}
return json.Marshal(alias(c)) //...that might be considered OK because it's used to compute the result
} or // Validate makes PasswordAuthConfig validatable by implementing [validation.Validatable] interface.
func (c PasswordAuthConfig) Validate() error {
// strip duplicated values
c.IdentityFields = list.ToUniqueStringSlice(c.IdentityFields)
return validation.ValidateStruct(&c,
validation.Field(&c.IdentityFields, validation.Required),
)
} |
modifies-value-receiver
reportsb.whatever = true
as an errorbut
a.B.whatever = true
is not reportedSame for
but this leads to detection
Somehow related to :
++
/--
#1109The text was updated successfully, but these errors were encountered: