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

New analyzer: Avoid usage of 'with' keyword outside record declaration #33

Closed
ronnygunawan opened this issue Nov 23, 2020 · 1 comment
Closed
Assignees
Labels
enhancement New feature or request

Comments

@ronnygunawan
Copy link
Owner

ronnygunawan commented Nov 23, 2020

record Foo(int X);

void Test() {
    Foo f = new(1);
    f = f with { X = 2 }; // warning: 'with' keyword used outside record declaration
}

Suggested best practice:

record Foo(int X) {
    public Foo WithX(int x) => this with { X = x };
    public Bar WithY(int y) => new(this, y);
}

record Bar(int X, int Y) : Foo (X) {
    public Bar(Foo foo, int y) : this(foo.X, y) { }
}

void Test() {
    Foo f = new(2);
    f = f.WithX(2);
    Bar b = f.WithY(3);
}
@ronnygunawan ronnygunawan self-assigned this Nov 23, 2020
@ronnygunawan ronnygunawan added the enhancement New feature or request label Nov 23, 2020
@ronnygunawan
Copy link
Owner Author

Added in 1.0.18

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

1 participant