SonarQube incorrectly indicating bug which is not actually

Hi Team,

This is about the bug:
https://sonar-use-prod.cicd.spglobal.com/organizations/default-organization/rules?open=csharpsquid%3AS4143&rule_key=csharpsquid%3AS4143

The explanation is valid but it is invalid in the following senario:

  1. var RatingEntity = new DbEntity();
  2. RatingEntity.EntityFields.Add(“prop1”, “Vijay”);
  3. var dbEntity = new DbEntity();
  4. dbEntity.EntityFields.Add(“prop1”, “Deepak”);

Here SonarQube is showing bug on line number 4, because we have set the value for prop1 in line number 2.
But it should not be a bug because they are assigned to diffrent variables.

Hope I am able to explain the problem.

hi @DeepakVijay and welcome to our community!

the link you gave does not work (server not found)

can you please tell us what rule are you talking? and also the version of sonar-csharp you are using?

@Andrei_Epure Apologies about the URL

The rule I am talking about is:
=> S4143 : Collection elements should not be replaced unconditionally.

I am afraid I cannot reproduce with the latest version (SonarCSharp 7.16).

What version of SonarCSharp are you using? Can you give more context on the used classes?

Here is my code snippet for reproducing…

    public class TestSameClassMultipleInstance
    {
        public class MyContainer
        {
            public Dictionary<string, string> dictionaryField;
            public static Dictionary<string, string> publicStaticDictionaryField = new Dictionary<string, string>();
            private static Dictionary<string, string> staticDictionary = new Dictionary<string, string>();
            public Dictionary<string, string> StaticDictionary
            {
                get => staticDictionary;
            }
            public MyContainer()
            {
                dictionaryField = new Dictionary<string, string>();
            }
        }

        public void Foo()
        {
            var dict1 = new MyContainer();
            var dict2 = new MyContainer();

            dict1.dictionaryField.Add("prop1", "1");
            dict2.dictionaryField.Add("prop1", "2"); // no issue raised

            dict1.StaticDictionary.Add("static", "a");
            dict2.StaticDictionary.Add("static", "b"); // no issue raised (false negative, we don't realize it's the same instance because of the property)

            MyContainer.publicStaticDictionaryField.Add("x", "x");
            MyContainer.publicStaticDictionaryField.Add("x", "x1"); // Noncompliant - issue is raised
        }
    }

Thank you @Andrei_Epure for your help.
Updating the SonarCSharp Analyzer resolved the issue.