-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
IDE0019 suggests pattern matching but the generated pattern matching changes the meaning of the code #39600
Comments
Should we only allow the refactoring if the |
I guess my view is fundamentally simpler than yours. It reads "Don't suggest fixes you don't know don't change the meaning of the code." |
These are effectively all fixes. We abandoned that approach because it meant we could almost never actually supply options, even when it was safe 99%+ of the time. as a simple example: var v = a.b == null ? null : a.b.c; In nearly every codebase, this can safely move to:
But that's not guaranteed, and there's a chance semantics may break. We opt to helping out the majority of users for the most common code patterns, and we'd like warnings for users that use patterns where the fix may be possible, but may statically look like it carries more risk. |
I wouldn't do that. Consider: var dataLevelId = x as byte?;
var visibleExpression = y as string;
var enabledExpression = z as string;
var completeExpression = w as string;
if (questionIds.Contains(questionId))
{
if (visibleExpression != null)
visibleExpressions[questionId] = visibleExpression;
if (enabledExpression != null)
enabledExpressions[questionId] = enabledExpression;
if (completeExpression != null)
completeExpressions[questionId] = completeExpression;
}
} There would still be merit here in updating the code. I would, however, look for obvious things that signal mutation (++) being one of those things, and then move to warning (optionally with a different diagnostic or property). The different diagnostic/property would exclude these cases from participating in fix-all due to the higher risk associated with them. |
In nearly every codebase, this can safely move to:
I actually had a sample where this didn't work. Somebody actually fixed it in a recent release to stop offering it on just that case. ( |
Yes, as i mentioned: But that's not guaranteed, and there's a chance semantics may break. We opt to helping out the majority of users for the most common code patterns, and we'd like warnings for users that use patterns where the fix may be possible, but may statically look like it carries more risk. The goal is to not let the rare cases prevent providing value for the more common and normal types of code cases. Where we can detect something potentially odd, we can try to add appropriate smarts. However, there are whole hosts of cases where there is no way to know statically and we can choose to basically cut the feature, or assume common coding patterns and attempt to offer the user something helpful for almost all cases. |
Note: happy to discuss this more over at gitter.im/dotnet/roslyn or aka.ms/csharp-discord. Thanks! |
Version Used:
C# 8.0
Steps to Reproduce:
Apologies if this doesn't quite reproduce here. The origin file is some 3000 lines and the project is probably in the millions.
For some reason it wants to use pattern matching but can't understand that
i
has a different value by that time and the code replacement is non-working code.We use this indexing idiom here because this loop runs thousands of times per web request and it's very nearly as resilient to schema changes as string indexers and just as easy to read as string indexers and as fast as numeric literal indexers into the data reader. The real code has 18 columns here.
Expected Behavior:
No ... under var as the fix can't be applied here.
Actual Behavior:
Attached screen grab.
![ide0019insanity](https://user-images.githubusercontent.com/17086870/67914096-63164180-fb4c-11e9-9ea4-ff8ead5a103a.png)
The text was updated successfully, but these errors were encountered: