From 311f42f6e28a65069cde2a96d1280c2c1d54de45 Mon Sep 17 00:00:00 2001 From: tpodolak Date: Mon, 22 Jul 2024 11:54:27 +0200 Subject: [PATCH] GH-223 - analyze callinfo assignment --- .../AbstractCallInfoAnalyzer.cs | 68 +++++++++++-------- ...oesMethodPrecededByExtensionMethodTests.cs | 5 +- ...DoesMethodPrecededByOrdinaryMethodTests.cs | 9 +-- .../CallInfoDiagnosticVerifier.cs | 34 +++++++++- .../DoMethodPrecededByExtensionMethodTests.cs | 5 +- .../DoMethodPrecededByOrdinaryMethodTests.cs | 9 +-- .../ReturnsAsExtensionMethodTests.cs | 5 +- .../ReturnsAsOrdinaryMethodTests.cs | 9 +-- .../ThrowsAsExtensionMethodTests.cs | 5 +- .../ThrowsAsOrdinaryMethodTests.cs | 9 +-- .../ICallInfoDiagnosticVerifier.cs | 2 +- ...oesMethodPrecededByExtensionMethodTests.cs | 5 +- ...DoesMethodPrecededByOrdinaryMethodTests.cs | 9 +-- .../CallInfoDiagnosticVerifier.cs | 36 +++++++++- .../DoMethodPrecededByExtensionMethodTests.cs | 5 +- .../DoMethodPrecededByOrdinaryMethodTests.cs | 9 +-- .../ReturnsAsExtensionMethodsTests.cs | 5 +- .../ReturnsAsOrdinaryMethodTests.cs | 9 +-- .../ThrowsAsExtensionMethodsTests.cs | 5 +- .../ThrowsAsOrdinaryMethodsTests.cs | 9 +-- 20 files changed, 170 insertions(+), 82 deletions(-) diff --git a/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/AbstractCallInfoAnalyzer.cs b/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/AbstractCallInfoAnalyzer.cs index fa84babf..b8a47557 100644 --- a/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/AbstractCallInfoAnalyzer.cs +++ b/src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/AbstractCallInfoAnalyzer.cs @@ -242,40 +242,54 @@ private bool AnalyzeAssignment( return false; } - if (indexerOperation is IPropertyReferenceOperation { Parent: ISimpleAssignmentOperation simpleAssignmentOperation }) + if (indexerOperation is not IPropertyReferenceOperation { Parent: IAssignmentOperation assignmentOperation }) { - var parameterSymbol = substituteCallParameters[position.Value]; - if (parameterSymbol.Parameter.RefKind != RefKind.Out && - parameterSymbol.Parameter.RefKind != RefKind.Ref) - { - var diagnostic = Diagnostic.Create( - DiagnosticDescriptorsProvider.CallInfoArgumentIsNotOutOrRef, - indexerOperation.Syntax.GetLocation(), - position.Value, - parameterSymbol.GetArgumentOperationDeclaredTypeSymbol()); - operationAnalysisContext.ReportDiagnostic(diagnostic); - return true; - } + return false; + } - var assignmentType = simpleAssignmentOperation.GetTypeSymbol(); - var typeSymbol = substituteCallParameters[position.Value].GetArgumentOperationDeclaredTypeSymbol(); - if (assignmentType != null && - IsAssignableTo(operationAnalysisContext.Compilation, assignmentType, typeSymbol) == false) - { - var diagnostic = Diagnostic.Create( - DiagnosticDescriptorsProvider.CallInfoArgumentSetWithIncompatibleValue, - indexerOperation.Syntax.GetLocation(), - assignmentType, - position.Value, - typeSymbol); - operationAnalysisContext.ReportDiagnostic(diagnostic); - return true; - } + if (!IsCallInfoAssignmentOperation(assignmentOperation)) + { + return false; + } + + var parameterSymbol = substituteCallParameters[position.Value]; + if (parameterSymbol.Parameter.RefKind != RefKind.Out && + parameterSymbol.Parameter.RefKind != RefKind.Ref) + { + var diagnostic = Diagnostic.Create( + this.DiagnosticDescriptorsProvider.CallInfoArgumentIsNotOutOrRef, + indexerOperation.Syntax.GetLocation(), + position.Value, + parameterSymbol.GetArgumentOperationDeclaredTypeSymbol()); + operationAnalysisContext.ReportDiagnostic(diagnostic); + return true; + } + + var assignmentType = assignmentOperation.GetTypeSymbol(); + var typeSymbol = substituteCallParameters[position.Value].GetArgumentOperationDeclaredTypeSymbol(); + if (assignmentType != null && + this.IsAssignableTo(operationAnalysisContext.Compilation, assignmentType, typeSymbol) == false) + { + var diagnostic = Diagnostic.Create( + this.DiagnosticDescriptorsProvider.CallInfoArgumentSetWithIncompatibleValue, + indexerOperation.Syntax.GetLocation(), + assignmentType, + position.Value, + typeSymbol); + operationAnalysisContext.ReportDiagnostic(diagnostic); + return true; } return false; } + private static bool IsCallInfoAssignmentOperation(IAssignmentOperation assignmentOperation) + { + return assignmentOperation.Target is IMemberReferenceOperation memberReferenceOperation && + memberReferenceOperation.Member.ContainingAssembly.Identity.Name.Equals(MetadataNames + .NSubstituteAssemblyName); + } + private IReadOnlyList? GetSubstituteCallArgumentOperations(OperationAnalysisContext operationAnalysisContext, IInvocationOperation invocationOperation) { var substituteOperation = _substitutionOperationFinder diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByExtensionMethodTests.cs index f50b94cf..35468fe7 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByExtensionMethodTests.cs @@ -122,6 +122,7 @@ public interface Foo public class FooTests {{ + private object state; public void Test() {{ var substitute = NSubstitute.Substitute.For(); @@ -710,7 +711,7 @@ public void Test() await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"using NSubstitute; @@ -730,7 +731,7 @@ public void Test() var substitute = NSubstitute.Substitute.For(); {call}.Returns(1).{method}(callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} }}); }} }} diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByOrdinaryMethodTests.cs index 14bf7410..11b9a019 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByOrdinaryMethodTests.cs @@ -149,6 +149,7 @@ public interface Foo public class FooTests {{ + private object state; public void Test() {{ var substitute = NSubstitute.Substitute.For(); @@ -930,7 +931,7 @@ public void Test() await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"using NSubstitute; @@ -950,15 +951,15 @@ public void Test() var substitute = NSubstitute.Substitute.For(); SubstituteExtensions.Returns({call}, 1).{method}(callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} }}); SubstituteExtensions.Returns(value: {call}, returnThis: 1).{method}(callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} }}); SubstituteExtensions.Returns(returnThis: 1, value: {call}).{method}(callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} }}); }} }} diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs index ddbb05a6..ff0ed86c 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs @@ -86,36 +86,62 @@ public abstract class CallInfoDiagnosticVerifier : CSharpDiagnosticVerifier, ICa [CombinatoryTheory] [InlineData("substitute[Arg.Any(), Arg.Any()]", "callInfo.ArgAt(0);")] [InlineData("substitute[Arg.Any(), Arg.Any()]", "callInfo.ArgAt(1);")] + [InlineData("substitute[Arg.Any(), Arg.Any()]", "state = callInfo.ArgAt(0);")] + [InlineData("substitute[Arg.Any(), Arg.Any()]", "state = callInfo.ArgAt(1);")] [InlineData("substitute[Arg.Any(), Arg.Any()]", "var x = callInfo[0];")] [InlineData("substitute[Arg.Any(), Arg.Any()]", "var x = callInfo[1];")] + [InlineData("substitute[Arg.Any(), Arg.Any()]", "state = callInfo[0];")] + [InlineData("substitute[Arg.Any(), Arg.Any()]", "state = callInfo[1];")] [InlineData("substitute[Arg.Any(), Arg.Any()]", "var x = callInfo.Args()[0];")] [InlineData("substitute[Arg.Any(), Arg.Any()]", "var x = callInfo.Args()[1];")] + [InlineData("substitute[Arg.Any(), Arg.Any()]", "state = callInfo.Args()[0];")] + [InlineData("substitute[Arg.Any(), Arg.Any()]", "state = callInfo.Args()[1];")] [InlineData("substitute[Arg.Any(), Arg.Any()]", "var x = callInfo.ArgTypes()[0];")] [InlineData("substitute[Arg.Any(), Arg.Any()]", "var x = callInfo.ArgTypes()[1];")] + [InlineData("substitute[Arg.Any(), Arg.Any()]", "state = callInfo.ArgTypes()[0];")] + [InlineData("substitute[Arg.Any(), Arg.Any()]", "state = callInfo.ArgTypes()[1];")] [InlineData("substitute[Arg.Any()]", "callInfo.ArgAt(0);")] [InlineData("substitute[Arg.Any()]", "callInfo.ArgAt(1);")] [InlineData("substitute[Arg.Any()]", "var x = callInfo[0];")] [InlineData("substitute[Arg.Any()]", "var x = callInfo[1];")] + [InlineData("substitute[Arg.Any()]", "state = callInfo[0];")] + [InlineData("substitute[Arg.Any()]", "state = callInfo[1];")] [InlineData("substitute[Arg.Any()]", "var x = callInfo.Args()[0];")] [InlineData("substitute[Arg.Any()]", "var x = callInfo.Args()[1];")] + [InlineData("substitute[Arg.Any()]", "state = callInfo.Args()[0];")] + [InlineData("substitute[Arg.Any()]", "state = callInfo.Args()[1];")] [InlineData("substitute[Arg.Any()]", "var x = callInfo.ArgTypes()[0];")] [InlineData("substitute[Arg.Any()]", "var x = callInfo.ArgTypes()[1];")] + [InlineData("substitute[Arg.Any()]", "state = callInfo.ArgTypes()[0];")] + [InlineData("substitute[Arg.Any()]", "state = callInfo.ArgTypes()[1];")] [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "callInfo.ArgAt(0);")] [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "callInfo.ArgAt(1);")] [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "var x = callInfo[0];")] [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "var x = callInfo[1];")] + [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "state = callInfo[0];")] + [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "state = callInfo[1];")] [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "var x = callInfo.Args()[0];")] [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "var x = callInfo.Args()[1];")] + [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "state = callInfo.Args()[0];")] + [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "state = callInfo.Args()[1];")] [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "var x = callInfo.ArgTypes()[0];")] [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "var x = callInfo.ArgTypes()[1];")] + [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "state = callInfo.ArgTypes()[0];")] + [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "state = callInfo.ArgTypes()[1];")] [InlineData("substitute.Bar(Arg.Any())", "callInfo.ArgAt(0);")] [InlineData("substitute.Bar(Arg.Any())", "callInfo.ArgAt(1);")] [InlineData("substitute.Bar(Arg.Any())", "var x = callInfo[0];")] [InlineData("substitute.Bar(Arg.Any())", "var x = callInfo[1];")] + [InlineData("substitute.Bar(Arg.Any())", "state = callInfo[0];")] + [InlineData("substitute.Bar(Arg.Any())", "state = callInfo[1];")] [InlineData("substitute.Bar(Arg.Any())", "var x = callInfo.Args()[0];")] [InlineData("substitute.Bar(Arg.Any())", "var x = callInfo.Args()[1];")] + [InlineData("substitute.Bar(Arg.Any())", "state = callInfo.Args()[0];")] + [InlineData("substitute.Bar(Arg.Any())", "state = callInfo.Args()[1];")] [InlineData("substitute.Bar(Arg.Any())", "var x = callInfo.ArgTypes()[0];")] [InlineData("substitute.Bar(Arg.Any())", "var x = callInfo.ArgTypes()[1];")] + [InlineData("substitute.Bar(Arg.Any())", "state = callInfo.ArgTypes()[0];")] + [InlineData("substitute.Bar(Arg.Any())", "state = callInfo.ArgTypes()[1];")] public abstract Task ReportsNoDiagnostic_WhenAccessingArgumentWithinBounds(string method, string call, string argAccess); [CombinatoryTheory] @@ -284,9 +310,11 @@ public abstract class CallInfoDiagnosticVerifier : CSharpDiagnosticVerifier, ICa public abstract Task ReportsNoDiagnostic_WhenAccessingArgumentByTypeMultipleDifferentTypesInInvocation(string method, string call, string argAccess); [CombinatoryTheory] - [InlineData("substitute.Bar(Arg.Any(), Arg.Any())")] - [InlineData("substitute[Arg.Any(), Arg.Any()]")] - public abstract Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call); + [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "[|callInfo[1]|] = 1;")] + [InlineData("substitute.Bar(Arg.Any(), Arg.Any())", "[|callInfo[1]|] = 1 as object;")] + [InlineData("substitute[Arg.Any(), Arg.Any()]", "[|callInfo[1]|] = 1;")] + [InlineData("substitute[Arg.Any(), Arg.Any()]", "[|callInfo[1]|] = 1 as object;")] + public abstract Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment); [CombinatoryTheory] [InlineData] diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/DoMethodPrecededByExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/DoMethodPrecededByExtensionMethodTests.cs index 44b6377d..fd266cd8 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/DoMethodPrecededByExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/DoMethodPrecededByExtensionMethodTests.cs @@ -121,6 +121,7 @@ public interface Foo public class FooTests {{ + private object state; public void Test() {{ var sub = NSubstitute.Substitute.For(); @@ -705,7 +706,7 @@ public void Test() await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"using NSubstitute; @@ -725,7 +726,7 @@ public void Test() var sub = NSubstitute.Substitute.For(); sub.{method}(substitute => {{var _ = {call}; }}).Do(callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} }}); }} }} diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/DoMethodPrecededByOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/DoMethodPrecededByOrdinaryMethodTests.cs index ae60f294..0d1056d0 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/DoMethodPrecededByOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/DoMethodPrecededByOrdinaryMethodTests.cs @@ -148,6 +148,7 @@ public interface Foo public class FooTests {{ + private object state; public void Test() {{ var sub = NSubstitute.Substitute.For(); @@ -918,7 +919,7 @@ public void Test() await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"using NSubstitute; @@ -938,15 +939,15 @@ public void Test() var sub = NSubstitute.Substitute.For(); {method}(sub, substitute => {{var _ = {call}; }}).Do(callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} }}); {method}(substitute: sub, substituteCall: substitute => {{var _ = {call}; }}).Do(callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} }}); {method}(substituteCall: substitute => {{var _ = {call}; }}, substitute: sub).Do(callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} }}); }} }} diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ReturnsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ReturnsAsExtensionMethodTests.cs index 0ae4a9f7..1fa30cd4 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ReturnsAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ReturnsAsExtensionMethodTests.cs @@ -125,6 +125,7 @@ public interface Foo public class FooTests {{ + private object state; public void Test() {{ var substitute = NSubstitute.Substitute.For(); @@ -732,7 +733,7 @@ public void Test() await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"using NSubstitute; @@ -752,7 +753,7 @@ public void Test() var substitute = NSubstitute.Substitute.For(); {call}.{method}(callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} return 1; }}); }} diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs index 5c3cd06d..36d35c8a 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs @@ -156,6 +156,7 @@ public interface Foo public class FooTests {{ + private object state; public void Test() {{ var substitute = NSubstitute.Substitute.For(); @@ -982,7 +983,7 @@ public void Test() await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"using NSubstitute; @@ -1002,17 +1003,17 @@ public void Test() var substitute = NSubstitute.Substitute.For(); {method}({call}, callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} return 1; }}); {method}(value: {call}, returnThis: callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} return 1; }}); {method}(returnThis: callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} return 1; }}, value: {call}); }} diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ThrowsAsExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ThrowsAsExtensionMethodTests.cs index 9e525a4c..db109f81 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ThrowsAsExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ThrowsAsExtensionMethodTests.cs @@ -132,6 +132,7 @@ public interface Foo public class FooTests {{ + private object state; public void Test() {{ var substitute = NSubstitute.Substitute.For(); @@ -766,7 +767,7 @@ public void Test() await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"using System; using System.Threading.Tasks; @@ -789,7 +790,7 @@ public void Test() var substitute = NSubstitute.Substitute.For(); {call}.{method}(callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} return new Exception(); }}); }} diff --git a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs index 68f728cc..481c82b0 100644 --- a/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.CSharp/DiagnosticAnalyzerTests/CallInfoAnalyzerTests/ThrowsAsOrdinaryMethodTests.cs @@ -167,6 +167,7 @@ public interface Foo public class FooTests {{ + private object state; public void Test() {{ var substitute = NSubstitute.Substitute.For(); @@ -1027,7 +1028,7 @@ public void Test() await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"using System; using System.Threading.Tasks; @@ -1050,17 +1051,17 @@ public void Test() var substitute = NSubstitute.Substitute.For(); {method}({call}, callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} return new Exception(); }}); {method}(value: {call}, createException: callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} return new Exception(); }}); {method}(createException: callInfo => {{ - [|callInfo[1]|] = 1; + {argAssignment} return new Exception(); }}, value: {call}); }} diff --git a/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/ICallInfoDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/ICallInfoDiagnosticVerifier.cs index f7b504d5..c3e89fb7 100644 --- a/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/ICallInfoDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.Shared/DiagnosticAnalyzers/ICallInfoDiagnosticVerifier.cs @@ -36,7 +36,7 @@ public interface ICallInfoDiagnosticVerifier Task ReportsNoDiagnostic_WhenAccessingArgumentByTypeMultipleDifferentTypesInInvocation(string method, string call, string argAccess); - Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call); + Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment); Task ReportsNoDiagnostic_WhenAssigningValueToRefArgument(string method); diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByExtensionMethodTests.cs index f6bb754a..e04d8649 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByExtensionMethodTests.cs @@ -96,6 +96,7 @@ Default ReadOnly Property Item(ByVal x As Integer, ByVal Optional y As Integer = End Interface Public Class FooTests + Private state As Object Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo)() {call}.Returns(1).{method}(Function(callInfo) @@ -522,7 +523,7 @@ End Namespace await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"Imports NSubstitute @@ -536,7 +537,7 @@ Public Class FooTests Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo)() {call}.Returns(1).{method}(Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} End Function) End Sub End Class diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByOrdinaryMethodTests.cs index eeab7505..3de18f62 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/AndDoesMethodPrecededByOrdinaryMethodTests.cs @@ -118,6 +118,7 @@ Default ReadOnly Property Item(ByVal x As Integer, ByVal Optional y As Integer = End Interface Public Class FooTests + Private state As Object Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo)() SubstituteExtensions.Returns({call}, 1).{method}(Function(callInfo) @@ -668,7 +669,7 @@ End Namespace await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"Imports NSubstitute @@ -682,13 +683,13 @@ Public Class FooTests Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo)() SubstituteExtensions.Returns({call}, 1).{method}(Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} End Function) SubstituteExtensions.Returns(value:= {call}, returnThis:= 1).{method}(Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} End Function) SubstituteExtensions.Returns(returnThis:= 1, value:= {call}).{method}(Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} End Function) End Sub End Class diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs index 56600662..66bc2f3c 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/CallInfoDiagnosticVerifier.cs @@ -100,34 +100,58 @@ public abstract class CallInfoDiagnosticVerifier : VisualBasicDiagnosticVerifier [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "callInfo.ArgAt(Of Integer)(1)")] [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "Dim x = callInfo(0)")] [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "Dim x = callInfo(1)")] + [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "state = callInfo(0)")] + [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "state = callInfo(1)")] [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "Dim x = callInfo.Args()(0)")] [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "Dim x = callInfo.Args()(1)")] + [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "state = callInfo.Args()(0)")] + [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "state = callInfo.Args()(1)")] [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "Dim x = callInfo.ArgTypes()(0)")] [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "Dim x = callInfo.ArgTypes()(1)")] + [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "state = callInfo.ArgTypes()(0)")] + [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "state = callInfo.ArgTypes()(1)")] [InlineData("substitute(Arg.Any(Of Integer)())", "callInfo.ArgAt(Of Integer)(0)")] [InlineData("substitute(Arg.Any(Of Integer)())", "callInfo.ArgAt(Of Integer)(1)")] [InlineData("substitute(Arg.Any(Of Integer)())", "Dim x = callInfo(0)")] [InlineData("substitute(Arg.Any(Of Integer)())", "Dim x = callInfo(1)")] + [InlineData("substitute(Arg.Any(Of Integer)())", "state = callInfo(0)")] + [InlineData("substitute(Arg.Any(Of Integer)())", "state = callInfo(1)")] [InlineData("substitute(Arg.Any(Of Integer)())", "Dim x = callInfo.Args()(0)")] [InlineData("substitute(Arg.Any(Of Integer)())", "Dim x = callInfo.Args()(1)")] + [InlineData("substitute(Arg.Any(Of Integer)())", "state = callInfo.Args()(0)")] + [InlineData("substitute(Arg.Any(Of Integer)())", "state = callInfo.Args()(1)")] [InlineData("substitute(Arg.Any(Of Integer)())", "Dim x = callInfo.ArgTypes()(0)")] [InlineData("substitute(Arg.Any(Of Integer)())", "Dim x = callInfo.ArgTypes()(1)")] + [InlineData("substitute(Arg.Any(Of Integer)())", "state = callInfo.ArgTypes()(0)")] + [InlineData("substitute(Arg.Any(Of Integer)())", "state = callInfo.ArgTypes()(1)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "callInfo.ArgAt(Of Integer)(0)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "callInfo.ArgAt(Of Integer)(1)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "Dim x = callInfo(0)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "Dim x = callInfo(1)")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "state = callInfo(0)")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "state = callInfo(1)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "Dim x = callInfo.Args()(0)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "Dim x = callInfo.Args()(1)")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "state = callInfo.Args()(0)")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "state = callInfo.Args()(1)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "Dim x = callInfo.ArgTypes()(0)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "Dim x = callInfo.ArgTypes()(1)")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "state = callInfo.ArgTypes()(0)")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Integer)())", "state = callInfo.ArgTypes()(1)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "callInfo.ArgAt(Of Integer)(0)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "callInfo.ArgAt(Of Integer)(1)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "Dim x = callInfo(0)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "Dim x = callInfo(1)")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "state = callInfo(0)")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "state = callInfo(1)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "Dim x = callInfo.Args()(0)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "Dim x = callInfo.Args()(1)")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "state = callInfo.Args()(0)")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "state = callInfo.Args()(1)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "Dim x = callInfo.ArgTypes()(0)")] [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "Dim x = callInfo.ArgTypes()(1)")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "state = callInfo.ArgTypes()(0)")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)())", "state = callInfo.ArgTypes()(1)")] public abstract Task ReportsNoDiagnostic_WhenAccessingArgumentWithinBounds(string method, string call, string argAccess); [CombinatoryTheory] @@ -315,9 +339,15 @@ public abstract class CallInfoDiagnosticVerifier : VisualBasicDiagnosticVerifier public abstract Task ReportsNoDiagnostic_WhenAccessingArgumentByTypeMultipleDifferentTypesInInvocation(string method, string call, string argAccess); [CombinatoryTheory] - [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Double)())")] - [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Double)())")] - public abstract Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call); + [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Double)())", "[|callInfo(1)|] = 1")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Double)())", "[|callInfo(1)|] = DirectCast(1, Object)")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Double)())", "[|callInfo(1)|] = TryCast(1, Object)")] + [InlineData("substitute.Bar(Arg.Any(Of Integer)(), Arg.Any(Of Double)())", "[|callInfo(1)|] = CType(1, Object)")] + [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Double)())", "[|callInfo(1)|] = 1")] + [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Double)())", "[|callInfo(1)|] = DirectCast(1, Object)")] + [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Double)())", "[|callInfo(1)|] = TryCast(1, Object)")] + [InlineData("substitute(Arg.Any(Of Integer)(), Arg.Any(Of Double)())", "[|callInfo(1)|] = CType(1, Object)")] + public abstract Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment); [CombinatoryTheory] [InlineData] diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/DoMethodPrecededByExtensionMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/DoMethodPrecededByExtensionMethodTests.cs index d19ef503..766dd3d0 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/DoMethodPrecededByExtensionMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/DoMethodPrecededByExtensionMethodTests.cs @@ -103,6 +103,7 @@ Default ReadOnly Property Item(ByVal x As Integer, ByVal Optional y As Integer = End Interface Public Class FooTests + Private state As Object Public Sub Test() Dim mock = NSubstitute.Substitute.[For](Of Foo)() mock.{method}(Function(substitute) @@ -553,7 +554,7 @@ End Namespace await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"Imports NSubstitute @@ -569,7 +570,7 @@ Public Sub Test() mock.{method}(Function(substitute) Dim x = {call} End Function).Do(Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} End Function) End Sub End Class diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/DoMethodPrecededByOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/DoMethodPrecededByOrdinaryMethodTests.cs index e4eb5ab4..dd9b393d 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/DoMethodPrecededByOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/DoMethodPrecededByOrdinaryMethodTests.cs @@ -136,6 +136,7 @@ Default ReadOnly Property Item(ByVal x As Integer, ByVal Optional y As Integer = End Interface Public Class FooTests + Private state As Object Public Sub Test() Dim mock = NSubstitute.Substitute.[For](Of Foo)() {method}(mock, Function(substitute) @@ -759,7 +760,7 @@ End Namespace await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"Imports NSubstitute @@ -775,17 +776,17 @@ Public Sub Test() {method}(mock, Function(substitute) Dim x = {call} End Function).Do(Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} End Function) {method}(substitute:= mock, substituteCall:= Function(substitute) Dim x = {call} End Function).Do(Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} End Function) {method}(substituteCall:= Function(substitute) Dim x = {call} End Function, substitute:= mock).Do(Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} End Function) End Sub End Class diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ReturnsAsExtensionMethodsTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ReturnsAsExtensionMethodsTests.cs index 4a150e12..1f24966d 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ReturnsAsExtensionMethodsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ReturnsAsExtensionMethodsTests.cs @@ -99,6 +99,7 @@ Default ReadOnly Property Item(ByVal x As Integer, ByVal Optional y As Integer = End Interface Public Class FooTests + Private state As Object Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo)() {call}.{method}(Function(callInfo) @@ -545,7 +546,7 @@ End Namespace await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"Imports NSubstitute @@ -559,7 +560,7 @@ Public Class FooTests Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo)() {call}.{method}(Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} Return 1 End Function) End Sub diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs index 3bfe2df8..104564af 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ReturnsAsOrdinaryMethodTests.cs @@ -124,6 +124,7 @@ Default ReadOnly Property Item(ByVal x As Integer, ByVal Optional y As Integer = End Interface Public Class FooTests + Private state As Object Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo)() {method}({call}, Function(callInfo) @@ -715,7 +716,7 @@ End Namespace await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"Imports NSubstitute @@ -729,15 +730,15 @@ Public Class FooTests Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo)() {method}({call}, Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} Return 1 End Function) {method}(value:= {call}, returnThis:= Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} Return 1 End Function) {method}(returnThis:= Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} Return 1 End Function, value:= {call}) End Sub diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ThrowsAsExtensionMethodsTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ThrowsAsExtensionMethodsTests.cs index 25bfcd64..07b91602 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ThrowsAsExtensionMethodsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ThrowsAsExtensionMethodsTests.cs @@ -106,6 +106,7 @@ Default ReadOnly Property Item(ByVal x As Integer, ByVal Optional y As Integer = End Interface Public Class FooTests + Private state As Object Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo)() {call}.{method}(Function(callInfo) @@ -572,7 +573,7 @@ End Namespace await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"Imports System Imports System.Threading.Tasks @@ -589,7 +590,7 @@ Public Class FooTests Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo)() {call}.{method}(Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} Return New Exception() End Function) End Sub diff --git a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ThrowsAsOrdinaryMethodsTests.cs b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ThrowsAsOrdinaryMethodsTests.cs index 3ff2af8a..edb94b17 100644 --- a/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ThrowsAsOrdinaryMethodsTests.cs +++ b/tests/NSubstitute.Analyzers.Tests.VisualBasic/DiagnosticAnalyzersTests/CallInfoAnalyzerTests/ThrowsAsOrdinaryMethodsTests.cs @@ -135,6 +135,7 @@ Default ReadOnly Property Item(ByVal x As Integer, ByVal Optional y As Integer = End Interface Public Class FooTests + Private state As Object Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo)() {method}({call}, Function(callInfo) @@ -738,7 +739,7 @@ End Namespace await VerifyNoDiagnostic(source); } - public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call) + public override async Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment) { var source = $@"Imports System Imports System.Threading.Tasks @@ -755,15 +756,15 @@ Public Class FooTests Public Sub Test() Dim substitute = NSubstitute.Substitute.[For](Of Foo)() {method}({call}, Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} Return New Exception() End Function) {method}(value:= {call}, createException:= Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} Return New Exception() End Function) {method}(createException:= Function(callInfo) - [|callInfo(1)|] = 1 + {argAssignment} Return New Exception() End Function, value:= {call}) End Sub