You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When applying EmptyMethodCodeFix to an empty top-level method (available from C# 9.0) the analyzer throws an exception (AD0001):
Stack Trace:
EmptyMethodCodeFix.RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context) line 51
SonarCodeFix.RegisterCodeFixesAsync(CodeFixContext context) line 45
--- End of inner exception stack trace ---
Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
Task.Wait()
CodeFixVerifier.ActionToApply(CodeFixProvider codeFix, Document document, Diagnostic diagnostic) line 94
<>c__DisplayClass6_0.<VerifyWhileDocumentChanges>b__1(Diagnostic x) line 56
SelectManySingleSelectorIterator`2.MoveNext()
Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
CodeFixVerifier.VerifyWhileDocumentChanges(ParseOptions parseOptions, String pathToExpected) line 54
Verifier.VerifyCodeFix() line 117
VerifierBuilderExtensions.VerifyCodeFix(VerifierBuilder builder) line 161
Repro steps
In Visual Studio put the cursor on EmptyTopLevelMethod in the snippet below, press ctrl + dot and see the analyzer except.
voidEmptyTopLevelMethod(){}
Note
The RegisterCodeFixesAsync throws a NullReferenceException, after it unsuccessfully attempts to find the method declaration node: syntaxNode.FirstAncestorOrSelf<MethodDeclarationSyntax>();
A top-level method is always considered a local function (LocalFunctionStatementSyntax) rather than a MethodDeclarationSyntax.
This can be reproduced in a unit test by adding a code fix test for an empty top-level method.
Another problem - caused by the same issue - is when a local function is inside another method:
If we apply the Throw fix for LocalFunction, then it will remove the code of the outer method (as well as the local function) and replace it with a throw statement:
Hi @jnyrup. Thank you for reporting this error. So far I've been unable to replicate it using SonarLint 6.14.1.66430 and Visual Studio 2022 17.6.0 Preview 2.0. Could you provide the code of the class - if it isn't too long or confidential - that contains the _ method?
zsolt-kolbay-sonarsource
changed the title
NullReferenceException from EmptyMethodCodeFix
Fix AD0001 on S1186: NullReferenceException for top-level methods
Apr 12, 2023
Description
When applying
EmptyMethodCodeFix
to an empty top-level method (available from C# 9.0) the analyzer throws an exception (AD0001):Repro steps
In Visual Studio put the cursor on
EmptyTopLevelMethod
in the snippet below, pressctrl + dot
and see the analyzer except.Note
The
RegisterCodeFixesAsync
throws aNullReferenceException
, after it unsuccessfully attempts to find the method declaration node:syntaxNode.FirstAncestorOrSelf<MethodDeclarationSyntax>();
A top-level method is always considered a local function (
LocalFunctionStatementSyntax
) rather than aMethodDeclarationSyntax
.This can be reproduced in a unit test by adding a code fix test for an empty top-level method.
Another problem - caused by the same issue - is when a local function is inside another method:
If we apply the Throw fix for
LocalFunction
, then it will remove the code of the outer method (as well as the local function) and replace it with a throw statement:Summa summarum, additional test cases are needed for local functions in general, not just for top-level methods when code fixes are involved.
Related information
The text was updated successfully, but these errors were encountered: