Fix S2583/S2589 FP: Delegate can be null when last invocation is removed but comparing the delegate against null gets reported as always false #8094
Labels
Area: C#
C# rules related issues.
Area: CFG/SE
CFG and SE related issues.
Area: VB.NET
VB.NET rules related issues.
Type: CFG/SE FPs
Rule IS triggered when it shouldn't be for CFG and SE rules.
Milestone
Description
When removing the last invocation in a delegate, it can become null (see https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/subtraction-operator#delegate-removal), but any comparison after that will be flagged as unnecessary because Sonar still identifies the delegate as non null.
![image](https://private-user-images.githubusercontent.com/68167947/270942395-d854d047-20b7-4984-bd71-8fd74ee761c4.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg5MjgyMjMsIm5iZiI6MTczODkyNzkyMywicGF0aCI6Ii82ODE2Nzk0Ny8yNzA5NDIzOTUtZDg1NGQwNDctMjBiNy00OTg0LWJkNzEtOGZkNzRlZTc2MWM0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDclMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA3VDExMzIwM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTg3NWJlY2ZhZTQ3MTMwNDA4Mjc0ZjZlYTI2Njc1ZTU1Y2QyZGE4NDcwNzhmNDBkOTEzYTVhNzZkMThlYzQ0MzImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.h44nnx4MymSrewaWyo-Y_IPiu6ZBI4npCPBhKbtNOL8)
It is correctly identified as potentially null by VS analyzers, but Sonar still thinks it can't be null. See this screenshot as reference:
Repro steps
Expected behavior
Sonar correctly identifies the delegate as potentially null and don't issue any warning.
Actual behavior
Sonar incorrectly identifies the delegate as not null and issues a S2583 warning. It could also be a S2589 depending on the code inside the if.
Related information
The text was updated successfully, but these errors were encountered: