From 8b730c56195289d99c86df244cbc7b071a2863e1 Mon Sep 17 00:00:00 2001 From: DoctorKrolic <70431552+DoctorKrolic@users.noreply.github.com> Date: Wed, 16 Mar 2022 02:40:02 +0300 Subject: [PATCH] Added aditional test to "ForeachToFor" refactoring (#60186) --- .../ConvertForEachToForTests.cs | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/EditorFeatures/CSharpTest/ConvertForEachToFor/ConvertForEachToForTests.cs b/src/EditorFeatures/CSharpTest/ConvertForEachToFor/ConvertForEachToForTests.cs index 9969417f18c43..7a4dfe303933a 100644 --- a/src/EditorFeatures/CSharpTest/ConvertForEachToFor/ConvertForEachToForTests.cs +++ b/src/EditorFeatures/CSharpTest/ConvertForEachToFor/ConvertForEachToForTests.cs @@ -27,7 +27,7 @@ protected override CodeRefactoringProvider CreateCodeRefactoringProvider( private readonly CodeStyleOption2 onWithSilent = new CodeStyleOption2(true, NotificationOption2.Silent); private OptionsCollection ImplicitTypeEverywhere - => new OptionsCollection(GetLanguage()) + => new(GetLanguage()) { { CSharpCodeStyleOptions.VarElsewhere, onWithSilent }, { CSharpCodeStyleOptions.VarWhenTypeIsApparent, onWithSilent }, @@ -1788,6 +1788,40 @@ void Method() await TestInRegularAndScriptAsync(text, expected); } + [WorkItem(50469, "https://github.com/dotnet/roslyn/issues/50469")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertForEachToFor)] + public async Task PreventExplicitCastToVar() + { + var text = @" +class Test +{ + void Method() + { + var items = new[] { new { x = 1 } }; + + foreach [||] (var item in items) + { + } + } +} +"; + var expected = @" +class Test +{ + void Method() + { + var items = new[] { new { x = 1 } }; + + for (int {|Rename:i|} = 0; i < items.Length; i++) + { + var item = items[i]; + } + } +} +"; + await TestInRegularAndScriptAsync(text, expected); + } + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertForEachToFor)] public async Task NotAssignable() {