Open fiotti opened 2 weeks ago
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).
var buf = new Buffer();
using (var insideBuffer = 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