Skip to content

Commit

Permalink
Add code fix for CS0037 (dotnet#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored and JochemH committed Jul 14, 2022
1 parent d9794cf commit 49a91f2
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 7 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Convert more syntax to implicit object creation (RCS1250) ([#910](https://github.com/josefpihrt/roslynator/pull/910)).
- Add code fix for CS0037 ([#929](https://github.com/josefpihrt/roslynator/pull/929)).

### Changed

Expand Down
2 changes: 2 additions & 0 deletions docs/cs/CS0037.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

## Code Fixes

* Remove property or field initializer
* Replace 'null' with default value


*\(Generated with [DotMarkdown](http://github.com/JosefPihrt/DotMarkdown)\)*
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
Expand All @@ -24,6 +23,7 @@ public override ImmutableArray<string> FixableDiagnosticIds
get
{
return ImmutableArray.Create(
CompilerDiagnosticIdentifiers.CS0037_CannotConvertNullToTypeBecauseItIsNonNullableValueType,
CompilerDiagnosticIdentifiers.CS0573_CannotHaveInstancePropertyOrFieldInitializersInStruct,
CompilerDiagnosticIdentifiers.CS8050_OnlyAutoImplementedPropertiesCanHaveInitializers);
}
Expand All @@ -41,12 +41,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
if (!TryFindToken(root, context.Span.Start, out SyntaxToken token))
return;

SyntaxDebug.Assert(token.IsKind(SyntaxKind.IdentifierToken), token);
SyntaxDebug.Assert(token.IsKind(SyntaxKind.IdentifierToken, SyntaxKind.NullKeyword), token);

if (!token.IsKind(SyntaxKind.IdentifierToken))
return;

switch (token.Parent)
switch (GetNode(token))
{
case PropertyDeclarationSyntax propertyDeclaration:
{
Expand Down Expand Up @@ -83,12 +80,36 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
return context.Document.ReplaceNodeAsync(variableDeclarator, newNode, ct);
},
GetEquivalenceKey(CompilerDiagnosticIdentifiers.CS0573_CannotHaveInstancePropertyOrFieldInitializersInStruct, CodeFixIdentifiers.RemovePropertyOrFieldInitializer));
GetEquivalenceKey(diagnostic, CodeFixIdentifiers.RemovePropertyOrFieldInitializer));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
}

static SyntaxNode GetNode(SyntaxToken token)
{
if (token.IsKind(SyntaxKind.IdentifierToken))
return token.Parent;

if (token.IsKind(SyntaxKind.NullKeyword))
{
SyntaxNode node = token.Parent;

if (node.IsKind(SyntaxKind.NullLiteralExpression))
{
node = node.Parent;

if (node.IsKind(SyntaxKind.SuppressNullableWarningExpression))
node = node.Parent;

if (node.IsKind(SyntaxKind.EqualsValueClause))
return node.Parent;
}
}

return null;
}
}
}
}
1 change: 1 addition & 0 deletions src/CodeFixes/CodeFixes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
</CodeFix>
<CodeFix Id="RCF0024" Identifier="RemovePropertyOrFieldInitializer" Title="Remove property or field initializer">
<FixableDiagnosticIds>
<Id>CS0037</Id>
<Id>CS0573</Id>
<Id>CS8050</Id>
</FixableDiagnosticIds>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) Josef Pihrt and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Roslynator.Testing.CSharp;
using Xunit;

namespace Roslynator.CSharp.CodeFixes.Tests
{
public class CS0037CannotConvertNullToTypeBecauseItIsNonNullableValueTypeTests : AbstractCSharpCompilerDiagnosticFixVerifier<RemovePropertyOrFieldInitializerCodeFixProvider>
{
public override string DiagnosticId { get; } = CompilerDiagnosticIdentifiers.CS0037_CannotConvertNullToTypeBecauseItIsNonNullableValueType;

[Fact, Trait(Traits.CodeFix, CompilerDiagnosticIdentifiers.CS0037_CannotConvertNullToTypeBecauseItIsNonNullableValueType)]
public async Task Test_RemovePropertyInitializer()
{
await VerifyFixAsync(@"
using System;
class C
{
public StringSplitOptions Options { get; set } = null;
}
", @"
using System;
class C
{
public StringSplitOptions Options { get; set }
}
", equivalenceKey: EquivalenceKey.Create(DiagnosticId, CodeFixIdentifiers.RemovePropertyOrFieldInitializer));
}

[Fact, Trait(Traits.CodeFix, CompilerDiagnosticIdentifiers.CS0037_CannotConvertNullToTypeBecauseItIsNonNullableValueType)]
public async Task Test_RemovePropertyInitializer_NullForgivingOperator()
{
await VerifyFixAsync(@"
using System;
class C
{
public StringSplitOptions Options { get; set } = null!;
}
", @"
using System;
class C
{
public StringSplitOptions Options { get; set }
}
", equivalenceKey: EquivalenceKey.Create(DiagnosticId, CodeFixIdentifiers.RemovePropertyOrFieldInitializer));
}

[Fact, Trait(Traits.CodeFix, CompilerDiagnosticIdentifiers.CS0037_CannotConvertNullToTypeBecauseItIsNonNullableValueType)]
public async Task Test_RemoveFieldInitializer()
{
await VerifyFixAsync(@"
using System;
class C
{
public StringSplitOptions Options = null!;
}
", @"
using System;
class C
{
public StringSplitOptions Options;
}
", equivalenceKey: EquivalenceKey.Create(DiagnosticId, CodeFixIdentifiers.RemovePropertyOrFieldInitializer));
}
}
}

0 comments on commit 49a91f2

Please sign in to comment.