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
795 stars 228 forks source link

Fix S1172 FP: Raises randomly under VS context #5491

Closed FStolte closed 2 years ago

FStolte commented 2 years ago

Description

S1172 sometimes flags parameters of private methods, regardless of whether they are used or not.

Repro steps

Create a new project with one class

 public class Class1
 {
    public static void Main(string[] args)
    {
       new Class1().DoSth("Test");
    }

    private void DoSth(string param)
    {
       Console.Out.WriteLine(param);
    }
 }

Add <AnalysisMode>AllEnabledByDefault</AnalysisMode> to the project and trigger the code analysis. If the warning does not appear, try removing the AnalysisMode and triggering the analysis again.

Expected behavior

No warning for rule S1172

Actual behavior

Sometimes the parameter param is flagged with S1172 despite being used in the method. This behavior is flaky, i.e. when editing the code, sometimes the warnings disappear again even if the code is the same.

Related information

HakamFostok commented 2 years ago

I am having the exact same problem with the same setup mentioned above, looking forward to being solved. Thank you very much

costin-zaharia-sonarsource commented 2 years ago

This issue was reported also on our community: https://community.sonarsource.com/t/s1172-false-positives-in-visual-studio/60144.

rvanmaanen commented 2 years ago

It would be great if someone could look into this. It's pretty annoying. We have set WarningsAsErrors to true and especially when this S1172 triggers a lot of false positives, it is hard to find an actual error.

NoahStolk commented 2 years ago

Any updates on this? This is still happening on SonarAnalyzer.CSharp 8.37.0.45539.

I'm currently working around the issue using the following line in my .editorconfig files:

dotnet_diagnostic.S1172.severity = none

Would much appreciate a proper fix. 🙂

aKzenT commented 2 years ago

I'm facing the same problem.

Corniel commented 2 years ago

I can confirm this issue. It is not reproducible in a small test, but in bigger solutions is occurs quite often, but is a bit flaky.

Hoaas commented 2 years ago

Also experiencing this on most solutions. Issue is temporarily gone for a single file if the file is changed.

myion commented 2 years ago

Appeared today in my solutions (at least two)

Extension updated today to 6.1.0.45943

DreadLordMikey commented 2 years ago

We are experiencing it at my company as well. It appears that neither solution size nor project type is a contributing factor (we build just about everything an enterprise operations division would, excluding mobile applications).

Disabling the rule is less than ideal: We need to know when parameters should be removed (we're cleaning up legacy code), but can't trust the linter to tell us the truth.

pavel-mikula-sonarsource commented 2 years ago

Hi, we're investigating the issue.

pavel-mikula-sonarsource commented 2 years ago

As reported by @tonto7973 in #5553

Description

S1172 unused parameter reported in VS 2022 when the parameter is used. This is coming from Visual Studio IntelliSense only. Running a build does not produce these errors.

Repro steps

Sample code

/*
Warning S1172 Remove this parameter 'value', whose value is ignored in the method.
Unused parameters are misleading. Whatever the values passed to such parameters, the behavior will be the same.
*/
private static dynamic ToDynamic(object value)
{
    var json = JsonConvert.SerializeObject(value); // <-- parameter 'value' used here
    return JsonConvert.DeserializeObject(json, typeof(ExpandoObject));
}
pavel-mikula-sonarsource commented 2 years ago

We've identified the root cause of the problem, fix will follow

HolisticDeveloper commented 2 years ago

@pavel-mikula-sonarsource Glad to see a fix is forthcoming. Do you have an estimated date for releasing an update?

pavel-mikula-sonarsource commented 2 years ago

We expect to release analyzers next week. That will fix scenarios when our NuGet is consumed. I can't tell when the next SonarLint version will be released.

DominikAmon commented 2 years ago

We expect to release analyzers next week. That will fix scenarios when our NuGet is consumed.

Great!

I can't tell when the next SonarLint version will be released.

Please prioritize it, when possible. Having hundreds of false-positives all over my code and I always have to filter them out.

Thx!

pavel-mikula-sonarsource commented 2 years ago

.NET Analyzers were just released. You can now follow this ticket on SonarLint side: https://github.com/SonarSource/sonarlint-visualstudio/issues/2989