S4275 warning is not on get

This is a very silly and insignificant problem, but I’d still rather post it

  • Operating system: Windows 11
  • Visual Studio version: 17.14.17
  • SonarQube for Visual Studio plugin version: 8.29.0.14599
  • Programming language you’re coding in: C#

And a thorough description of the problem / question:

In the following code :

class Foo
{
    private string _test;

    public string Test
    {
        get
        {
            string a = string.Empty;
            return a;
        }
        set
        {
            _test = "b";
        }
    }
}

The S4275 warning highlights correctly the set keyword, but for the getter, Empty is highlighted instead of get :

The problem seems to leave when I remove string.Empty

Hi @Gameplushy,

Thanks for the report!

I agree this is confusing, the intention is for this to raise on the field when it is the wrong field e.g. this code will raise on someOtherField for both the setter and getter.

class Foo{
  private string _test;
  private string someOtherField;
  public string Test{
    get
    {
      string a = someOtherField;
      return a;
    }
    set
    {
      someOtherField = value;
    }
  }
}

However I agree that this is confusing when the field is on another type, like in the case of string.Empty.

I will add a ticket to our backlog to improve the location of this rule.