SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
787 stars 227 forks source link

Fix S3900 FP: Parameter re-assignment through coalescing assignment and tuple deconstruction #7091

Open zsolt-kolbay-sonarsource opened 1 year ago

zsolt-kolbay-sonarsource commented 1 year ago

Description

Currently, the S3900 rule does not raise an issue if the parameter of the method is assigned a new value before it's dereferenced:

public void Method(object o)
{
    o = Unknown();
    o.ToString(); // Compliant - o no longer has the value which was passed to the method
}

private object Unkown() => null;

This currently works for simple assignments, but not for coalescing assignments, tuple deconstructions, and when the parameter is passed as ref parameter to another method.

private object Unkown() => null;


- [ ] Passed as ref parameter:
```cs
public void RefParameter(object o)
{
    MethodWithRefParam(ref o);
    o.ToString(); // Noncompliant - FP
}

private void MethodWithRefParam(ref object o) { }

Some of the tests needed are also in #7039

Related information

martin-strecker-sonarsource commented 1 year ago

see also #7039