Skip to content

Commit

Permalink
Add code fix for CS8632
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Apr 4, 2021
1 parent 432a8fe commit 2c1d9ca
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,6 @@ internal static class CompilerDiagnosticIdentifiers
public const string LocalFunctionMustAlwaysHaveBody = "CS8112";
public const string CannotChangeTupleElementNameWhenOverridingInheritedMember = "CS8139";
public const string InstanceFieldsOfReadOnlyStructsMustBeReadOnly = "CS8340";
public const string AnnotationForNullableReferenceTypesShouldOnlyBeUsedWithinNullableAnnotationsContext = "CS8632";
}
}
7 changes: 7 additions & 0 deletions src/CodeFixes/CSharp/CodeFixDescriptors.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -895,5 +895,12 @@ public static partial class CodeFixDescriptors
isEnabledByDefault: true,
"CS7036");

/// <summary>RCF0118 (fixes CS8632)</summary>
public static readonly CodeFixDescriptor RemoveAnnotationForNullableReferenceTypes = new CodeFixDescriptor(
id: CodeFixIdentifiers.RemoveAnnotationForNullableReferenceTypes,
title: "Remove annotation for nullable reference types",
isEnabledByDefault: true,
"CS8632");

}
}
1 change: 1 addition & 0 deletions src/CodeFixes/CSharp/CodeFixIdentifiers.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,6 @@ public static partial class CodeFixIdentifiers
public const string ReplaceInvocationWithMemberAccessOrViceVersa = CodeFixIdentifier.CodeFixIdPrefix + "0115";
public const string AddParameterToExplicitlyImplementedInterfaceMember = CodeFixIdentifier.CodeFixIdPrefix + "0116";
public const string MoveInitializerExpressionsToConstructor = CodeFixIdentifier.CodeFixIdPrefix + "0117";
public const string RemoveAnnotationForNullableReferenceTypes = CodeFixIdentifier.CodeFixIdPrefix + "0118";
}
}
24 changes: 23 additions & 1 deletion src/CodeFixes/CSharp/CodeFixes/TokenCodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public sealed override ImmutableArray<string> FixableDiagnosticIds
CompilerDiagnosticIdentifiers.TypeExpected,
CompilerDiagnosticIdentifiers.SemicolonAfterMethodOrAccessorBlockIsNotValid,
CompilerDiagnosticIdentifiers.CannotConvertType,
CompilerDiagnosticIdentifiers.OptionalParametersMustAppearAfterAllRequiredParameters);
CompilerDiagnosticIdentifiers.OptionalParametersMustAppearAfterAllRequiredParameters,
CompilerDiagnosticIdentifiers.AnnotationForNullableReferenceTypesShouldOnlyBeUsedWithinNullableAnnotationsContext);
}
}

Expand Down Expand Up @@ -430,6 +431,27 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
},
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
case CompilerDiagnosticIdentifiers.AnnotationForNullableReferenceTypesShouldOnlyBeUsedWithinNullableAnnotationsContext:
{
if (!Settings.IsEnabled(diagnostic.Id, CodeFixIdentifiers.RemoveAnnotationForNullableReferenceTypes))
break;

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

CodeAction codeAction = CodeAction.Create(
"Remove 'nullable' annotation",
ct =>
{
var textChange = new TextChange(token.Span, "");
return context.Document.WithTextChangeAsync(textChange, ct);
},
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
Expand Down
12 changes: 12 additions & 0 deletions src/CodeFixes/CSharp/CompilerDiagnosticDescriptors.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2024,5 +2024,17 @@ public static partial class CompilerDiagnosticDescriptors
helpLinkUri: "",
customTags: WellKnownDiagnosticTags.Compiler);

/// <summary>CS8632</summary>
public static readonly DiagnosticDescriptor AnnotationForNullableReferenceTypesShouldOnlyBeUsedWithinNullableAnnotationsContext = new DiagnosticDescriptor(
id: CompilerDiagnosticIdentifiers.AnnotationForNullableReferenceTypesShouldOnlyBeUsedWithinNullableAnnotationsContext,
title: "The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.",
messageFormat: "The annotation for nullable reference types should only be used in code within a '#nullable' annotations context",
category: "Compiler",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: null,
helpLinkUri: "",
customTags: WellKnownDiagnosticTags.Compiler);

}
}
5 changes: 5 additions & 0 deletions src/CodeFixes/CodeFixes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -665,4 +665,9 @@
<Id>CS7036</Id>
</FixableDiagnosticIds>
</CodeFix>
<CodeFix Id="RCF0118" Identifier="RemoveAnnotationForNullableReferenceTypes" Title="Remove annotation for nullable reference types">
<FixableDiagnosticIds>
<Id>CS8632</Id>
</FixableDiagnosticIds>
</CodeFix>
</CodeFixes>
7 changes: 7 additions & 0 deletions src/CodeFixes/Diagnostics.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1176,4 +1176,11 @@
Title="Instance fields of read-only structs must be read-only."
Message="Instance fields of readonly structs must be readonly"
HelpUrl="" />
<Diagnostic
Id="CS8632"
Identifier="AnnotationForNullableReferenceTypesShouldOnlyBeUsedWithinNullableAnnotationsContext"
Severity="Warning"
Title="The annotation for nullable reference types should only be used in code within a '#nullable' annotations context."
Message="The annotation for nullable reference types should only be used in code within a '#nullable' annotations context"
HelpUrl="" />
</Diagnostics>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Josef Pihrt. All rights reserved. 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 CS8632AnnotationForNullableReferenceTypesShouldOnlyBeUsedWithinNullableAnnotationsContextTests : AbstractCSharpCompilerDiagnosticFixVerifier<TokenCodeFixProvider>
{
public override string DiagnosticId { get; } = CompilerDiagnosticIdentifiers.AnnotationForNullableReferenceTypesShouldOnlyBeUsedWithinNullableAnnotationsContext;

[Fact, Trait(Traits.CodeFix, CompilerDiagnosticIdentifiers.AnnotationForNullableReferenceTypesShouldOnlyBeUsedWithinNullableAnnotationsContext)]
public async Task Test_Method()
{
await VerifyFixAsync(@"
class C
{
public string? M()
{
return null;
}
}
", @"
class C
{
public string M()
{
return null;
}
}
", equivalenceKey: EquivalenceKey.Create(DiagnosticId));
}
}
}
2 changes: 1 addition & 1 deletion src/VisualStudio.Common/CodeFixesOptionsPage.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ protected override string MaxId
get;
}

= CodeFixIdentifiers.MoveInitializerExpressionsToConstructor;
= CodeFixIdentifiers.RemoveAnnotationForNullableReferenceTypes;
}
}

0 comments on commit 2c1d9ca

Please sign in to comment.