-
-
Notifications
You must be signed in to change notification settings - Fork 117
Description
Problem Statement
This warning has been more falsely alarming than helpful in my case: I always design my test cases as immutable by default; they really should not have any mutable state associated with them. Such false positive cases include:
- immutable record classes without settable state
- .NET-provided types like
EncodingandStringComparer(obviously many more also)
Proposed Solution
The point of the warning still makes sense when the type is evidently abusable, such as by having settable fields or properties.
Based on the aforementioned Encoding case, the warning should have rules such that special-casing Encoding or any similar BCL classes is not required. Unfortunately, Encoding contains private and internal non-readonly fields that could really be made readonly. However, the publicly exposed properties and fields are all readonly.
My recommendation is to adjust the rule such that the warning is only emitted to reference types with:
- Any non-init property setters (of any accessibility)
- Any non-readonly public fields
- Any
Lazy<T>fields or properties
I'm special-casing Lazy there to ensure the thread-safety of the type. It's possibly common enough to encounter lazily-evaluated values without caring for thread safety. Not to mention that the safety is only parameterized; anyone could provide an improperly-configured Lazy and thread safety can be violated.
Alternatives Considered
#4770 is another issue about this but only specifically tackles records. Since it has been closed, I preferred to open a new one to also properly capture the scope of non-record types.
Feature Category
Data-Driven Testing (Arguments, DataSources)
How important is this feature to you?
Nice to have - would improve my experience
Additional Context
No response
Contribution
- I'm willing to submit a pull request for this feature