Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add repro for #5457 #5481

Merged
merged 1 commit into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add later today a second PR with a fix.

}