File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed
CodeFixes/MakeMemberRequired Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -58,6 +58,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
5858
5959 var semanticModel = await document . GetRequiredSemanticModelAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
6060
61+ if ( semanticModel . Compilation . GetBestTypeByMetadataName ( "System.Runtime.CompilerServices.RequiredMemberAttribute" ) is null )
62+ {
63+ // The attribute necessary to support required members is not present
64+ return ;
65+ }
66+
6167 var fieldOrPropertySymbol = semanticModel . GetDeclaredSymbol ( node , cancellationToken ) ;
6268 if ( fieldOrPropertySymbol is IPropertySymbol propertySymbol )
6369 {
Original file line number Diff line number Diff line change 88using Microsoft . CodeAnalysis . Editor . UnitTests . CodeActions ;
99using Microsoft . CodeAnalysis . Test . Utilities ;
1010using Microsoft . CodeAnalysis . Testing ;
11+ using Roslyn . Test . Utilities ;
1112using Xunit ;
1213
1314namespace Microsoft . CodeAnalysis . CSharp . Analyzers . UnitTests . MakeMemberRequired
@@ -119,6 +120,28 @@ public static IEnumerable<object[]> AccessorAccessibilityModifierCombinationsWhe
119120 yield return new [ ] { "internal" , "private protected" } ;
120121 }
121122
123+ [ Fact ]
124+ [ WorkItem ( "https://github.com/dotnet/roslyn/issues/68478" ) ]
125+ public async Task SimpleSetPropertyMissingRequiredAttribute ( )
126+ {
127+ var code =
128+ """
129+ #nullable enable
130+ class MyClass
131+ {
132+ public string {|CS8618:MyProperty|} { get; set; }
133+ }
134+ """ ;
135+
136+ await new VerifyCS . Test
137+ {
138+ TestCode = code ,
139+ FixedCode = code ,
140+ LanguageVersion = LanguageVersion . CSharp11 ,
141+ ReferenceAssemblies = ReferenceAssemblies . Net . Net60 ,
142+ } . RunAsync ( ) ;
143+ }
144+
122145 [ Fact ]
123146 public async Task SimpleSetProperty ( )
124147 {
You can’t perform that action at this time.
0 commit comments