diff --git a/sonaranalyzer-dotnet/its/expected/Nancy/Nancy-{34576216-0DCA-4B0F-A0DC-9075E75A676F}-S4226.json b/sonaranalyzer-dotnet/its/expected/Nancy/Nancy-{34576216-0DCA-4B0F-A0DC-9075E75A676F}-S4226.json index 4b4adb77c5c..ec02798d210 100644 --- a/sonaranalyzer-dotnet/its/expected/Nancy/Nancy-{34576216-0DCA-4B0F-A0DC-9075E75A676F}-S4226.json +++ b/sonaranalyzer-dotnet/its/expected/Nancy/Nancy-{34576216-0DCA-4B0F-A0DC-9075E75A676F}-S4226.json @@ -4,71 +4,6 @@ "id": "S4226", "message": "Either move this extension to another namespace or move the method inside the type itself.", "location": { -"uri": "sources\Nancy\src\Nancy\Configuration\INancyEnvironmentExtensions.cs", -"region": { -"startLine": 16, -"startColumn": 28, -"endLine": 16, -"endColumn": 36 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\Configuration\INancyEnvironmentExtensions.cs", -"region": { -"startLine": 27, -"startColumn": 25, -"endLine": 27, -"endColumn": 33 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\Configuration\INancyEnvironmentExtensions.cs", -"region": { -"startLine": 39, -"startColumn": 25, -"endLine": 39, -"endColumn": 33 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\Configuration\INancyEnvironmentExtensions.cs", -"region": { -"startLine": 52, -"startColumn": 25, -"endLine": 52, -"endColumn": 44 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\Configuration\INancyEnvironmentExtensions.cs", -"region": { -"startLine": 67, -"startColumn": 25, -"endLine": 67, -"endColumn": 44 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { "uri": "sources\Nancy\src\Nancy\Conventions\StaticContentHelper.cs", "region": { "startLine": 22, @@ -82,162 +17,6 @@ "id": "S4226", "message": "Either move this extension to another namespace or move the method inside the type itself.", "location": { -"uri": "sources\Nancy\src\Nancy\FormatterExtensions.cs", -"region": { -"startLine": 25, -"startColumn": 32, -"endLine": 25, -"endColumn": 38 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\FormatterExtensions.cs", -"region": { -"startLine": 37, -"startColumn": 32, -"endLine": 37, -"endColumn": 38 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\FormatterExtensions.cs", -"region": { -"startLine": 51, -"startColumn": 32, -"endLine": 51, -"endColumn": 38 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\FormatterExtensions.cs", -"region": { -"startLine": 64, -"startColumn": 32, -"endLine": 64, -"endColumn": 38 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\FormatterExtensions.cs", -"region": { -"startLine": 76, -"startColumn": 32, -"endLine": 76, -"endColumn": 38 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\FormatterExtensions.cs", -"region": { -"startLine": 86, -"startColumn": 32, -"endLine": 86, -"endColumn": 38 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\FormatterExtensions.cs", -"region": { -"startLine": 99, -"startColumn": 32, -"endLine": 99, -"endColumn": 38 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\FormatterExtensions.cs", -"region": { -"startLine": 115, -"startColumn": 32, -"endLine": 115, -"endColumn": 42 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\FormatterExtensions.cs", -"region": { -"startLine": 128, -"startColumn": 32, -"endLine": 128, -"endColumn": 37 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\FormatterExtensions.cs", -"region": { -"startLine": 142, -"startColumn": 32, -"endLine": 142, -"endColumn": 42 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\FormatterExtensions.cs", -"region": { -"startLine": 154, -"startColumn": 32, -"endLine": 154, -"endColumn": 42 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\Nancy\src\Nancy\NancyEngineExtensions.cs", -"region": { -"startLine": 14, -"startColumn": 42, -"endLine": 14, -"endColumn": 55 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { "uri": "sources\Nancy\src\Nancy\Owin\NancyOptionsExtensions.cs", "region": { "startLine": 16, diff --git a/sonaranalyzer-dotnet/its/expected/akka.net/Akka-{5DEDDF90-37F0-48D3-A0B0-A5CBD8A7E377}-S4226.json b/sonaranalyzer-dotnet/its/expected/akka.net/Akka-{5DEDDF90-37F0-48D3-A0B0-A5CBD8A7E377}-S4226.json index 2deb6b06f7e..dcfdfbe47e1 100644 --- a/sonaranalyzer-dotnet/its/expected/akka.net/Akka-{5DEDDF90-37F0-48D3-A0B0-A5CBD8A7E377}-S4226.json +++ b/sonaranalyzer-dotnet/its/expected/akka.net/Akka-{5DEDDF90-37F0-48D3-A0B0-A5CBD8A7E377}-S4226.json @@ -4,71 +4,6 @@ "id": "S4226", "message": "Either move this extension to another namespace or move the method inside the type itself.", "location": { -"uri": "sources\akka.net\src\core\Akka\Actor\ActorRef.cs", -"region": { -"startLine": 122, -"startColumn": 28, -"endLine": 122, -"endColumn": 32 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\ActorRef.cs", -"region": { -"startLine": 134, -"startColumn": 28, -"endLine": 134, -"endColumn": 35 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\ActorRef.Extensions.cs", -"region": { -"startLine": 20, -"startColumn": 28, -"endLine": 20, -"endColumn": 36 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\ActorRefFactoryExtensions.cs", -"region": { -"startLine": 12, -"startColumn": 33, -"endLine": 12, -"endColumn": 40 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Cancellation\CancelableExtensions.cs", -"region": { -"startLine": 16, -"startColumn": 28, -"endLine": 16, -"endColumn": 43 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { "uri": "sources\akka.net\src\core\Akka\Actor\Extensions.cs", "region": { "startLine": 74, @@ -108,357 +43,6 @@ "id": "S4226", "message": "Either move this extension to another namespace or move the method inside the type itself.", "location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Futures.cs", -"region": { -"startLine": 27, -"startColumn": 36, -"endLine": 27, -"endColumn": 39 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Futures.cs", -"region": { -"startLine": 32, -"startColumn": 31, -"endLine": 32, -"endColumn": 34 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\GracefulStopSupport.cs", -"region": { -"startLine": 37, -"startColumn": 34, -"endLine": 37, -"endColumn": 46 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\GracefulStopSupport.cs", -"region": { -"startLine": 42, -"startColumn": 34, -"endLine": 42, -"endColumn": 46 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\DeprecatedSchedulerExtensions.cs", -"region": { -"startLine": 17, -"startColumn": 28, -"endLine": 17, -"endColumn": 40 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\DeprecatedSchedulerExtensions.cs", -"region": { -"startLine": 24, -"startColumn": 28, -"endLine": 24, -"endColumn": 40 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\DeprecatedSchedulerExtensions.cs", -"region": { -"startLine": 31, -"startColumn": 28, -"endLine": 31, -"endColumn": 36 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\DeprecatedSchedulerExtensions.cs", -"region": { -"startLine": 39, -"startColumn": 28, -"endLine": 39, -"endColumn": 36 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\DeprecatedSchedulerExtensions.cs", -"region": { -"startLine": 49, -"startColumn": 28, -"endLine": 49, -"endColumn": 36 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\DeprecatedSchedulerExtensions.cs", -"region": { -"startLine": 55, -"startColumn": 28, -"endLine": 55, -"endColumn": 36 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\DeprecatedSchedulerExtensions.cs", -"region": { -"startLine": 64, -"startColumn": 28, -"endLine": 64, -"endColumn": 40 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\DeprecatedSchedulerExtensions.cs", -"region": { -"startLine": 70, -"startColumn": 28, -"endLine": 70, -"endColumn": 40 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\SchedulerExtensions.cs", -"region": { -"startLine": 21, -"startColumn": 28, -"endLine": 21, -"endColumn": 44 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\SchedulerExtensions.cs", -"region": { -"startLine": 35, -"startColumn": 28, -"endLine": 35, -"endColumn": 50 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\SchedulerExtensions.cs", -"region": { -"startLine": 49, -"startColumn": 28, -"endLine": 49, -"endColumn": 40 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\SchedulerExtensions.cs", -"region": { -"startLine": 64, -"startColumn": 28, -"endLine": 64, -"endColumn": 46 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\SchedulerExtensions.cs", -"region": { -"startLine": 78, -"startColumn": 35, -"endLine": 78, -"endColumn": 61 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\SchedulerExtensions.cs", -"region": { -"startLine": 92, -"startColumn": 35, -"endLine": 92, -"endColumn": 61 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\SchedulerExtensions.cs", -"region": { -"startLine": 107, -"startColumn": 35, -"endLine": 107, -"endColumn": 67 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\SchedulerExtensions.cs", -"region": { -"startLine": 122, -"startColumn": 35, -"endLine": 122, -"endColumn": 67 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\SchedulerExtensions.cs", -"region": { -"startLine": 138, -"startColumn": 35, -"endLine": 138, -"endColumn": 57 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\SchedulerExtensions.cs", -"region": { -"startLine": 152, -"startColumn": 35, -"endLine": 152, -"endColumn": 57 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\SchedulerExtensions.cs", -"region": { -"startLine": 169, -"startColumn": 35, -"endLine": 169, -"endColumn": 63 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Scheduler\SchedulerExtensions.cs", -"region": { -"startLine": 184, -"startColumn": 35, -"endLine": 184, -"endColumn": 63 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Stash\StashFactory.cs", -"region": { -"startLine": 19, -"startColumn": 30, -"endLine": 19, -"endColumn": 41 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Stash\StashFactory.cs", -"region": { -"startLine": 25, -"startColumn": 30, -"endLine": 25, -"endColumn": 41 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka\Actor\Stash\StashFactory.cs", -"region": { -"startLine": 30, -"startColumn": 30, -"endLine": 30, -"endColumn": 41 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { "uri": "sources\akka.net\src\core\Akka\Actor\SupervisorStrategy.cs", "region": { "startLine": 627, @@ -576,19 +160,6 @@ "id": "S4226", "message": "Either move this extension to another namespace or move the method inside the type itself.", "location": { -"uri": "sources\akka.net\src\core\Akka\Util\Internal\Collections\IImmutableSet.cs", -"region": { -"startLine": 45, -"startColumn": 39, -"endLine": 45, -"endColumn": 45 -} -} -}, -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { "uri": "sources\akka.net\src\core\Akka\Util\Internal\Extensions.cs", "region": { "startLine": 67, diff --git a/sonaranalyzer-dotnet/its/expected/akka.net/Akka.Cluster.Sharding-{A05C31E8-0246-46A1-B3BC-4D6FE7A9AA49}-S4226.json b/sonaranalyzer-dotnet/its/expected/akka.net/Akka.Cluster.Sharding-{A05C31E8-0246-46A1-B3BC-4D6FE7A9AA49}-S4226.json deleted file mode 100644 index 136e7086945..00000000000 --- a/sonaranalyzer-dotnet/its/expected/akka.net/Akka.Cluster.Sharding-{A05C31E8-0246-46A1-B3BC-4D6FE7A9AA49}-S4226.json +++ /dev/null @@ -1,17 +0,0 @@ -{ -"issues": [ -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\contrib\cluster\Akka.Cluster.Sharding\ClusterSharding.cs", -"region": { -"startLine": 489, -"startColumn": 35, -"endLine": 489, -"endColumn": 48 -} -} -} -] -} diff --git a/sonaranalyzer-dotnet/its/expected/akka.net/Akka.Persistence.Tests-{4492004A-A8D0-45B0-BACC-05BA2F38EC55}-S4226.json b/sonaranalyzer-dotnet/its/expected/akka.net/Akka.Persistence.Tests-{4492004A-A8D0-45B0-BACC-05BA2F38EC55}-S4226.json deleted file mode 100644 index 7ead541fa31..00000000000 --- a/sonaranalyzer-dotnet/its/expected/akka.net/Akka.Persistence.Tests-{4492004A-A8D0-45B0-BACC-05BA2F38EC55}-S4226.json +++ /dev/null @@ -1,17 +0,0 @@ -{ -"issues": [ -{ -"id": "S4226", -"message": "Either move this extension to another namespace or move the method inside the type itself.", -"location": { -"uri": "sources\akka.net\src\core\Akka.Persistence.Tests\AtLeastOnceDeliveryFailureSpec.cs", -"region": { -"startLine": 23, -"startColumn": 30, -"endLine": 23, -"endColumn": 33 -} -} -} -] -} diff --git a/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/ExtensionMethodShouldBeInSeparateNamespace.cs b/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/ExtensionMethodShouldBeInSeparateNamespace.cs index eeeef7a1098..370ada4df63 100644 --- a/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/ExtensionMethodShouldBeInSeparateNamespace.cs +++ b/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/ExtensionMethodShouldBeInSeparateNamespace.cs @@ -52,6 +52,7 @@ protected override void Initialize(SonarAnalysisContext context) methodSymbol.IsExtensionMethod && methodSymbol.Parameters.Length > 0 && methodSymbol.Parameters[0].Type.Kind != SymbolKind.ErrorType && + methodSymbol.Parameters[0].Type.IsClass() && methodSymbol.ContainingNamespace == methodSymbol.Parameters[0].Type.ContainingNamespace) { c.ReportDiagnosticWhenActive(Diagnostic.Create(rule, methodDeclaration.Identifier.GetLocation())); diff --git a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExtensionMethodShouldBeInSeparateNamespace.cs b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExtensionMethodShouldBeInSeparateNamespace.cs index 45b0971f1b3..8d3ee8f9566 100644 --- a/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExtensionMethodShouldBeInSeparateNamespace.cs +++ b/sonaranalyzer-dotnet/tests/SonarAnalyzer.UnitTest/TestCases/ExtensionMethodShouldBeInSeparateNamespace.cs @@ -48,4 +48,34 @@ static void Baz(this SomeNonExistingClass snec) { } } -} \ No newline at end of file +} + +namespace OtherPackageNamespace +{ + public interface IFoo { } + + public enum FooBar { } + + public struct FooQux { } + + public static class FooHelpers + { + public static void FooInterface(this IFoo foo) // Compliant + { + } + + public static void FooEnum(this FooBar foo) // Compliant + { + } + + public static void FooStruct(this FooQux foo) // Compliant + { + } + + public static T FooGeneric(this T foo) // Compliant + { + return default(T); + } + } + +}