Sonar-dotnet: `VarPattern` not supported in the analyzer (when building with MSBuild 16)

Created on 15 May 2019  路  5Comments  路  Source: SonarSource/sonar-dotnet

Description

Error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.EmptyNullableValueAccess' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.

Repro steps

Compile the following with SonarAnalyzer.CSharp nuget package installed:

using System;
public class C {
    public int M<T>() {
        switch (typeof(T))
        {
            case var x when x == typeof(Foo): return 1;
            case var x when x == typeof(Bar): return 2;
            default: throw new InvalidOperationException();
        }
    }
}

public class Foo {}
public class Bar {}
  • SonarC# Version: 7.14.0.8411
  • Visual Studio Version: VS 2019 16.0.3
C# CFG Bug

Most helpful comment

@andrei-epure-sonarsource Hello.

I've attached the project that fails on my local machine VS 2019 16.0.3 installed.

More precisely, the build fails on MSBuild 16. In my case it is situated in C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe.

MSBuild 15+ works with no issues.

The list of errors inside the repro solution on my local machine:

  CSC: error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.ConditionEvaluatesToConstant' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
  CSC: error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.EmptyCollectionsShouldNotBeEnumerated' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
  CSC: error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.EmptyNullableValueAccess' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
  CSC: error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.InvalidCastToInterface' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
  CSC: error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.NullPointerDereference' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
  CSC: error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.ObjectsShouldNotBeDisposedMoreThanOnce' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.

Source code on which the issue could be reproduced -
SonarIssueRepro.zip

All 5 comments

Hi @YairHalberstadt . I'm trying to reproduce the issue but cannot. Could you please tell me what version of the .NET framework you are using?

@andrei-epure-sonarsource Hello.

I've attached the project that fails on my local machine VS 2019 16.0.3 installed.

More precisely, the build fails on MSBuild 16. In my case it is situated in C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe.

MSBuild 15+ works with no issues.

The list of errors inside the repro solution on my local machine:

  CSC: error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.ConditionEvaluatesToConstant' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
  CSC: error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.EmptyCollectionsShouldNotBeEnumerated' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
  CSC: error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.EmptyNullableValueAccess' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
  CSC: error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.InvalidCastToInterface' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
  CSC: error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.NullPointerDereference' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
  CSC: error AD0001: Analyzer 'SonarAnalyzer.Rules.CSharp.ObjectsShouldNotBeDisposedMoreThanOnce' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.

Source code on which the issue could be reproduced -
SonarIssueRepro.zip

For everyone, who observes this blocking behaviour. A quick fix is to modify csproj file and add the following lines inside:

    <PropertyGroup>
      <NoWarn>AD0001</NoWarn>
    </PropertyGroup>

I have the same problem when compiling Roslyn.

'SonarAnalyzer.Rules.CSharp.NullPointerDereference' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
'SonarAnalyzer.Rules.CSharp.EmptyCollectionsShouldNotBeEnumerated' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
'SonarAnalyzer.Rules.CSharp.EmptyNullableValueAccess' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
'SonarAnalyzer.Rules.CSharp.InvalidCastToInterface' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
'SonarAnalyzer.Rules.CSharp.ConditionEvaluatesToConstant' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.
'SonarAnalyzer.Rules.CSharp.ObjectsShouldNotBeDisposedMoreThanOnce' threw an exception of type 'System.NotSupportedException' with message 'VarPattern'.

Ok, I found the problem. Previously, var x would be a DeclarationPattern (in MSBuild 15)

image

In the new Roslyn version which gets bundled in MSBuild 16, it becomes a VarPattern which we don't support yet (as we don't yet support C# 8 syntax; even if this is not C#8, it's a breaking change which comes bundled with the new C# 8 syntax).

This problem is also tied to the fact that we don't run any tests with MSBuild 16 (#2489)

Was this page helpful?
0 / 5 - 0 ratings