S4275 Getters and setters should access the expected fields

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube - Version 6.7 (build 33306) , SonartLint for VS 2019 4.3)
  • what are you trying to achieve
    to fix s4275 rule
  • what have you tried so far to achieve this
    am using a private filed in get and adding a value in loop through set but not able to fix the rule
    need to know how to refactor it

Code Sample:

private readonly MyCollection _myPrivateField;

public MyCollection Data
    {
        get => _myPrivateField;

        private set (//Am getting S4275 error here)

        {
            foreach (var item in value)
            {
                _myPrivateField.Add(item);                    
            }
        }
    }

MyCollection.cs

public class MyCollection : KeyedCollection<string, MyModel>

MyModel.cs

public int ID {get;set;}
public string Name {get; set;}

Tried changing accessor from private to public, protected and removed readonly as well for private variable but not able to fix the error so far
Please suggest me an approach to handle this violation
Since its a bug sonar quality gate is failing
Awaiting for help

Thanks,
Vara

hi @Varaprasad_yadati this seems to be a False Positive.

You can just mark it as such in your SonarQube instance.

you are using a very old version of SonarQube (which is not supported anymore) and an old version of SonarLint as well (from August 2018!)

We fixed a lot of False Positives for rule S4275 (see the list here : https://github.com/SonarSource/sonar-dotnet/issues?q=is%3Aissue+sort%3Aupdated-desc+S4275+is%3Aclosed) in the past two years

which is the version of the SonarCSharp analyzer you are using?

I highly recommend to update your SonarQube and SonarLint versions to the latest ones - for SQ, if you want stability you can choose SQ 7.9 LTS https://www.sonarqube.org/downloads/

We’re using SonarLint for Visual Studio 2019, version 4.21.0.16909, it looks like installing the latest did not fix, at least locally.

@jbogard could you please provide a minimal reproducer for your problem?

private readonly List<int> _data = new List<int>();
public List<int> Data {
    get => _data;
    private set {
         foreach (var item in value) {
             _data.Add(value);
         }
    }
}

The setter in this case is only executed by a deserializer, but we don’t want to replace the field instance.

Thanks @jbogard . Indeed, if the set is private it seems ok. I’ll open a ticket. https://github.com/SonarSource/sonar-dotnet/issues/3441

Actualy what you doing is using the setter as an Add method, I don’t do that… it`s right the Sonar warning you… that can cause a ruge problem…

Try create an AddRange Method:
private readonly List _data = new List();
public List Data => _data;

public void AddRange(IEnumerable data) {
foreach (var item in value) {
_data.Add(value);
}
}

But even so… you be more easy just doing like that:

public List Data { get; } = new List();

and when you need to set de data use

yourInstance.Data.AddRange(…)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.