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

Allow missing properties that are set later in the same scope #125

Open
Seb-C opened this issue Feb 3, 2025 · 2 comments
Open

Allow missing properties that are set later in the same scope #125

Seb-C opened this issue Feb 3, 2025 · 2 comments

Comments

@Seb-C
Copy link

Seb-C commented Feb 3, 2025

Thank you for this analyzer, it's very useful.

In constructors, I very often need to setup many things, sometimes which refer to the struct itself (in order to reuse code, setup destructors, start goroutines...).

It's not always possible to reorganize such methods to set all the properties at once. So I was wondering if it would be possible to allow such pattern whenever it's done in the same scope?

Example:

struct Foo{
    a int
    b string
}

func NewFoo() Foo {
    foo := Foo{
        a: 42,
    }
    foo.b = foo.someMethod()

    return foo
}
@Seb-C
Copy link
Author

Seb-C commented Feb 3, 2025

An alternative idea for this issue could be to add comments to ignore the error on some properties only. I am guessing that this could be easier to implement:

struct Foo{
    propA int
    propB int
    propC int
    propD int
    propE int
}

func NewFoo() Foo {
    //exhaustruct:ignore propB, propC
    foo := Foo{
        propA: 1,
        propD: 4,
    }
    foo.b = foo.someMethod()
    foo.c = foo.anotherThing()

    return foo
}

➡ This would only output an error for the missing propE, but not for propB or propC.

func NewFoo() Foo {
    //exhaustruct:ignore propB, propC
    foo := Foo{
        propA: 1,
        propD: 4,
        propE: 5,
    }
    foo.b = foo.someMethod()
    foo.c = foo.anotherThing()

    return foo
}

➡ This would not output any error

@xobotyi
Copy link
Collaborator

xobotyi commented Feb 3, 2025

Yeah, as I outlined in #123 - I'm planning to implement such thing. But currently it looks like nightmare to implement. Though, Im not given up on this idea yet)

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

No branches or pull requests

2 participants