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

Fix S107: Do not raise for P/Invoke methods (#1459) #1493

Merged
merged 1 commit into from
Jun 22, 2018
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 @@ -53,7 +53,6 @@ protected override void Initialize(ParameterLoadingAnalysisContext context)
var parameterListNode = (ParameterListSyntax)c.Node;
var parameters = parameterListNode.Parameters.Count;


if (parameters > Maximum &&
parameterListNode.Parent != null &&
CanBeChanged(parameterListNode.Parent, c.SemanticModel) &&
Expand Down Expand Up @@ -91,6 +90,15 @@ private bool CanBeChanged(SyntaxNode node, SemanticModel semanticModel)
return false;
}

if (declaredSymbol.IsExtern &&
declaredSymbol.IsStatic &&
declaredSymbol.HasAttribute(KnownType.System_Runtime_InteropServices_DllImportAttribute))
{
// P/Invoke method is defined externally.
// Do not raise
return false;
}

return declaredSymbol.GetOverriddenMember() == null &&
declaredSymbol.GetInterfaceMember() == null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.InteropServices;

namespace Tests.Diagnostics
{
Expand All @@ -24,6 +25,11 @@ public void F()
var v6 = new Action<object, object, object>((p1, p2, p3) => Console.WriteLine());
var v7 = new Action<object, object, object, object>((p1, p2, p3, p4) => Console.WriteLine()); // Noncompliant
}

// see https://github.com/SonarSource/sonar-csharp/issues/1459
// We should not raise for imported methods according to external definition.
[DllImport()]
public static extern Extern(int p1, int p2, int p3, int p4); // Compliant, external definition
}

public interface If
Expand Down