-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xunit/xunit#2680: Add Analyzer to warn about calling Assert.Empty wit…
…h StringValues or ArraySegment<T>
- Loading branch information
1 parent
179e874
commit 01f4b5f
Showing
4 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/xunit.analyzers.tests/Analyzers/X2000/DoNotUseAssertEmptyWithProblematicTypesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Xunit; | ||
using Verify = CSharpVerifier<Xunit.Analyzers.DoNotUseAssertEmptyWithProblematicTypes>; | ||
|
||
public class DoNotUseAssertEmptyWithProblematicTypes | ||
{ | ||
public static TheoryData<string, string, string> ProblematicTypes = new() | ||
{ | ||
{ "StringValues.Empty", "StringValues", "it is implicitly cast to a string, not a collection" }, | ||
{ "new ArraySegment<int>()", "ArraySegment<int>", "its implementation of GetEnumerator() can throw" }, | ||
}; | ||
|
||
[Theory] | ||
[InlineData("new int[0]")] | ||
[InlineData("new List<int>()")] | ||
[InlineData("new Dictionary<string, int>()")] | ||
public async Task NonProblematicCollection_DoesNotTrigger(string invocation) | ||
{ | ||
var source = @$" | ||
using System; | ||
using System.Collections.Generic; | ||
using Xunit; | ||
public class TestClass {{ | ||
public void TestMethod() {{ | ||
Assert.Empty({invocation}); | ||
}} | ||
}}"; | ||
|
||
await Verify.VerifyAnalyzer(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(ProblematicTypes))] | ||
public async Task ConvertingToCollection_DoesNotTrigger( | ||
string invocation, | ||
string _1, | ||
string _2) | ||
{ | ||
var source = @$" | ||
using System; | ||
using System.Linq; | ||
using Microsoft.Extensions.Primitives; | ||
using Xunit; | ||
public class TestClass {{ | ||
public void TestMethod() {{ | ||
Assert.Empty({invocation}.ToArray()); | ||
}} | ||
}}"; | ||
|
||
await Verify.VerifyAnalyzer(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(ProblematicTypes))] | ||
public async Task UsingProblematicType_Triggers( | ||
string invocation, | ||
string typeName, | ||
string problem) | ||
{ | ||
var source = @$" | ||
using System; | ||
using Microsoft.Extensions.Primitives; | ||
using Xunit; | ||
public class TestClass {{ | ||
public void TestMethod() {{ | ||
Assert.Empty({invocation}); | ||
}} | ||
}}"; | ||
|
||
var expected = | ||
Verify | ||
.Diagnostic() | ||
.WithSpan(8, 9, 8, 23 + invocation.Length) | ||
.WithSeverity(DiagnosticSeverity.Warning) | ||
.WithArguments(typeName, problem); | ||
|
||
await Verify.VerifyAnalyzer(source, expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/xunit.analyzers/X2000/DoNotUseAssertEmptyWithProblematicTypes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace Xunit.Analyzers; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class DoNotUseAssertEmptyWithProblematicTypes : AssertUsageAnalyzerBase | ||
{ | ||
public DoNotUseAssertEmptyWithProblematicTypes() : | ||
base(Descriptors.X2028_DoNotUseAssertEmptyWithProblematicTypes, new[] { Constants.Asserts.Empty }) | ||
{ } | ||
|
||
protected override void AnalyzeInvocation( | ||
OperationAnalysisContext context, | ||
XunitContext xunitContext, | ||
IInvocationOperation invocationOperation, | ||
IMethodSymbol method) | ||
{ | ||
Guard.ArgumentNotNull(xunitContext); | ||
Guard.ArgumentNotNull(invocationOperation); | ||
Guard.ArgumentNotNull(method); | ||
|
||
var semanticModel = context.Operation.SemanticModel; | ||
if (semanticModel is null) | ||
return; | ||
|
||
var arguments = invocationOperation.Arguments; | ||
if (arguments.Length != 1) | ||
return; | ||
|
||
if (method.Parameters.Length != 1) | ||
return; | ||
|
||
if (semanticModel.GetTypeInfo(arguments[0].Value.Syntax).Type is not INamedTypeSymbol sourceType) | ||
return; | ||
|
||
var stringValuesType = TypeSymbolFactory.StringValues(context.Compilation); | ||
if (stringValuesType is not null && SymbolEqualityComparer.Default.Equals(sourceType, stringValuesType)) | ||
context.ReportDiagnostic( | ||
Diagnostic.Create( | ||
Descriptors.X2028_DoNotUseAssertEmptyWithProblematicTypes, | ||
invocationOperation.Syntax.GetLocation(), | ||
sourceType.ToMinimalDisplayString(semanticModel, 0), | ||
"it is implicitly cast to a string, not a collection" | ||
) | ||
); | ||
|
||
if (sourceType.IsGenericType) | ||
{ | ||
var arraySegmentType = TypeSymbolFactory.ArraySegmentOfT(context.Compilation)?.ConstructUnboundGenericType(); | ||
if (arraySegmentType is not null && SymbolEqualityComparer.Default.Equals(sourceType.ConstructUnboundGenericType(), arraySegmentType)) | ||
context.ReportDiagnostic( | ||
Diagnostic.Create( | ||
Descriptors.X2028_DoNotUseAssertEmptyWithProblematicTypes, | ||
invocationOperation.Syntax.GetLocation(), | ||
sourceType.ToMinimalDisplayString(semanticModel, 0), | ||
"its implementation of GetEnumerator() can throw" | ||
) | ||
); | ||
} | ||
} | ||
} |