Roslyn-analyzers: 'BasicDoNotUseCountWhenAnyCanBeUsedFixer' encountered an error

Created on 14 Jun 2020  路  17Comments  路  Source: dotnet/roslyn-analyzers

Analyzer package

Microsoft.CodeAnalysis.FxCopAnalyzers

Package Version

v3.0.0 (Latest)

Diagnostic ID

CA1827

Repro steps

image

StackTrace

System.NullReferenceException : Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.SyntaxNodeExtensions.WithTrailingTrivia[TSyntax](TSyntax node,IEnumerable`1 trivia)
   at async Microsoft.NetCore.Analyzers.Performance.DoNotUseCountWhenAnyCanBeUsedFixer.DoNotUseCountWhenAnyCanBeUsedCodeAction.GetChangedDocumentAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)
   at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)

Note: The fix message mentioned AnyAsync and CountAsync, while my method isn't async at all.

image

Note2: I haven't tested with the latest master.

Bug Verified in progress

Most helpful comment

Here is the repro steps

  1. Create a new .NET Core VB Console App
  2. Install version 3.0 of FxCopAnalyzers
  3. Use the following code:

    Imports System
    
    Module Program
        Sub Main(args As String())
            Console.WriteLine("Hello World!")
        End Sub
    
        Sub TestSub(x As IEnumerable(Of Integer))
            If x.Count = 0 Then Return
        End Sub
    End Module
    
  4. Put the cursor on Count.

  5. Press CTRL+.

  6. The exception is thrown with the stack trace:

System.NullReferenceException : Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.SyntaxNodeExtensions.WithTrailingTrivia[TSyntax](TSyntax node,IEnumerable`1 trivia)
   at async Microsoft.NetCore.Analyzers.Performance.DoNotUseCountWhenAnyCanBeUsedFixer.DoNotUseCountWhenAnyCanBeUsedCodeAction.GetChangedDocumentAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)
   at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)

All 17 comments

@Youssef1313 As far as I can see when looking at the StackTrace and at the fixer implementation (and history) this seems to be an issue on roslyn's side.

If that's the case, @mavasani can you move this to Roslyn repo?

I just remembered this part:

Note: The fix message mentioned AnyAsync and CountAsync, while my method isn't async at all.

@Evangelink, I think this should be issue on roslyn-analyzers side, and the exception on roslyn side, right?

Oh yes! I also forgot about the other part of the issue!

I'll keep this open here for the other part, and open another issue in Roslyn now.

SyntaxNodeExtensions.WithTrailingTriviaTSyntax is just:

c# public static TSyntax WithTrailingTrivia<TSyntax>( this TSyntax node, IEnumerable<SyntaxTrivia>? trivia) where TSyntax : SyntaxNode { var last = node.GetLastToken(includeZeroWidth: true); var newLast = last.WithTrailingTrivia(trivia); return node.ReplaceToken(last, newLast); }

A null ref here is just either that node was null (caller error). or node.GetLastToken() returned null. Feels extremely unlikely for the latter to be true. Absent a repro, we woudl not be able to fix this.

@mavasani says this was fixed in https://github.com/dotnet/roslyn-analyzers/commit/2404ec0009697e2f85bc903343bc15cbe6030165#diff-3d9e4124e9d936082db4831148dac9ca here: https://github.com/dotnet/roslyn-analyzers/issues/3087

I'm going to close this out.

Hrmm.. reopening. other issue says it's fixed for 2.9.9, but this reported against 3.0

In the future, if you can please supply a repro source case, that would be very useful.

Here is the repro steps

  1. Create a new .NET Core VB Console App
  2. Install version 3.0 of FxCopAnalyzers
  3. Use the following code:

    Imports System
    
    Module Program
        Sub Main(args As String())
            Console.WriteLine("Hello World!")
        End Sub
    
        Sub TestSub(x As IEnumerable(Of Integer))
            If x.Count = 0 Then Return
        End Sub
    End Module
    
  4. Put the cursor on Count.

  5. Press CTRL+.

  6. The exception is thrown with the stack trace:

System.NullReferenceException : Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.SyntaxNodeExtensions.WithTrailingTrivia[TSyntax](TSyntax node,IEnumerable`1 trivia)
   at async Microsoft.NetCore.Analyzers.Performance.DoNotUseCountWhenAnyCanBeUsedFixer.DoNotUseCountWhenAnyCanBeUsedCodeAction.GetChangedDocumentAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)
   at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)

Thanks. super super helpful!

@Youssef1313 Do you know if the issues is fixed or not?

@Evangelink I'll test and tell you shortly.

The exception is still repro with 3.3.0-beta2.final
The extra "Async" in the message is fixed.

Note: This doesn't repro if I added parentheses after Count.
So, something goes wrong when the function parentheses in VB are omitted

The VB version of the fixer wasn't updated with the same changes in 2404ec0. I suspect this could be the problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

onyxmaster picture onyxmaster  路  3Comments

paulomorgado picture paulomorgado  路  3Comments

lgolding picture lgolding  路  4Comments

x3ntrix picture x3ntrix  路  3Comments

paulomorgado picture paulomorgado  路  3Comments