Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.15.3" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.15.2" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.15.1" />
<PackageVersion Include="NUnit" Version="4.5.1" />
<PackageVersion Include="NUnit" Version="4.6.0" />
<PackageVersion Include="NUnit.Analyzers" Version="4.13.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="6.2.0" />
<PackageVersion Include="OneOf" Version="3.0.271" />
Expand Down
13 changes: 4 additions & 9 deletions TUnit.Analyzers.Tests/MSTestMigrationAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -719,16 +719,13 @@ public async Task TestWithMessages()
[Test]
public async Task MSTest_Assertions_With_FormatStrings_Converted()
{
// Note: The diagnostic is on [TestMethod] because Assert.AreEqual with format strings
// isn't a valid MSTest overload, so semantic model doesn't resolve it.
// The analyzer detects the method attribute instead of the Assert call.
await CodeFixer.VerifyCodeFixAsync(
"""
using Microsoft.VisualStudio.TestTools.UnitTesting;

public class MyClass
{|#0:public class MyClass|}
{
{|#0:[TestMethod]|}
[TestMethod]
public void TestWithFormatStrings()
{
int x = 5;
Expand Down Expand Up @@ -761,16 +758,14 @@ public async Task MSTest_Assertions_With_Comparer_AddsTodoComment()
{
// When a comparer is detected (via semantic or syntax-based detection),
// a TODO comment is added explaining that TUnit uses different comparison semantics.
// Note: The diagnostic is on [TestMethod] because Assert.AreEqual with comparer
// isn't a valid MSTest overload, so semantic model doesn't resolve it.
await CodeFixer.VerifyCodeFixAsync(
"""
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

public class MyClass
{|#0:public class MyClass|}
{
{|#0:[TestMethod]|}
[TestMethod]
public void TestWithComparer()
{
var comparer = StringComparer.OrdinalIgnoreCase;
Expand Down
33 changes: 19 additions & 14 deletions TUnit.Analyzers/Migrators/Base/BaseMigrationAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ protected virtual bool HasFrameworkTypes(SyntaxNodeAnalysisContext context, INam
foreach (var invocation in invocationExpressions)
{
var symbolInfo = context.SemanticModel.GetSymbolInfo(invocation);
if (symbolInfo.Symbol is IMethodSymbol methodSymbol)
var methodSymbol = symbolInfo.Symbol as IMethodSymbol
?? symbolInfo.CandidateSymbols.OfType<IMethodSymbol>().FirstOrDefault();

if (methodSymbol != null)
{
var namespaceName = methodSymbol.ContainingNamespace?.ToDisplayString();

Expand All @@ -228,22 +231,24 @@ protected virtual bool HasFrameworkTypes(SyntaxNodeAnalysisContext context, INam
return true;
}
}
else if (symbolInfo.Symbol == null)
else if (invocation.Expression is MemberAccessExpressionSyntax memberAccess)
{
// Fallback: if symbol resolution fails completely, check the syntax directly
// This handles cases where the semantic model hasn't fully resolved types
// Note: If TUnit is available, we already returned false above, so this only
// runs when TUnit is not present (pure source framework project).
if (invocation.Expression is MemberAccessExpressionSyntax memberAccess)
// Method symbol couldn't be resolved (e.g. overload removed across framework
// versions). Try the receiver: if it resolves to a framework type, treat the
// call as framework usage.
var receiverSymbol = context.SemanticModel.GetSymbolInfo(memberAccess.Expression).Symbol;
if (receiverSymbol is INamedTypeSymbol receiverType && IsFrameworkType(receiverType))
{
var typeExpression = memberAccess.Expression.ToString();
return true;
}

// For framework-specific types, only flag if the framework is still available
// This prevents flagging after migration when the framework assembly has been removed
if (IsFrameworkTypeName(typeExpression) && IsFrameworkAvailable(context.SemanticModel.Compilation))
{
return true;
}
// Final fallback: pure syntactic match for framework-specific type names.
// Only flag when the framework assembly is still available, to avoid false
// positives after migration has removed it.
var typeExpression = memberAccess.Expression.ToString();
if (IsFrameworkTypeName(typeExpression) && IsFrameworkAvailable(context.SemanticModel.Compilation))
{
return true;
}
}
}
Expand Down
Loading