-
Notifications
You must be signed in to change notification settings - Fork 468
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 rule: using mutable structs in readonly fields #2811
Comments
Yes
For SpinLock, I can't think of any real situation where it's not definitively a bug. Calling For GCHandle, it may not be a real buge.g. if it's stored into an object that needs access to what's in the handle, but, for example, the handle will never be Free'd, then you could imagine
Yes. If the object storing the struct has no plans or need to mutate it and is just reading from it, you could imagine someone wanting to make it readonly. But I think that's still worth a suggestion/warning/whatever default level is chosen. However, this is also why IMO any such rule should focus only on a specific list of hand-picked types, rather than, say, warning on any struct that's not |
What package/project should this go in? I don't think this is an FxCop rule so maybe Microsoft.CodeQuality.Analyzers or Microsoft.NetCore.Analyzers? |
I have started looking into the implementation here and came up with a few more questions:
|
We generally follow the below steps:
I would recommend just using GetTypeByMetadataName. The workaround/fallback strategy was primarily added for ImmutableArray due to the fact that it exists in couple of different assemblies and the test framework was including both of them. It will likely be removed in future. |
FxCopAnalyzers package does not have any rules by itself. It is just a master package that includes both Microsoft.CodeQuality.Analyzers and Microsoft.NetCore.Analyzers as dependencies for ease of install and migration for legacy FxCop users. I hope the description over https://github.com/dotnet/roslyn-analyzers#microsoftcodequalityanalyzers and https://github.com/dotnet/roslyn-analyzers#microsoftnetcoreanalyzers helps you decide the correct package to choose. Bottom line is analyzers related to purely code quality improvements, but are not specific to any API will go into Microsoft.CodeQuality.Analyzers. Analyzers specific to usage of any API would go into Microsoft.NetCore.Analyzers. A good rule of thumb is that if your analyzer needs to invoke |
Based on the questions from dotnet#2811
FYI: I submitted a PR to document the above guidelines in the repo. |
Guidelines are now at https://github.com/dotnet/roslyn-analyzers/blob/master/GuidelinesForNewRules.md |
Awesome, thanks for the guidance! |
Reading the comments above another time, it seems like this is not a performance rule, but indicates a very likely functional bug. Is that correct? If so, this should likely not be a Performance rule, but must have a different category and rule ID. Thoughts? |
Yeah, I'm on the fence here. On one side, using the readonly modifier degrades performance because of the shadow copying by the compiler. On the other, using a So it's kind of Performance and kind of Usage or Maintainability. Not sure which would be best but I agree the performance aspect is rather marginal here. |
I don't think it should be performance. Can you come up with a benchmark where adding readonly measurably improves performance with a correct implementation? |
Also a good point. New idea: since such a usage likely (at least for the |
What's the definition of the correctness category vs the reliability category? |
Ideally we would have the ability to mark I would argue that for the rule in question here, the importance of non-copyable is sufficient to make that the target behavior of the rule instead of limiting it to whether or not the storage is readonly. |
As far as I can see in the docs there is no correctness category. Which is consistent with the From the description of the Reliability category, I think it might be a good fit:
|
@mavasani I noticed this proposal was marked as approved in this repo. I'll remove the label since we want it to go through API review via dotnet/runtime#33773 Edit: Maybe we can make an exception with this one since the discussion is large and there's a PR out already. I'll wait for confirmation on the runtime issue. |
A split off issue from corefx/issues/40739 to track the suggested rule
readonly SpinLock fields. SpinLock is a mutable struct, meant only for advanced scenarios. Accidentally making a SpinLock field readonly can result in silent but significant problems, as any mutations to the instance (e.g. Enter, Exit) will be done on a compiler-generated copy and thus be ignored, making the lock an expensive nop. (It might make sense to extend this analyzer to additional mutable struct types where storing them in a readonly field is likely a bug, e.g. GCHandle.)
Detecting this should be easy - for each class/struct find all fields that are
readonly
and of a type that is designed to be mutable (for starters, theGCHandle
andSpinLock
).Questions:
The text was updated successfully, but these errors were encountered: