You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
structBuffer:IDisposable{byte[]?_rented;// S2933: Make '_rented' 'readonly'.byte[]_bytes;// S2933: Make '_bytes' 'readonly'.publicbyte[] Data => _bytes;publicBuffer(byte[]buffer){_bytes=buffer;}publicBuffer(intcapacity){_rented=ArrayPool<byte>.Shared.Rent(capacity);_bytes=_rented;}publicvoidDispose(){if(_rented!=null)ArrayPool<byte>.Shared.Return(_rented);this=default;// <-- Look here.}}
But after adding the readonly modifiers, Visual Studio complains with:
IDE0064: Struct contains assignment to 'this' outside of costructor. Make readonly fields writable.
Expected behavior
I would expect SonarLint to either disable IDE0064 or not report this case at all.
public void Dispose()
{
if (_rented != null)
ArrayPool<byte>.Shared.Return(_rented);
- this = default; // <-- Look here.+ _rented = null;+ _bytes = null!;
}
Related information
SonarLint for Visual Studio 2022, version 8.9.0.92083
Visual Studio Professional 2022 (64 bit), version 17.10.4
dotnet 8.0.303
MSBuild version 17.10.4+10fbfbf2e for .NET Framework, version 17.10.4.21802
Windows 11 Pro 23H2 22631.3880
The text was updated successfully, but these errors were encountered:
fiotti
changed the title
Fix S2933 FP/FN: Issue title
Fix S2933 FP/FN: readonly fields in a struct re-assigned with 'this'
Aug 26, 2024
fiotti
changed the title
Fix S2933 FP/FN: readonly fields in a struct re-assigned with 'this'
Fix S2933 FP: readonly fields in a struct re-assigned with 'this'
Aug 26, 2024
Hi @fiotti. Thank you for reporting the issue. Confirmed as False Positive.
Out of curiosity: why do you use a struct instead of a class in the code sample? Creating an IDisposable struct sounds dangerous, as it's passed by value (copied).
varbuf=new Buffer();using(varinsideBuffer=new Buffer(1000)){buf=insideBuffer;}// After this line buf._rented will point to a memory location that's no longer usable.
Hi @zsolt-kolbay-sonarsource 🙋♂️ This example is artificial just to trigger the warning. I used a struct instead of a class because it's not possible to assign this = something; inside classes.
For reference, I took inspiration from this code from Microsoft, which happens to involve a ref struct with a Dispose() method, even though it doesn't implement IDisposable as ref structs cannot implement interfaces.
Microsoft agrees with you, btw. They are not making this struct public until we get support for the [NonCopyable] attribute which allows to mark structs as non-safe-for-copy.
Description
S2933 disagrees with IDE0064.
Repro steps
But after adding the
readonly
modifiers, Visual Studio complains with:Expected behavior
I would expect SonarLint to either disable IDE0064 or not report this case at all.
According to Microsoft it is incorrect to add the
readonly
modifiers in this case.Actual behavior
SonarLint reports a warning.
Known workarounds
or
or
Related information
The text was updated successfully, but these errors were encountered: