Skip to content
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

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

Closed
ruben-ivre opened this issue Sep 27, 2023 · 1 comment · Fixed by #8457
Assignees
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

Comments

@ruben-ivre
Copy link

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.
It is correctly identified as potentially null by VS analyzers, but Sonar still thinks it can't be null. See this screenshot as reference:
image

Repro steps

    internal class Test
    {
        public void TestMethod() 
        {
            Action? someDelegate = delegate { };
            someDelegate += Callback;
            
            // ....

            someDelegate -= Callback;

            if (someDelegate == null) // S2583 reported here
            {
                // ...
            }
            
        }

        private void Callback()
        {
            //...
        } 
    }

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

  • C#/VB.NET Plugins version = 7.3.0.77872
  • Visual Studio version = 17.7.2
  • MSBuild / dotnet version =7.0
  • Operating System = Windows 11
@ruben-ivre ruben-ivre changed the title Fix S2583/S2589 FP/FN: Delegate can be null when last invocation is removed but comparing the delegate against null gets reported as always false Fix S2583/S2589 FP: Delegate can be null when last invocation is removed but comparing the delegate against null gets reported as always false Sep 27, 2023
@mary-georgiou-sonarsource
Copy link
Contributor

Hello @ruben-ivre,
I confirm this is a false positive.
I added it to our backlog to tackle in a future sprint.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants