Skip to content

Commit ccae450

Browse files
Merge pull request #72073 from CyrusNajmabadi/sgCrash
Do not attempt to update generated code in 'Convert to record'
2 parents e4b5e0c + 1943827 commit ccae450

File tree

3 files changed

+209
-140
lines changed

3 files changed

+209
-140
lines changed

src/Analyzers/CSharp/CodeFixes/ConvertToRecord/ConvertToRecordEngine.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,15 @@ private static async Task RefactorInitializersAsync(
476476
var symbolReferences = await SymbolFinder
477477
.FindReferencesAsync(type, solutionEditor.OriginalSolution, cancellationToken).ConfigureAwait(false);
478478
var referenceLocations = symbolReferences.SelectMany(reference => reference.Locations);
479-
var documentLookup = referenceLocations.ToLookup(refLoc => refLoc.Document.Id);
480-
foreach (var (documentID, documentLocations) in documentLookup)
479+
var documentLookup = referenceLocations.ToLookup(refLoc => refLoc.Document);
480+
foreach (var (document, documentLocations) in documentLookup)
481481
{
482+
// We don't want to process source-generated documents. Make sure we can get back to a real document here.
483+
if (document is SourceGeneratedDocument)
484+
continue;
485+
482486
var documentEditor = await solutionEditor
483-
.GetDocumentEditorAsync(documentID, cancellationToken).ConfigureAwait(false);
487+
.GetDocumentEditorAsync(document.Id, cancellationToken).ConfigureAwait(false);
484488
if (documentEditor.OriginalDocument.Project.Language != LanguageNames.CSharp)
485489
{
486490
// since this is a CSharp-dependent file, we need to have specific VB support.

src/Analyzers/CSharp/Tests/ConvertToRecord/ConvertToRecordCodeFixTests.cs

Lines changed: 132 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -2,181 +2,176 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
6-
using System.Collections.Generic;
75
using System.Collections.Immutable;
86
using System.Threading.Tasks;
97
using Microsoft.CodeAnalysis.CSharp;
108
using Microsoft.CodeAnalysis.CSharp.ConvertToRecord;
119
using Microsoft.CodeAnalysis.Diagnostics;
1210
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
1311
using Microsoft.CodeAnalysis.Test.Utilities;
14-
using Microsoft.CodeAnalysis.Testing;
15-
using Roslyn.Test.Utilities;
1612
using Xunit;
1713

18-
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.ConvertToRecord
14+
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.ConvertToRecord;
15+
16+
[Trait(Traits.Feature, Traits.Features.CodeActionsConvertToRecord)]
17+
public class ConvertToRecordCodeFixTests
1918
{
20-
[Trait(Traits.Feature, Traits.Features.CodeActionsConvertToRecord)]
21-
public class ConvertToRecordCodeFixTests
19+
[Fact]
20+
public async Task TestMovePropertySimpleRecordInheritance_CodeFix()
2221
{
23-
[Fact]
24-
public async Task TestMovePropertySimpleRecordInheritance_CodeFix()
25-
{
26-
var initialMarkup = """
27-
namespace N
22+
var initialMarkup = """
23+
namespace N
24+
{
25+
public record B
2826
{
29-
public record B
30-
{
31-
public int Foo { get; init; }
32-
}
27+
public int Foo { get; init; }
28+
}
3329
34-
public class C : [|B|]
35-
{
36-
public int P { get; init; }
37-
}
30+
public class C : [|B|]
31+
{
32+
public int P { get; init; }
3833
}
39-
""";
40-
var changedMarkup = """
41-
namespace N
34+
}
35+
""";
36+
var changedMarkup = """
37+
namespace N
38+
{
39+
public record B
4240
{
43-
public record B
44-
{
45-
public int Foo { get; init; }
46-
}
47-
48-
public record C(int P) : B;
41+
public int Foo { get; init; }
4942
}
50-
""";
51-
await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false);
52-
}
5343
54-
[Fact]
55-
public async Task TestMovePropertyPositionalParameterRecordInheritance_CodeFix()
56-
{
57-
var initialMarkup = """
58-
namespace N
59-
{
60-
public record B(int Foo, int Bar);
44+
public record C(int P) : B;
45+
}
46+
""";
47+
await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false);
48+
}
6149

62-
public class {|CS1729:C|} : [|B|]
63-
{
64-
public int P { get; init; }
65-
}
66-
}
67-
""";
68-
var changedMarkup = """
69-
namespace N
70-
{
71-
public record B(int Foo, int Bar);
50+
[Fact]
51+
public async Task TestMovePropertyPositionalParameterRecordInheritance_CodeFix()
52+
{
53+
var initialMarkup = """
54+
namespace N
55+
{
56+
public record B(int Foo, int Bar);
7257
73-
public record C(int Foo, int Bar, int P) : B(Foo, Bar);
58+
public class {|CS1729:C|} : [|B|]
59+
{
60+
public int P { get; init; }
7461
}
75-
""";
76-
await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false);
77-
}
62+
}
63+
""";
64+
var changedMarkup = """
65+
namespace N
66+
{
67+
public record B(int Foo, int Bar);
7868
79-
[Fact]
80-
public async Task TestMovePropertyPositionalParameterRecordInheritanceWithComments_CodeFix()
81-
{
82-
var initialMarkup = """
83-
namespace N
84-
{
85-
/// <summary> B </summary>
86-
/// <param name="Foo"> Foo is an int </param>
87-
/// <param name="Bar"> Bar is an int as well </param>
88-
public record B(int Foo, int Bar);
69+
public record C(int Foo, int Bar, int P) : B(Foo, Bar);
70+
}
71+
""";
72+
await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false);
73+
}
8974

90-
/// <summary> C inherits from B </summary>
91-
public class {|CS1729:C|} : [|B|]
92-
{
93-
/// <summary> P can be initialized </summary>
94-
public int P { get; init; }
95-
}
96-
}
97-
""";
98-
var changedMarkup = """
99-
namespace N
75+
[Fact]
76+
public async Task TestMovePropertyPositionalParameterRecordInheritanceWithComments_CodeFix()
77+
{
78+
var initialMarkup = """
79+
namespace N
80+
{
81+
/// <summary> B </summary>
82+
/// <param name="Foo"> Foo is an int </param>
83+
/// <param name="Bar"> Bar is an int as well </param>
84+
public record B(int Foo, int Bar);
85+
86+
/// <summary> C inherits from B </summary>
87+
public class {|CS1729:C|} : [|B|]
10088
{
101-
/// <summary> B </summary>
102-
/// <param name="Foo"> Foo is an int </param>
103-
/// <param name="Bar"> Bar is an int as well </param>
104-
public record B(int Foo, int Bar);
105-
106-
/// <summary> C inherits from B </summary>
107-
/// <param name="Foo"><inheritdoc/></param>
108-
/// <param name="Bar"><inheritdoc/></param>
109-
/// <param name="P"> P can be initialized </param>
110-
public record C(int Foo, int Bar, int P) : B(Foo, Bar);
89+
/// <summary> P can be initialized </summary>
90+
public int P { get; init; }
11191
}
112-
""";
113-
await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false);
114-
}
92+
}
93+
""";
94+
var changedMarkup = """
95+
namespace N
96+
{
97+
/// <summary> B </summary>
98+
/// <param name="Foo"> Foo is an int </param>
99+
/// <param name="Bar"> Bar is an int as well </param>
100+
public record B(int Foo, int Bar);
101+
102+
/// <summary> C inherits from B </summary>
103+
/// <param name="Foo"><inheritdoc/></param>
104+
/// <param name="Bar"><inheritdoc/></param>
105+
/// <param name="P"> P can be initialized </param>
106+
public record C(int Foo, int Bar, int P) : B(Foo, Bar);
107+
}
108+
""";
109+
await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false);
110+
}
115111

116-
[Fact]
117-
public async Task TestMovePropertyAndReorderWithPositionalParameterRecordInheritance_CodeFix()
118-
{
119-
var initialMarkup = """
120-
namespace N
112+
[Fact]
113+
public async Task TestMovePropertyAndReorderWithPositionalParameterRecordInheritance_CodeFix()
114+
{
115+
var initialMarkup = """
116+
namespace N
117+
{
118+
public record B(int Foo, int Bar);
119+
120+
public class C : [|B|]
121121
{
122-
public record B(int Foo, int Bar);
122+
public int P { get; init; }
123123
124-
public class C : [|B|]
124+
public {|CS1729:C|}(int p, int bar, int foo)
125125
{
126-
public int P { get; init; }
127-
128-
public {|CS1729:C|}(int p, int bar, int foo)
129-
{
130-
P = p;
131-
Bar = bar;
132-
Foo = foo;
133-
}
126+
P = p;
127+
Bar = bar;
128+
Foo = foo;
134129
}
135130
}
136-
""";
137-
var changedMarkup = """
138-
namespace N
139-
{
140-
public record B(int Foo, int Bar);
131+
}
132+
""";
133+
var changedMarkup = """
134+
namespace N
135+
{
136+
public record B(int Foo, int Bar);
141137
142-
public record C(int P, int Bar, int Foo) : B(Foo, Bar);
143-
}
144-
""";
145-
await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false);
146-
}
138+
public record C(int P, int Bar, int Foo) : B(Foo, Bar);
139+
}
140+
""";
141+
await TestCodeFixAsync(initialMarkup, changedMarkup).ConfigureAwait(false);
142+
}
147143

148-
private class CodeFixTest : CSharpCodeFixVerifier<TestAnalyzer, CSharpConvertToRecordCodeFixProvider>.Test
149-
{
150-
}
144+
private class CodeFixTest : CSharpCodeFixVerifier<TestAnalyzer, CSharpConvertToRecordCodeFixProvider>.Test
145+
{
146+
}
151147

152-
private static async Task TestCodeFixAsync(string initialMarkup, string fixedMarkup)
148+
private static async Task TestCodeFixAsync(string initialMarkup, string fixedMarkup)
149+
{
150+
var test = new CodeFixTest()
153151
{
154-
var test = new CodeFixTest()
155-
{
156-
TestCode = initialMarkup,
157-
FixedCode = fixedMarkup,
158-
LanguageVersion = LanguageVersion.CSharp10,
159-
ReferenceAssemblies = Testing.ReferenceAssemblies.Net.Net60,
160-
};
161-
await test.RunAsync().ConfigureAwait(false);
162-
}
152+
TestCode = initialMarkup,
153+
FixedCode = fixedMarkup,
154+
LanguageVersion = LanguageVersion.CSharp10,
155+
ReferenceAssemblies = Testing.ReferenceAssemblies.Net.Net60,
156+
};
157+
await test.RunAsync().ConfigureAwait(false);
158+
}
163159

164-
private class TestAnalyzer : DiagnosticAnalyzer
165-
{
166-
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
160+
private class TestAnalyzer : DiagnosticAnalyzer
161+
{
162+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
167163
#pragma warning disable RS0030 // Do not used banned APIs
168-
=> ImmutableArray.Create(new DiagnosticDescriptor(
169-
"CS8865",
170-
"Only records may inherit from records.",
171-
"Only records may inherit from records.",
172-
"Compiler error",
173-
DiagnosticSeverity.Error,
174-
isEnabledByDefault: true));
164+
=> ImmutableArray.Create(new DiagnosticDescriptor(
165+
"CS8865",
166+
"Only records may inherit from records.",
167+
"Only records may inherit from records.",
168+
"Compiler error",
169+
DiagnosticSeverity.Error,
170+
isEnabledByDefault: true));
175171
#pragma warning restore RS0030 // Do not used banned APIs
176172

177-
public override void Initialize(AnalysisContext context)
178-
{
179-
}
173+
public override void Initialize(AnalysisContext context)
174+
{
180175
}
181176
}
182177
}

0 commit comments

Comments
 (0)