bkoelman / ResharperCodeContractNullability

Reports diagnostics, helping you to annotate your source tree with (Item)NotNull / (Item)CanBeNull attributes.
https://www.jetbrains.com/resharper/help/Code_Analysis__Code_Annotations.html
Apache License 2.0
26 stars 4 forks source link

NRE in 'Fix All' -> Solution #41

Closed deniszykov closed 3 years ago

deniszykov commented 3 years ago

Version Used: 2.0.0

Steps to Reproduce:

  1. Have a huge project
  2. Have a nullable = enabled + JetBrains.Annotations around a project
  3. Choose 'Convert to C# Syntax' -> Solution

Expected Behavior: No error.

Actual Behavior:

System.ArgumentNullException : Value cannot be null.
Parameter name: declarationSymbol
   at CodeContractNullability.Conversion.NullConversionScope..ctor(NullConversionContext context,SyntaxNode declarationSyntax,ISymbol declarationSymbol)
   at async CodeContractNullability.NullableReferenceTypeConversionCodeFixProvider.ChangeSolutionAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionInternalAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeFixes.BatchFixAllProvider.GetChangedDocumentsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeFixes.BatchFixAllProvider.GetDocumentIdToChangedDocuments(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeFixes.BatchFixAllProvider.TryMergeFixesAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeFixes.BatchFixAllProvider.TryGetMergedFixAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeFixes.BatchFixAllProvider.GetFixAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeFixes.BatchFixAllProvider.GetFixAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.FixAllGetFixesService.GetFixAllCodeActionAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.FixAllGetFixesService.GetFixAllOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetOperationsCoreAsync(<Unknown Parameters>)
   at Roslyn.Utilities.TaskExtensions.WaitAndGetResult_CanCallOnBackground[T](Task`1 task,CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.InvokeWorker(Func`1 getFromDocument,IProgressTracker progressTracker,CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.<>c__DisplayClass20_0.<InvokeCore>b__0()
   at Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformAction(IExtensionManager extensionManager,Object extension,Action action)
bkoelman commented 3 years ago

Thanks for reporting this. I'm unable to reproduce, unfortunately. Can you narrow this down to a single project, then to the affected source file and submit a small repro?

deniszykov commented 3 years ago

This is difficult to narrow, because it is huge. I found workaround for me, I just removed all [[NotNull]] attributes with text replacement.

This code could give null because it based on GetDeclaredSymbolCore which could return null.

You could skip syntax which is not declaration, or generate warning/message to output.

bkoelman commented 3 years ago

Yes, but almost everything in roslyn returns null under some circumstances. For example, when typing code that is not yet valid syntax. The point is to validate upfront and skip analysis in those cases, instead of sprinkling the code full with null checks everywhere. So the question is: what piece of code is causing this?

bkoelman commented 3 years ago

You could skip syntax which is not declaration, or generate warning/message to output.

As far as I'm aware, analysis is configured to only run on type/member/parameter declarations, so I suspect something else is causing the error.

deniszykov commented 3 years ago

I will try to locate this error tomorrow.

bkoelman commented 3 years ago

Thanks. The next steps should make it easier to find the source file that causes this:

Note that the second VS instance will be very slow with attached debugger, so it may take some time to get there. Hope this helps.

deniszykov commented 3 years ago

This code cause error:

public class A
{

    [NotNull]
    public static implicit operator A([NotNull] string value)
    {
        return new A();
    }

}
deniszykov commented 3 years ago

BTW, Thanks for your package. It helped me a lot.

bkoelman commented 3 years ago

Thank you for investigating the root cause. I can repro the problem now and the fix is coming soon.

Glad to hear my package is useful to you. Please thank me by adding a star to the repo. :)