diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/ExceptionShouldNotBeThrownFromUnexpectedMethodsTest.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/ExceptionShouldNotBeThrownFromUnexpectedMethodsTest.cs index 532fb9528a1..5900ed71c8d 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/ExceptionShouldNotBeThrownFromUnexpectedMethodsTest.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/ExceptionShouldNotBeThrownFromUnexpectedMethodsTest.cs @@ -1,4 +1,4 @@ -/* +/* * SonarAnalyzer for .NET * Copyright (C) 2015-2020 SonarSource SA * mailto: contact AT sonarsource DOT com @@ -30,11 +30,17 @@ public class ExceptionShouldNotBeThrownFromUnexpectedMethodsTest { [TestMethod] [TestCategory("Rule")] - public void ExceptionShouldNotBeThrownFromUnexpectedMethods() - { + public void ExceptionShouldNotBeThrownFromUnexpectedMethods() => Verifier.VerifyAnalyzer(@"TestCases\ExceptionShouldNotBeThrownFromUnexpectedMethods.cs", new ExceptionShouldNotBeThrownFromUnexpectedMethods(), ParseOptionsHelper.FromCSharp8); - } + #if NET5_0 + [TestMethod] + [TestCategory("Rule")] + public void ExceptionShouldNotBeThrownFromUnexpectedMethods_CSharp9() => + Verifier.VerifyAnalyzerFromCSharp9Library(@"TestCases\ExceptionShouldNotBeThrownFromUnexpectedMethods.CSharp9.cs", + new ExceptionShouldNotBeThrownFromUnexpectedMethods()); +#endif + } } diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/ExpressionComplexityTest.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/ExpressionComplexityTest.cs index d1c2c26934f..6dd3925fb47 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/ExpressionComplexityTest.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/ExpressionComplexityTest.cs @@ -1,4 +1,4 @@ -/* +/* * SonarAnalyzer for .NET * Copyright (C) 2015-2020 SonarSource SA * mailto: contact AT sonarsource DOT com @@ -28,20 +28,21 @@ public class ExpressionComplexityTest { [TestMethod] [TestCategory("Rule")] - public void ExpressionComplexity_CSharp() - { - var diagnostic = new SonarAnalyzer.Rules.CSharp.ExpressionComplexity { Maximum = 3}; + public void ExpressionComplexity_CSharp() => Verifier.VerifyAnalyzer(@"TestCases\ExpressionComplexity.cs", - diagnostic, + new SonarAnalyzer.Rules.CSharp.ExpressionComplexity { Maximum = 3}, options: ParseOptionsHelper.FromCSharp8); - } [TestMethod] [TestCategory("Rule")] - public void ExpressionComplexity_VisualBasic() - { - var diagnostic = new SonarAnalyzer.Rules.VisualBasic.ExpressionComplexity { Maximum = 3 }; - Verifier.VerifyAnalyzer(@"TestCases\ExpressionComplexity.vb", diagnostic); - } + public void ExpressionComplexity_CSharp9() => + Verifier.VerifyAnalyzerFromCSharp9Console(@"TestCases\ExpressionComplexity.CSharp9.cs", + new SonarAnalyzer.Rules.CSharp.ExpressionComplexity { Maximum = 3}); + + [TestMethod] + [TestCategory("Rule")] + public void ExpressionComplexity_VisualBasic() => + Verifier.VerifyAnalyzer(@"TestCases\ExpressionComplexity.vb", + new SonarAnalyzer.Rules.VisualBasic.ExpressionComplexity { Maximum = 3 }); } } diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/ExtensionMethodShouldBeInSeparateNamespaceTest.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/ExtensionMethodShouldBeInSeparateNamespaceTest.cs index 9bdc0c99dcb..07dc5af7c1c 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/ExtensionMethodShouldBeInSeparateNamespaceTest.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/Rules/ExtensionMethodShouldBeInSeparateNamespaceTest.cs @@ -30,10 +30,15 @@ public class ExtensionMethodShouldBeInSeparateNamespaceTest { [TestMethod] [TestCategory("Rule")] - public void ExtensionMethodShouldBeInSeparateNamespace() - { + public void ExtensionMethodShouldBeInSeparateNamespace() => Verifier.VerifyAnalyzer(@"TestCases\ExtensionMethodShouldBeInSeparateNamespace.cs", new ExtensionMethodShouldBeInSeparateNamespace()); - } + + [TestMethod] + [TestCategory("Rule")] + public void ExtensionMethodShouldBeInSeparateNamespace_CSharp9() => + Verifier.VerifyAnalyzerFromCSharp9Console(@"TestCases\ExtensionMethodShouldBeInSeparateNamespace.CSharp9.cs", + new ExtensionMethodShouldBeInSeparateNamespace()); + } } diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExceptionShouldNotBeThrownFromUnexpectedMethods.CSharp9.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExceptionShouldNotBeThrownFromUnexpectedMethods.CSharp9.cs new file mode 100644 index 00000000000..4bd50a22715 --- /dev/null +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExceptionShouldNotBeThrownFromUnexpectedMethods.CSharp9.cs @@ -0,0 +1,47 @@ +using System; +using System.Runtime.CompilerServices; + +namespace Tests.Diagnostics +{ + record Record1 : IDisposable + { + static Record1() => throw new NotImplementedException(); + public void Dispose() + { + throw new NotImplementedException(); + } + } + + record Record2 : IDisposable + { + static Record2() => throw new Exception(); // FN, needs support for throw expressions + + [ModuleInitializer] + internal static void M1() + { + throw new Exception(); // FN + } + + event EventHandler OnSomething + { + add + { + throw new Exception(); // Noncompliant + } + remove + { + throw new InvalidOperationException(); // Compliant + } + } + + public override string ToString() + { + throw new Exception(); // Noncompliant + } + + public void Dispose() + { + throw new Exception(); // Noncompliant + } + } +} diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExceptionShouldNotBeThrownFromUnexpectedMethods.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExceptionShouldNotBeThrownFromUnexpectedMethods.cs index 3da8383eff9..b704426686e 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExceptionShouldNotBeThrownFromUnexpectedMethods.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExceptionShouldNotBeThrownFromUnexpectedMethods.cs @@ -262,4 +262,10 @@ public void Dispose() throw new Exception(); // Noncompliant } } + + class ArrowMethods : IDisposable + { + static ArrowMethods() => throw new Exception(); // FN, needs support for throw expressions + public void Dispose() => throw new Exception(); // FN + } } diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExpressionComplexity.CSharp9.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExpressionComplexity.CSharp9.cs new file mode 100644 index 00000000000..d24bc8498e3 --- /dev/null +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExpressionComplexity.CSharp9.cs @@ -0,0 +1,11 @@ +var d1 = true && false && true && false && true && true; // Noncompliant + +object x = null; + +if (x is true or true or true or true or true) { } // FN + +if (x is true and true and true and true and true) { } // FN + +if (x is 10 or 20 or 40 or 50 or 60 or 70) { } // FN + +if (x is < 10 or < 20 or < 30 and (40 or 50 or 60)) { } // FN diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExtensionMethodShouldBeInSeparateNamespace.CSharp9.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExtensionMethodShouldBeInSeparateNamespace.CSharp9.cs new file mode 100644 index 00000000000..fb48e876e5d --- /dev/null +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExtensionMethodShouldBeInSeparateNamespace.CSharp9.cs @@ -0,0 +1,29 @@ +var isTopLevelFile = true; + +public record GlobalRecord { } + +public static class GlobalExtensions +{ + public static void Bar(this GlobalRecord r) { } // Noncompliant +} + +namespace MyLibrary +{ + public record Foo { } +} +namespace Helpers +{ + public static class MyExtensions + { + public static void Bar(this MyLibrary.Foo foo) { } + public static void Bar(this GlobalRecord foo) { } + } +} +namespace Same +{ + public record R { } + public static class Extensions + { + public static void Bar(this R r) { } // Noncompliant + } +}