dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.95k stars 4.02k forks source link

NullReferenceException in CSharpUseImplicitTypeDiagnosticAnalyzer #21202

Open sharwell opened 7 years ago

sharwell commented 7 years ago

Version Used: 15.3 Preview 6

:link: https://devdiv.visualstudio.com/DevDiv/Extensibility/_workitems/edit/459424 (Watson which suggested it's possible to hit this - 131 reports first appearing with 15.3 Preview 3)

Steps to Reproduce:

  1. Check out dotnet/roslyn
  2. Delete src/Compilers/.editorconfig
  3. Open AdditionalTextFile.cs
  4. Type code¹ in the GetText method

¹ I reproduced this one, but I'm finding it difficult to reproduce this again. It would be extremely helpful if someone was able to find a specific case that reproduces this based on the SyntaxNode information in the error text below.

Expected Behavior:

AD0001 is not reported (i.e. NullReferenceException is not thrown).

Actual Behavior:

AD0001 is reported.

Error AD0001: 'Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle.CSharpUseImplicitTypeDiagnosticAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.

Stack Trace:

Exception occurred with following context:
Compilation: Microsoft.CodeAnalysis
SyntaxTree: J:\dev\roslyn\src\Compilers\Core\Portable\AdditionalTextFile.cs
SyntaxNode: ref x = new  [VariableDeclarationSyntax]@[1611..1625) (45,12)-(46,0)

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle.CSharpUseImplicitTypeDiagnosticAnalyzer.AssignmentSupportsStylePreference(SyntaxToken identifier, TypeSyntax typeName, ExpressionSyntax initializer, SemanticModel semanticModel, OptionSet optionSet, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle.CSharpUseImplicitTypeDiagnosticAnalyzer.TryAnalyzeVariableDeclaration(TypeSyntax typeName, SemanticModel semanticModel, OptionSet optionSet, CancellationToken cancellationToken, TextSpan& issueSpan)
   at Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle.CSharpTypeStyleDiagnosticAnalyzerBase.HandleVariableDeclaration(SyntaxNodeAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__43`1.<ExecuteSyntaxNodeAction>b__43_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
sharwell commented 7 years ago

⚠️ I'm seeing a huge spike in reports of this issue (3600+).

gulbanana commented 7 years ago

I've just run into it today :(

Repro: Compile Microsoft.Extensions.Logging. This function in LoggerFactory.cs will trigger the bug:

        private void ApplyRules(LoggerInformation[] loggers, string categoryName, int start, int count)
        {
            for (var index = start; index < start + count; index++)
            {
                ref var loggerInformation = ref loggers[index];

                _RuleSelector.Select(_filterOptions,
                    loggerInformation.ProviderType,
                    categoryName,
                    out var minLevel,
                    out var filter);

                loggerInformation.Category = categoryName;
                loggerInformation.MinLevel = minLevel;
                loggerInformation.Filter = filter;
            }
        }
mavasani commented 5 years ago

Also reported here: https://developercommunity.visualstudio.com/content/problem/285267/csharpuseimplicittypediagnosticanalyzer-nullrefere.html