-
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
Defencive copy does not produce warning #43490
Comments
You can write an analyzer for this purpose. :) @sharwell Were you working on a code-analyzer to catch anything like this? |
The C# language does not include a concept for non-copyable value types. See dotnet/csharplang#859 for a feature request to add this concept. I have been working on an analyzer for this in dotnet/roslyn-analyzers#3420. Another potential option is ufcpp/NonCopyableAnalyzer. Keep in mind this is a very difficult analyzer to get correct, since the language itself allows these copies anywhere it sees fit. |
This is all incorrect you know :) C# 8 out with warning for implicit defensive copies of structs. And it work some times see https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#using-declarations May be issue is in C# docs. |
Could you clarify? The compiler/lang is working as spec'ed as far as i can tell. Can you be more specific about what you think is wrong here? Note that the link you mentioned is specifically about
In your case, your members aren't |
In may case member sd is readonly because entire struct is readonly warning CS8656: raised when compiler try access non readonly member from readonly I suppose when I have readonly struct and try to call any non readonly member on it I must be warned about defensive copy creation in this point. this is beggest pain with defensive copies in C#. Dot state that: "The compiler warns you when it needs to create a defensive copy." Reading this I was very happy because it is hell to event dont have warning on defensive copies but now unhappy because it is false promise or bug in compiler because it create defensive copy and dont warn us about this. What I actually want is make all defensive copy creation warning to be errors in project and make they explicit where we actually need. |
this is not how the feature was designed or specified. Note: as i mentioned here you could always write an analyzer for this. :)
As i stated, that's for certain types of members (and the linked post explicitly states that). For more types of cases, you'll have to write an analyzer.
You can definitely do this by writing an analyzer that can produce errors that will stop your build. :) |
Note: this is explicitly called out here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/readonly-instance-members#drawbacks
Thanks! |
Sad for now :( @CyrusNajmabadi thinks fro your responces :) Can you direct me please how to write additional analyzer for compiler without rewriting compiler itself to add this as plugin into Unity project. |
Sorry, I don't know about any proposal like that.
There are definitely looking if resources out there on writing analyzers :-). You can also get help at gitter.im/dotnet/Roslyn, or discord.gg/csharp (#Roslyn). Thanks! |
Version Used:
Latest from latest VS 3.5.0-beta2-20057-04 (f43cfcd)
Steps to Reproduce:
compile this code
Expected Behavior:
I must have warning about defencive copy of struct
Actual Behavior:
No warning
The text was updated successfully, but these errors were encountered: