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

Code fix for SA 1202 breaks code #1767

Open
JohanLarsson opened this issue Nov 13, 2015 · 4 comments
Open

Code fix for SA 1202 breaks code #1767

JohanLarsson opened this issue Nov 13, 2015 · 4 comments

Comments

@JohanLarsson
Copy link

public static class Foo
{
    private static readonly string A = string.Empty;
    public static readonly string B = A;
}

Repro ^

@sharwell
Copy link
Member

@Noryoko @hvanbakel How would you recommend this be handled?

@hvanbakel
Copy link
Contributor

@sharwell I would suggest to leave it like this to be honest. This is just a (hopefully very rare) edge case in my opinion with a public field which could've been encapsulated as a property.

@sharwell
Copy link
Member

The code above is interesting. It's almost always a bug to have code like this, and an analyzer could certainly report cases where it occurs. Filed DotNetAnalyzers/Proposals#42.

I am concerned about the code fix behavior for the following reasons (which all add up to what looks like a pretty major problem):

  1. The code fix is not expected to change behavior at all, but it can.
  2. The incorrect code is not obvious to the eye. Users need to understand the order of evaluation in C#, and also in large files need to actually see both of the affected fields to see that their placement changed.
  3. The change in behavior is completely silent. No warning or errors are produced with either ordering.

@JohanLarsson
Copy link
Author

@hvanbakel pretty common in WPF, not sure you can declare a readonly dependency property without violating it.

Sample

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

No branches or pull requests

3 participants