Skip to content

Commit

Permalink
Add repro for #5457
Browse files Browse the repository at this point in the history
  • Loading branch information
costin-zaharia-sonarsource committed Mar 14, 2022
1 parent 951b238 commit dfbbe60
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ namespace SonarAnalyzer.Rules.CSharp
public sealed class ExtensionMethodShouldBeInSeparateNamespace : SonarDiagnosticAnalyzer
{
private const string DiagnosticId = "S4226";
private const string MessageFormat = "Either move this extension to another namespace or move the method " +
"inside the class itself.";
private const string MessageFormat = "Either move this extension to another namespace or move the method inside the class itself.";

private static readonly DiagnosticDescriptor Rule =
DiagnosticDescriptorBuilder.GetDescriptor(DiagnosticId, MessageFormat, RspecStrings.ResourceManager);
private static readonly DiagnosticDescriptor Rule = DiagnosticDescriptorBuilder.GetDescriptor(DiagnosticId, MessageFormat, RspecStrings.ResourceManager);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);

protected override void Initialize(SonarAnalysisContext context) =>
Expand All @@ -46,9 +44,7 @@ protected override void Initialize(SonarAnalysisContext context) =>
var methodDeclaration = (MethodDeclarationSyntax)c.Node;

if (methodDeclaration.IsExtensionMethod()
&& c.SemanticModel.GetDeclaredSymbol(methodDeclaration) is { } methodSymbol
&& methodSymbol.IsExtensionMethod
&& methodSymbol.Parameters.Length > 0
&& c.SemanticModel.GetDeclaredSymbol(methodDeclaration) is { IsExtensionMethod: true, Parameters: { Length: > 0 } } methodSymbol
&& methodSymbol.Parameters[0].Type.Kind != SymbolKind.ErrorType
&& methodSymbol.Parameters[0].Type.IsClass()
&& methodSymbol.ContainingNamespace.Equals(methodSymbol.Parameters[0].Type.ContainingNamespace))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,29 @@ namespace SonarAnalyzer.UnitTest.Rules
[TestClass]
public class ExtensionMethodShouldBeInSeparateNamespaceTest
{
private readonly VerifierBuilder builder = new VerifierBuilder<ExtensionMethodShouldBeInSeparateNamespace>();

[TestMethod]
public void ExtensionMethodShouldBeInSeparateNamespace() =>
OldVerifier.VerifyAnalyzer(@"TestCases\ExtensionMethodShouldBeInSeparateNamespace.cs", new ExtensionMethodShouldBeInSeparateNamespace());
builder
.AddPaths("ExtensionMethodShouldBeInSeparateNamespace.cs", "ExtensionMethodShouldBeInSeparateNamespace.GeneratedCode.cs")
.Verify();

#if NET

[TestMethod]
public void ExtensionMethodShouldBeInSeparateNamespace_CSharp9() =>
OldVerifier.VerifyAnalyzerFromCSharp9Console(@"TestCases\ExtensionMethodShouldBeInSeparateNamespace.CSharp9.cs", new ExtensionMethodShouldBeInSeparateNamespace());
builder.AddPaths("ExtensionMethodShouldBeInSeparateNamespace.CSharp9.cs").WithTopLevelStatements().Verify();

[TestMethod]
public void ExtensionMethodShouldBeInSeparateNamespace_CSharp10() =>
OldVerifier.VerifyAnalyzerFromCSharp10Library(@"TestCases\ExtensionMethodShouldBeInSeparateNamespace.CSharp10.cs", new ExtensionMethodShouldBeInSeparateNamespace());
builder
.AddPaths("ExtensionMethodShouldBeInSeparateNamespace.CSharp10.cs")
.WithConcurrentAnalysis(false)
.WithOptions(ParseOptionsHelper.FromCSharp10)
.Verify();

#endif

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
public abstract partial class GenClass
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,8 @@ public static T FooGeneric<T>(this T foo) // Compliant
}

}

internal static class GenClassExtensions
{
public static void SetSyncLaterError(this GenClass foo) { } // Noncompliant - FP, see: https://github.com/SonarSource/sonar-dotnet/issues/5457
}

0 comments on commit dfbbe60

Please sign in to comment.