Skip to content

Commit

Permalink
Fix S107: Do not for P/Invoke methods (#1459) (#1493)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-ais authored and duncanp-sonar committed Jun 22, 2018
1 parent d7402b4 commit 1a603e8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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

0 comments on commit 1a603e8

Please sign in to comment.