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
798 stars 229 forks source link

Fix S1215 FN: `GC.GetTotalMemory(forceFullCollection: true)` should not be called #9687

Open fiotti opened 1 month ago

fiotti commented 1 month ago

Description

GC.GetTotalMemory(forceFullCollection: true) calls GC.Collect() internally.

It might make sense to consider it equivalent to GC.Collect() and trigger a S1215.

Repro steps

GC.Collect(); // S1215: Refactor the code to remove this use of 'GC.Collect'.

GC.GetTotalMemory(true); // No warning.

Expected behavior

GC.Collect(); // S1215: Refactor the code to remove this use of 'GC.Collect'.

GC.GetTotalMemory(true); // S1215: Refactor the code to remove this use of 'GC.GetTotalMemory', or do not set 'forceFullCollection' to true.

Actual behavior

No warning.

Known workarounds

None.

Related information

mary-georgiou-sonarsource commented 1 month ago

Hello @fiotti. Thanks for reporting this. Indeed that's what I also see in the source code . I'll add this to our backlog.