dotnet / roslyn-analyzers

MIT License
1.59k stars 466 forks source link

CA1806 does not produce error #7202

Open AFaurholt opened 8 months ago

AFaurholt commented 8 months ago

Analyzer

Diagnostic ID: CA1806: Do not ignore method results

Analyzer source

SDK: Built-in CA analyzers in .NET 8 SDK

Version: SDK 8.0.102

AND TESTED WITH

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 8.0.0

Describe the bug

When enabling CA1806 as Error in my .editorconfig, the following code does not produce an error:

public int Test()
{
    return 0;
}

public void TestTest()
{
    Test();
}

Steps To Reproduce

Expected behavior

Error for not using returned value

Actual behavior

No error

Additional context

I have tried with both the NuGet package and the built-in analyzer in the SDK. I have attempted to build via Visual Studio and via the dotnet CLI, but no error in either.

CollinAlpert commented 8 months ago

This analyzer only emits diagnostics for specific constructs. From the documentation:

  • A new object is created but never used.
  • A method that creates and returns a new string is called and the new string is never used.
  • A COM or P/Invoke method that returns a HRESULT or error code that's never used.
  • A language-integrated query (LINQ) method that returns a result that's never used.

The analyzer only targets constructs which are known not to cause side-effects. Maybe your Test() method performs additional logic and the return value can safely be ignored. This is not something the analyzer can know.

If you want your own methods to be included by the analyzer, you can configure it as described in the documentation.