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

GH-223 - analyze callinfo assignment #224

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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 @@ -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<IArgumentOperation>? GetSubstituteCallArgumentOperations(OperationAnalysisContext operationAnalysisContext, IInvocationOperation invocationOperation)
{
var substituteOperation = _substitutionOperationFinder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public interface Foo

public class FooTests
{{
private object state;
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
Expand Down Expand Up @@ -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;

Expand All @@ -730,7 +731,7 @@ public void Test()
var substitute = NSubstitute.Substitute.For<Foo>();
{call}.Returns(1).{method}(callInfo =>
{{
[|callInfo[1]|] = 1;
{argAssignment}
}});
}}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public interface Foo

public class FooTests
{{
private object state;
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
Expand Down Expand Up @@ -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;

Expand All @@ -950,15 +951,15 @@ public void Test()
var substitute = NSubstitute.Substitute.For<Foo>();
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}
}});
}}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,36 +86,62 @@ public abstract class CallInfoDiagnosticVerifier : CSharpDiagnosticVerifier, ICa
[CombinatoryTheory]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "callInfo.ArgAt<int>(0);")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "callInfo.ArgAt<int>(1);")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "state = callInfo.ArgAt<int>(0);")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "state = callInfo.ArgAt<int>(1);")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "var x = callInfo[0];")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "var x = callInfo[1];")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "state = callInfo[0];")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "state = callInfo[1];")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "var x = callInfo.Args()[0];")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "var x = callInfo.Args()[1];")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "state = callInfo.Args()[0];")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "state = callInfo.Args()[1];")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "var x = callInfo.ArgTypes()[0];")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "var x = callInfo.ArgTypes()[1];")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "state = callInfo.ArgTypes()[0];")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<int>()]", "state = callInfo.ArgTypes()[1];")]
[InlineData("substitute[Arg.Any<int>()]", "callInfo.ArgAt<int>(0);")]
[InlineData("substitute[Arg.Any<int>()]", "callInfo.ArgAt<int>(1);")]
[InlineData("substitute[Arg.Any<int>()]", "var x = callInfo[0];")]
[InlineData("substitute[Arg.Any<int>()]", "var x = callInfo[1];")]
[InlineData("substitute[Arg.Any<int>()]", "state = callInfo[0];")]
[InlineData("substitute[Arg.Any<int>()]", "state = callInfo[1];")]
[InlineData("substitute[Arg.Any<int>()]", "var x = callInfo.Args()[0];")]
[InlineData("substitute[Arg.Any<int>()]", "var x = callInfo.Args()[1];")]
[InlineData("substitute[Arg.Any<int>()]", "state = callInfo.Args()[0];")]
[InlineData("substitute[Arg.Any<int>()]", "state = callInfo.Args()[1];")]
[InlineData("substitute[Arg.Any<int>()]", "var x = callInfo.ArgTypes()[0];")]
[InlineData("substitute[Arg.Any<int>()]", "var x = callInfo.ArgTypes()[1];")]
[InlineData("substitute[Arg.Any<int>()]", "state = callInfo.ArgTypes()[0];")]
[InlineData("substitute[Arg.Any<int>()]", "state = callInfo.ArgTypes()[1];")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<int>())", "callInfo.ArgAt<int>(0);")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<int>())", "callInfo.ArgAt<int>(1);")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<int>())", "var x = callInfo[0];")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<int>())", "var x = callInfo[1];")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<int>())", "state = callInfo[0];")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<int>())", "state = callInfo[1];")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<int>())", "var x = callInfo.Args()[0];")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<int>())", "var x = callInfo.Args()[1];")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<int>())", "state = callInfo.Args()[0];")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<int>())", "state = callInfo.Args()[1];")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<int>())", "var x = callInfo.ArgTypes()[0];")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<int>())", "var x = callInfo.ArgTypes()[1];")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<int>())", "state = callInfo.ArgTypes()[0];")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<int>())", "state = callInfo.ArgTypes()[1];")]
[InlineData("substitute.Bar(Arg.Any<int>())", "callInfo.ArgAt<int>(0);")]
[InlineData("substitute.Bar(Arg.Any<int>())", "callInfo.ArgAt<int>(1);")]
[InlineData("substitute.Bar(Arg.Any<int>())", "var x = callInfo[0];")]
[InlineData("substitute.Bar(Arg.Any<int>())", "var x = callInfo[1];")]
[InlineData("substitute.Bar(Arg.Any<int>())", "state = callInfo[0];")]
[InlineData("substitute.Bar(Arg.Any<int>())", "state = callInfo[1];")]
[InlineData("substitute.Bar(Arg.Any<int>())", "var x = callInfo.Args()[0];")]
[InlineData("substitute.Bar(Arg.Any<int>())", "var x = callInfo.Args()[1];")]
[InlineData("substitute.Bar(Arg.Any<int>())", "state = callInfo.Args()[0];")]
[InlineData("substitute.Bar(Arg.Any<int>())", "state = callInfo.Args()[1];")]
[InlineData("substitute.Bar(Arg.Any<int>())", "var x = callInfo.ArgTypes()[0];")]
[InlineData("substitute.Bar(Arg.Any<int>())", "var x = callInfo.ArgTypes()[1];")]
[InlineData("substitute.Bar(Arg.Any<int>())", "state = callInfo.ArgTypes()[0];")]
[InlineData("substitute.Bar(Arg.Any<int>())", "state = callInfo.ArgTypes()[1];")]
public abstract Task ReportsNoDiagnostic_WhenAccessingArgumentWithinBounds(string method, string call, string argAccess);

