Skip to content

Commit

Permalink
Merge pull request #214 from nsubstitute/GH-213-arg-matcher-false-pos…
Browse files Browse the repository at this point in the history
…itives

GH-213 - do not report NS1004 when arg matcher is used directly in return statement
  • Loading branch information
tpodolak authored Dec 9, 2023
2 parents 8e35c28 + e996107 commit 417e4e7
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal abstract class AbstractNonSubstitutableMemberArgumentMatcherAnalyzer :
OperationKind.SimpleAssignment);

private static readonly ImmutableHashSet<OperationKind> IgnoredAncestors =
ImmutableHashSet.Create(OperationKind.VariableDeclarator, OperationKind.VariableDeclaration);
ImmutableHashSet.Create(OperationKind.VariableDeclarator, OperationKind.VariableDeclaration, OperationKind.Return);

private static readonly ImmutableHashSet<OperationKind> DynamicOperations =
ImmutableHashSet.Create(OperationKind.DynamicInvocation, OperationKind.DynamicIndexerAccess, OperationKind.DynamicMemberReference);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ public abstract class NonSubstitutableMemberArgumentMatcherDiagnosticVerifier :
[MemberData(nameof(CorrectlyUsedArgTestCasesWithoutDelegates))]
public abstract Task ReportsDiagnostics_WhenAssigningInvalidArgMatchersToMemberPrecededByWithAnyArgsLikeMethod(string receivedMethod, string arg);

[CombinatoryTheory]
[MemberData(nameof(CorrectlyUsedArgTestCasesWithoutDelegates))]
public abstract Task ReportsNoDiagnostics_WhenUsedDirectlyWithReturnStatement(string arg);

public static IEnumerable<object[]> MisusedArgTestCases
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,36 @@ public void Test()
{receivedMethod}[1] = {arg};
}}
}}
}}";
await VerifyNoDiagnostic(source);
}

public override async Task ReportsNoDiagnostics_WhenUsedDirectlyWithReturnStatement(string arg)
{
var source = $@"using System;
using NSubstitute;
using NSubstitute.ReceivedExtensions;
namespace MyNamespace
{{
public interface IFoo
{{
int Bar(int? x);
}}
public class FooTests
{{
public void Test()
{{
var substitute = Substitute.For<IFoo>();
substitute.Received(1).Bar(MatchesArg());
}}
private int? MatchesArg()
{{
return {arg};
}}
}}
}}";
await VerifyNoDiagnostic(source);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ public interface INonSubstitutableMemberArgumentMatcherDiagnosticVerifier
Task ReportsDiagnostics_WhenAssigningArgMatchersToNonSubstitutableMember_InWhenLikeMethod(string whenMethod, string arg);

Task ReportsNoDiagnostics_WhenAssigningArgMatchersToSubstitutableMember_InWhenLikeMethod(string whenMethod, string arg);

Task ReportsNoDiagnostics_WhenUsedDirectlyWithReturnStatement(string arg);
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ public abstract class NonSubstitutableMemberArgumentMatcherDiagnosticVerifier :
[Fact]
public abstract Task ReportsNoDiagnostic_WhenOverloadCannotBeInferred();

[CombinatoryTheory]
[MemberData(nameof(CorrectlyUsedArgTestCasesWithoutDelegates))]
public abstract Task ReportsNoDiagnostics_WhenUsedDirectlyWithReturnStatement(string arg);

public static IEnumerable<object[]> MisusedArgTestCases
{
get { return MisusedArgs.Select(argArray => argArray.Select<string, object>(arg => arg).ToArray()); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1017,4 +1017,30 @@ End Namespace

await VerifyNoDiagnostic(source);
}

public override async Task ReportsNoDiagnostics_WhenUsedDirectlyWithReturnStatement(string arg)
{
var source = @"Imports System
Imports NSubstitute
Imports NSubstitute.ReceivedExtensions
Namespace MyNamespace
Interface IFoo
Function Bar(ByVal x As Integer?) As Integer
End Interface
Public Class FooTests
Public Sub Test()
Dim substitute = NSubstitute.Substitute.[For](Of IFoo)()
substitute.Received(1).Bar(MatchesArg())
End Sub
Private Function MatchesArg() As Integer?
Return Arg.Any(Of Integer)()
End Function
End Class
End Namespace
";
await VerifyNoDiagnostic(source);
}
}

0 comments on commit 417e4e7

Please sign in to comment.