-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fix false positive 'Unnecessary assignment of a value' #77297
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
Changes from 1 commit
9b24f62
8c00a94
696f317
3eb8e1b
e96bc3a
7f67f6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -210,7 +210,16 @@ where void Foo(in T v) | |
| // Accessing an indexer/property off of a value type will read/write the value type depending on how the | ||
| // indexer/property itself is used. We add ValueUsageInfo.Read to the result since the value on which we are | ||
| // accessing a property is always read. | ||
| return ValueUsageInfo.Read | GetValueUsageInfo(operation.Parent, containingSymbol); | ||
| // We consider a potential write only if the type is not a ref-like type. | ||
|
||
| // For example, if we have `s[0] = ...`, we will consider this as: | ||
| // 1. Read+Write: if s is value type and is not ref-like type. | ||
| // 2. Read only: if s is value type and is ref-like type. | ||
| // In the second case, it's a similar treatment as if it was a reference type. | ||
| // This is to help with unused assignment analysis where it's valid to have s[0] = ... without a subsequent read | ||
| // as the assignment can be observed by the caller. | ||
| return operation.Type.IsRefLikeType | ||
| ? ValueUsageInfo.Read | ||
| : ValueUsageInfo.Read | GetValueUsageInfo(operation.Parent, containingSymbol); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove both the recent changes made to this file. moving to MakeFieldReadonly. |
||
| else if (operation.Parent is IVariableInitializerOperation variableInitializerOperation) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider work items
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure where the related issue on GitHub is. The one I could find is #77258 but it's unrelated to ref-structs and won't be fixed by this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a problem! Btw, do you know what would fix 77258?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#77258 feels tricky. If accurate results are needed then I'm guessing some sort of interprocedural analysis may be needed?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intraprocedural analysis isn't safe to do and would represent at best a point in time snapshot. It's notably also "impossible" to do for cross assembly due to the existence of reference assemblies.
We would require some new attribute or construct that represents the necessary concept (which is most functionally similar to a
Purelike annotation) and have it documented as a breaking change to remove from a method once it's been placed on it.In general any property, indexer, or method may have side effects or involve indirect mutations; even if these members are annotated
readonly. The implementation of such members may change over time or differ at runtime as compared to compile time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The question here is if we can make the field read-only. The field can be readonly if the user is only access read-only members off of it, as those won't cause copies to happen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An indirect mutation is fine. This analyzer is just asking if the field can be readonly. It can be, while still making indirect mutations.
Note: the fail case here is to not offer anything at all. So it's a narrow whitelist of cases we offer the feature for. And it's a very popular and widely used analyzer/fixer with widespread usage. We want it to still be useful in th those whitelisted cases, without accidentally having false positives.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, gotcha. The comment was confusing to me and I understood it as relating to
IDE0059(unnecessary assignment of a value). It sounds like you're saying its rather in relation toIDE0044(add readonly modifier),IDE0250(struct can be made readonly), orIDE0251(member can be made readonly)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup! There are multiple features affected here, all with different desired behaviors :-)
"Make member read-only" wants to offer in all cases where it is totally safe to make readonly (so where it would not cause errors, or different runtime behavior (like a copy)).
"Remove unnecessary assignment" wants to inform the user of a full overwrite of a variable (local, parameter) that won't be observed (either because there no following reads, or because there is another full write that follows).
Different features. Different goals. Subtle interactions since they use some common helpers which we need to be more vigilant about :-)