From c7349324e98e5a9dbc38b9c8a89f09b41d3c2012 Mon Sep 17 00:00:00 2001 From: vsadov Date: Thu, 18 Feb 2016 18:51:57 -0800 Subject: [PATCH] Suppressing inline temp refactoring on byref locals. The feature needs to be properly implemented taking into account that variable is byref. For now disabling this refactoring if variable happen to be byref to avoid crash as described in #8416 --- .../InlineTemporary/InlineTemporaryTests.cs | 22 +++++++++++++++++++ .../InlineTemporaryCodeRefactoringProvider.cs | 6 +++++ 2 files changed, 28 insertions(+) diff --git a/src/EditorFeatures/CSharpTest/CodeActions/InlineTemporary/InlineTemporaryTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/InlineTemporary/InlineTemporaryTests.cs index 193e364770f1b..5bfe6ebee54db 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/InlineTemporary/InlineTemporaryTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/InlineTemporary/InlineTemporaryTests.cs @@ -5,11 +5,16 @@ using Microsoft.CodeAnalysis.CSharp.CodeRefactorings.InlineTemporary; using Roslyn.Test.Utilities; using Xunit; +using System.Collections.Generic; namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings.InlineTemporary { public class InlineTemporaryTests : AbstractCSharpCodeActionTest { + private static readonly Dictionary s_experimentalFeatures = new Dictionary { { "localFunctions", "true" }, { "refLocalsAndReturns", "true" } }; + public static readonly CSharpParseOptions ExperimentalParseOptions = + new CSharpParseOptions(kind: SourceCodeKind.Regular, documentationMode: DocumentationMode.None, languageVersion: LanguageVersion.CSharp6).WithFeatures(s_experimentalFeatures); + protected override object CreateCodeRefactoringProvider(Workspace workspace) { return new InlineTemporaryCodeRefactoringProvider(); @@ -62,6 +67,23 @@ public async Task NotOnField() await TestMissingAsync(@"class C { int [||]x = 42; void M() { System.Console.WriteLine(x); } }"); } + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTemporary)] + public async Task WithRefInitializer1() + { + await TestMissingAsync(@" +class C +{ + ref int M() + { + int[] arr = new[] { 1, 2, 3 }; + ref int [||]x = ref arr[2]; + return ref x; + } +}", + // TODO: propagating features to the project is currently NYI + parseOptions: ExperimentalParseOptions); + } + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTemporary)] public async Task SingleStatement() { diff --git a/src/Features/CSharp/Portable/CodeRefactorings/InlineTemporary/InlineTemporaryCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/CodeRefactorings/InlineTemporary/InlineTemporaryCodeRefactoringProvider.cs index 3706658c2b73d..bc69463d8c66c 100644 --- a/src/Features/CSharp/Portable/CodeRefactorings/InlineTemporary/InlineTemporaryCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/CodeRefactorings/InlineTemporary/InlineTemporaryCodeRefactoringProvider.cs @@ -69,6 +69,12 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte return; } + if (variableDeclarator.Initializer.RefKeyword.Kind() != SyntaxKind.None) + { + // TODO: inlining byref temps is NYI + return; + } + if (localDeclarationStatement.ContainsDiagnostics) { return;