[CombinatoryTheory]
Expand Down Expand Up @@ -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<int>(), Arg.Any<double>())")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<double>()]")]
public abstract Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call);
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<double>())", "[|callInfo[1]|] = 1;")]
[InlineData("substitute.Bar(Arg.Any<int>(), Arg.Any<double>())", "[|callInfo[1]|] = 1 as object;")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<double>()]", "[|callInfo[1]|] = 1;")]
[InlineData("substitute[Arg.Any<int>(), Arg.Any<double>()]", "[|callInfo[1]|] = 1 as object;")]
public abstract Task ReportsDiagnostic_WhenAssigningValueToNotOutNorRefArgument(string method, string call, string argAssignment);

[CombinatoryTheory]
[InlineData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public interface Foo

public class FooTests
{{
private object state;
public void Test()
{{
var sub = NSubstitute.Substitute.For<Foo>();
Expand Down Expand Up @@ -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;

Expand All @@ -725,7 +726,7 @@ public void Test()
var sub = NSubstitute.Substitute.For<Foo>();
sub.{method}(substitute => {{var _ = {call}; }}).Do(callInfo =>
{{
[|callInfo[1]|] = 1;
{argAssignment}
}});
}}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public interface Foo

public class FooTests
{{
private object state;
public void Test()
{{
var sub = NSubstitute.Substitute.For<Foo>();
Expand Down Expand Up @@ -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;

Expand All @@ -938,15 +939,15 @@ public void Test()
var sub = NSubstitute.Substitute.For<Foo>();
{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}
}});
}}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public interface Foo

public class FooTests
{{
private object state;
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
Expand Down Expand Up @@ -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;

Expand All @@ -752,7 +753,7 @@ public void Test()
var substitute = NSubstitute.Substitute.For<Foo>();
{call}.{method}(callInfo =>
{{
[|callInfo[1]|] = 1;
{argAssignment}
return 1;
}});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public interface Foo

public class FooTests
{{
private object state;
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
Expand Down Expand Up @@ -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;

Expand All @@ -1002,17 +1003,17 @@ public void Test()
var substitute = NSubstitute.Substitute.For<Foo>();
{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});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public interface Foo

public class FooTests
{{
private object state;
public void Test()
{{
var substitute = NSubstitute.Substitute.For<Foo>();
Expand Down Expand Up @@ -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;
Expand All @@ -789,7 +790,7 @@ public void Test()
var substitute = NSubstitute.Substitute.For<Foo>();
{call}.{method}(callInfo =>
{{
[|callInfo[1]|] = 1;
{argAssignment}
return new Exception();
}});
}}
Expand Down
Loading