diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/AbstractTypesShouldNotHaveConstructors.Records.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/AbstractTypesShouldNotHaveConstructors.Records.cs index feecb4369f0..31f82db0694 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/AbstractTypesShouldNotHaveConstructors.Records.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/AbstractTypesShouldNotHaveConstructors.Records.cs @@ -1,10 +1,4 @@ -// See https://github.com/dotnet/roslyn/issues/45510 -namespace System.Runtime.CompilerServices -{ - public class IsExternalInit { } -} - -namespace Tests.Diagnostics +namespace Tests.Diagnostics { abstract record AbstractRecordOne { diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/CheckArgumentException.CSharp9.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/CheckArgumentException.CSharp9.cs index 72445ac0ced..0d2cf7ee4aa 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/CheckArgumentException.CSharp9.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/CheckArgumentException.CSharp9.cs @@ -1,12 +1,4 @@ -using System; -using System.Collections.Generic; - -namespace System.Runtime.CompilerServices -{ - public class IsExternalInit { } -} - -namespace Tests.Diagnostics +namespace Tests.Diagnostics { record Program { diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ClassNotInstantiatable.CSharp9.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ClassNotInstantiatable.CSharp9.cs index 6c70e322d69..00a75847f59 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ClassNotInstantiatable.CSharp9.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ClassNotInstantiatable.CSharp9.cs @@ -77,9 +77,3 @@ public record WithAttribute2 private WithAttribute2() { } } } - -// See https://github.com/dotnet/roslyn/issues/45510 -namespace System.Runtime.CompilerServices -{ - public class IsExternalInit { } -} diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ClassShouldNotBeAbstract.CSharp9.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ClassShouldNotBeAbstract.CSharp9.cs index 61950e2ccab..e308b3bacc1 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ClassShouldNotBeAbstract.CSharp9.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ClassShouldNotBeAbstract.CSharp9.cs @@ -72,9 +72,3 @@ public abstract record View2 // Compliant, has abstract and non abstract members public abstract record Record(string X); } - -// See https://github.com/dotnet/roslyn/issues/45510 -namespace System.Runtime.CompilerServices -{ - public class IsExternalInit { } -} diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ClassWithEqualityShouldImplementIEquatable.CSharp9.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ClassWithEqualityShouldImplementIEquatable.CSharp9.cs index 9b3dd34ba22..72257354017 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ClassWithEqualityShouldImplementIEquatable.CSharp9.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ClassWithEqualityShouldImplementIEquatable.CSharp9.cs @@ -15,9 +15,3 @@ public bool Equals(C other) return false; } } - -// See https://github.com/dotnet/roslyn/issues/45510 -namespace System.Runtime.CompilerServices -{ - public class IsExternalInit { } -} diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/CognitiveComplexity.CSharp9.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/CognitiveComplexity.CSharp9.cs index 55f30a81775..301b76fd5eb 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/CognitiveComplexity.CSharp9.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/CognitiveComplexity.CSharp9.cs @@ -105,9 +105,3 @@ static void TargetTypedConditional(bool isTrue, Apple[] apples, Fruit[] givenFru Fruit[] fruits3 = givenFruits ?? apples; } } - -// See https://github.com/dotnet/roslyn/issues/45510 -namespace System.Runtime.CompilerServices -{ - public class IsExternalInit { } -} diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/MethodName.CSharp9.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/MethodName.CSharp9.cs index 9aa02f13f0b..3e0ff98e54b 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/MethodName.CSharp9.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/MethodName.CSharp9.cs @@ -1,8 +1,4 @@ -namespace System.Runtime.CompilerServices -{ - public class IsExternalInit { } -} -namespace Tests.Diagnostics +namespace Tests.Diagnostics { public interface IMyInterface { diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/UnusedPrivateMember.CSharp9.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/UnusedPrivateMember.CSharp9.cs index eef9db9fb03..c8bef5ba3d3 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/UnusedPrivateMember.CSharp9.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/UnusedPrivateMember.CSharp9.cs @@ -63,9 +63,3 @@ static void Bar() { } } } - -// See https://github.com/dotnet/roslyn/issues/45510 -namespace System.Runtime.CompilerServices -{ - public class IsExternalInit { } -} diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/UseValueParameter.CSharp9.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/UseValueParameter.CSharp9.cs index a32d083eea1..7f5b7c2ee12 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/UseValueParameter.CSharp9.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/UseValueParameter.CSharp9.cs @@ -21,9 +21,3 @@ public int this[int i] } } } - -// See https://github.com/dotnet/roslyn/issues/45510 -namespace System.Runtime.CompilerServices -{ - public class IsExternalInit { } -} diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/VariableUnused.CSharp9.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/VariableUnused.CSharp9.cs index 1f2aeec2010..e6ac6e71c2a 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/VariableUnused.CSharp9.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/VariableUnused.CSharp9.cs @@ -27,9 +27,3 @@ void Method() string unusedString = string.Empty; // Noncompliant } } - -// See https://github.com/dotnet/roslyn/issues/45510 -namespace System.Runtime.CompilerServices -{ - public class IsExternalInit { } -} diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/SolutionBuilder.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/SolutionBuilder.cs index ee0db5ccae0..59bed837e13 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/SolutionBuilder.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/SolutionBuilder.cs @@ -32,6 +32,7 @@ namespace SonarAnalyzer.UnitTest.TestFramework internal struct SolutionBuilder { private const string GeneratedAssemblyName = "project"; + private const string InitSnippet = @"namespace System.Runtime.CompilerServices { public class IsExternalInit { } } // See https://github.com/dotnet/roslyn/issues/45510"; private Solution Solution { get; } @@ -89,8 +90,9 @@ public static SolutionBuilder Create() => FromSolution(new AdhocWorkspace().CurrentSolution); public static SolutionBuilder CreateSolutionFromPaths(IEnumerable paths, - OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary, - IEnumerable additionalReferences = null) + OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary, + IEnumerable additionalReferences = null, + bool isSupportForCSharp9InitNeeded = false) { if (paths == null || !paths.Any()) @@ -109,6 +111,11 @@ public static SolutionBuilder CreateSolutionFromPaths(IEnumerable paths, .AddDocuments(paths) .AddReferences(additionalReferences); + if (isSupportForCSharp9InitNeeded) + { + project = project.AddSnippet(InitSnippet); + } + if (additionalReferences != null && additionalReferences.Any(r => r.Display.Contains("\\netstandard"))) { diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/Verifier.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/Verifier.cs index 0dcd059d4ee..db01aea4d71 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/Verifier.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestFramework/Verifier.cs @@ -141,7 +141,7 @@ private static void VerifyAnalyzer(IEnumerable paths, SonarDiagnosticAna OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary, IEnumerable additionalReferences = null) { - var solutionBuilder = SolutionBuilder.CreateSolutionFromPaths(paths, outputKind, additionalReferences); + var solutionBuilder = SolutionBuilder.CreateSolutionFromPaths(paths, outputKind, additionalReferences, IsSupportForCSharp9InitNeeded(options)); foreach (var compilation in solutionBuilder.Compile(options?.ToArray())) { @@ -206,5 +206,9 @@ public static void VerifyCodeFix(string path, string pathToExpected, { CodeFixVerifier.VerifyCodeFix(path, pathToExpected, pathToExpected, diagnosticAnalyzer, codeFixProvider, codeFixTitle, options, additionalReferences); } + + private static bool IsSupportForCSharp9InitNeeded(IEnumerable options) => + options != null + && options.OfType().Select(option => option.LanguageVersion).Contains(LanguageVersion.CSharp9); } }