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

ArgumentException in DisposeFieldsAndPropertiesInTearDownAnalyzer #635

Closed
MaceWindu opened this issue Nov 10, 2023 · 2 comments · Fixed by #658
Closed

ArgumentException in DisposeFieldsAndPropertiesInTearDownAnalyzer #635

MaceWindu opened this issue Nov 10, 2023 · 2 comments · Fixed by #658
Milestone

Comments

@MaceWindu
Copy link

Analyzer 'NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer' threw an exception of type 'System.ArgumentException' with message 'Syntax node is not within syntax tree'
1>    SyntaxNode: public partial class TestFixture ... [ClassDeclarationSyntax]@[17..158) (2,0)-(8,1)
1>    System.ArgumentException: Syntax node is not within syntax tree
1>    at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.CheckSyntaxNode(CSharpSyntaxNode syntax)
1>    at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.GetSymbolInfo(ExpressionSyntax expression, CancellationToken cancellationToken)
1>    at Microsoft.CodeAnalysis.CSharp.CSharpExtensions.GetSymbolInfo(SemanticModel semanticModel, ExpressionSyntax expression, CancellationToken cancellationToken)
1>    at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.Parameters.IsLocalMethodCall(InvocationExpressionSyntax invocationExpression, IMethodSymbol& calledMethod)
1>    at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AssignedIn(Parameters parameters, HashSet`1 assignments, ExpressionSyntax expression)
1>    at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AssignedIn(Parameters parameters, HashSet`1 assignments, SyntaxList`1 statements)
1>    at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AssignedIn(Parameters parameters, HashSet`1 assignments, BaseMethodDeclarationSyntax method)
1>    at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AssignedIn(Parameters parameters, HashSet`1 assignments, IMethodSymbol symbol)
1>    at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AssignedIn(Parameters parameters, HashSet`1 assignments, IEnumerable`1 methods)
1>    at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AnalyzeAssignedButNotDisposed(SyntaxNodeAnalysisContext context, Dictionary`2 symbols, Parameters parameters, String where, IEnumerable`1 setUpMethods, IEnumerable`1 tearDownMethods, HashSet`1 assignedWithInitializers)
1>    at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AnalyzeDisposableFields(SyntaxNodeAnalysisContext context)
1>    at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__49`1.<ExecuteSyntaxNodeAction>b__49_0(ValueTuple`2 data)
1>    at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)

NUnit: 3.14.0
NUnit.Analyzers: 3.9.0

using System;

public partial class TestFixture
{
    private static object Property
    {
        get => throw new NotImplementedException();
    }
}
using NUnit.Framework;
using System;

public partial class TestFixture
{
	[Test]
	public void SomeTest()
	{
		SomeAsserts();
	}

	private static void SomeAsserts() => throw new InvalidOperationException();
}
@manfred-brands
Copy link
Member

Thanks @MaceWindu for the report.
I never used partial classes with nunit tests.
It looks as if it tries to find a symbol which is defined in the other partial part.

I have no access to a PC this weekend, but will look into this next week.

@manfred-brands
Copy link
Member

@MaceWindu I can reproduce this in an nunit test.

If your property is readonly, a workaround might be to replace the get accessor with an expression:

private static object Property => throw new NotImplementedException();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants