dotnet / dotnet/roslyn

Exception in ExtractMethodCodeRefactoringProvider

Open
#82,031 2 comments 0 reactions 1 assignee Claimed by @DeborahOlaboye View on GitHub
Area-IDE Feature - Extract Method
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Version Used**: VS 18.2.0

**Steps to Reproduce**:
Select the entire `while` statement with the mouse or the keyboard.

```csharp
class C
{
public void M()
{
while (Read() is not -1 and var raw)
{
_ = (char)raw;
}
}

int Read() { return 0; }
}
```

**Expected Behavior**: No exception.

**Actual Behavior**:
`ExtractMethodCodeRefactoringProvider` reports an exception:
```
System.InvalidOperationException : Sequence contains no elements
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at Microsoft.CodeAnalysis.ExtractMethod.Extensions.GetTokenWithAnnotation(SemanticDocument document,SyntaxAnnotation annotation)
at Microsoft.CodeAnalysis.CSharp.ExtractMethod.CSharpExtractMethodService.CSharpMethodExtractor.GetInsertionPointNode(AnalyzerResult analyzerResult,CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.ExtractMethod.AbstractExtractMethodService`3.MethodExtractor.ExtractMethod(OperationStatus initialStatus,CancellationToken cancellationToken)
at async Microsoft.CodeAnalysis.ExtractMethod.AbstractExtractMethodService`3.ExtractMethodAsync[TStatementSyntax,TExecutableStatementSyntax,TExpressionSyntax]()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeRefactorings.ExtractMethod.ExtractMethodCodeRefactoringProvider.ExtractLocalFunctionAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeRefactorings.ExtractMethod.ExtractMethodCodeRefactoringProvider.GetCodeActionsAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeRefactorings.ExtractMethod.ExtractMethodCodeRefactoringProvider.ComputeRefactoringsAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeRefactorings.CodeRefactoringService.<>c__DisplayClass12_0.b__0()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T]()
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.