Skip to content

NRE in WTG1010 when analyzing a conditional invoke. #232

@brian-reichle

Description

@brian-reichle

WTG.Analyzers: 3.7.2
VS: 17.14.3

Analyzing this line of code:

if (maxCpuTimeHit?.TryGetValue("QueryHash", out object queryHashObj) ?? false)

Resulted in this error:

Analyzer 'WTG.Analyzers.VarAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.
Exception occurred with following context:
Compilation: ZClientEDI
SyntaxTree: C:\Dev\Enterprise\ClientExtensions\EDI\ZClientEDI\ZClientEDI\ServiceTasks\ExternalMonitoringAlert\SqlCpuUsageAlertProvider.cs
SyntaxNode: .TryGetValue("QueryHash", out object ... [InvocationExpressionSyntax]@[23133..23183) (467,26)-(467,76)

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.CSharp.SyntaxFactory.FindConditionalAccessNodeForBinding(CSharpSyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.Binder.GetReceiverForConditionalBinding(ExpressionSyntax binding, BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindMemberBindingExpression(MemberBindingExpressionSyntax node, Boolean invoked, Boolean indexed, BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node, BindingDiagnosticBag diagnostics, Boolean invoked, Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpression(ExpressionSyntax node, BindingDiagnosticBag diagnostics, Boolean invoked, Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindMethodGroup(ExpressionSyntax node, Boolean invoked, Boolean indexed, BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindInvocationExpression(InvocationExpressionSyntax node, BindingDiagnosticBag diagnostics)
   at Microsoft.CodeAnalysis.CSharp.Binder.BindExpressionInternal(ExpressionSyntax node, BindingDiagnosticBag diagnostics, Boolean invoked, Boolean indexed)
   at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.GetSpeculativelyBoundExpressionHelper(Binder binder, ExpressionSyntax expression, SpeculativeBindingOption bindingOption)
   at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.GetSpeculativelyBoundExpressionWithoutNullability(Int32 position, ExpressionSyntax expression, SpeculativeBindingOption bindingOption, Binder& binder, ImmutableArray`1& crefSymbols)
   at Microsoft.CodeAnalysis.CSharp.MemberSemanticModel.GetSpeculativelyBoundExpression(Int32 position, ExpressionSyntax expression, SpeculativeBindingOption bindingOption, Binder& binder, ImmutableArray`1& crefSymbols)
   at Microsoft.CodeAnalysis.CSharp.SyntaxTreeSemanticModel.GetSpeculativelyBoundExpression(Int32 position, ExpressionSyntax expression, SpeculativeBindingOption bindingOption, Binder& binder, ImmutableArray`1& crefSymbols)
   at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.GetSpeculativeSymbolInfo(Int32 position, ExpressionSyntax expression, SpeculativeBindingOption bindingOption)
   at WTG.Analyzers.VarAnalyzer.AnalyzeInvoke(SyntaxNodeAnalysisContext context, FileDetailCache cache)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)
-----

Suppress the following diagnostics to disable this analyzer: WTG1002, WTG1010, WTG1012

I suspect this is because of the conditional invocation. We get the invocation, change the type of the out parameter to var and then perform speculative type resolution to determine if that changes the identified type. But with conditional invocation, the information about the target of the invocation is not contained within the invocation syntax node and so gets 'chopped off'. If the target is a MemberBindingExpression then we should probably step out to the ConditionalAccessExpression before rewriting the types so that the speculative type resolution has the required information. (more investigation needed to confirm)

Metadata

Metadata

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions