未验证 提交 b1a94562 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #30430 from Neme12/useAnotherLiteral

Code fix for CS8313, CS8363 (invalid default literal as case constant / pattern) to use another literal
......@@ -1261,6 +1261,15 @@ internal class CSharpFeaturesResources {
}
}
/// <summary>
/// Looks up a localized string similar to Use &apos;{0}&apos;.
/// </summary>
internal static string Use_0 {
get {
return ResourceManager.GetString("Use_0", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Use explicit type.
/// </summary>
......
......@@ -572,6 +572,9 @@
<data name="Remove_unused_variables" xml:space="preserve">
<value>Remove unused variables</value>
</data>
<data name="Use_0" xml:space="preserve">
<value>Use '{0}'</value>
</data>
<data name="Add_missing_usings" xml:space="preserve">
<value>Add missing usings</value>
<comment>{Locked="using"} "using" is a C# keyword and should not be localized.</comment>
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.CSharp.ReplaceDefaultLiteral
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.ReplaceDefaultLiteral), Shared]
internal sealed class CSharpReplaceDefaultLiteralCodeFixProvider : CodeFixProvider
{
private const string CS8313 = nameof(CS8313); // A default literal 'default' is not valid as a case constant. Use another literal (e.g. '0' or 'null') as appropriate. If you intended to write the default label, use 'default:' without 'case'.
private const string CS8363 = nameof(CS8363); // A default literal 'default' is not valid as a pattern. Use another literal (e.g. '0' or 'null') as appropriate. To match everything, use a discard pattern 'var _'.
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
ImmutableArray.Create(CS8313, CS8363);
public override FixAllProvider GetFixAllProvider()
{
// This code fix addresses very specific compiler errors. It's unlikely there will be more than 1 of them at a time.
return null;
}
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var token = syntaxRoot.FindToken(context.Span.Start);
if (token.Span == context.Span &&
token.IsKind(SyntaxKind.DefaultKeyword) &&
token.Parent.IsKind(SyntaxKind.DefaultLiteralExpression))
{
var defaultLiteral = (LiteralExpressionSyntax)token.Parent;
var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
var (newExpression, displayText) = GetReplacementExpressionAndText(
context.Document, semanticModel, defaultLiteral, context.CancellationToken);
if (newExpression != null)
{
context.RegisterCodeFix(
new MyCodeAction(
c => ReplaceAsync(context.Document, context.Span, newExpression, c),
displayText),
context.Diagnostics);
}
}
}
private static async Task<Document> ReplaceAsync(
Document document, TextSpan span, SyntaxNode newExpression, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var defaultToken = syntaxRoot.FindToken(span.Start);
var defaultLiteral = (LiteralExpressionSyntax)defaultToken.Parent;
var newRoot = syntaxRoot.ReplaceNode(defaultLiteral, newExpression.WithTriviaFrom(defaultLiteral));
return document.WithSyntaxRoot(newRoot);
}
private static (SyntaxNode newExpression, string displayText) GetReplacementExpressionAndText(
Document document,
SemanticModel semanticModel,
LiteralExpressionSyntax defaultLiteral,
CancellationToken cancellationToken)
{
var generator = SyntaxGenerator.GetGenerator(document);
var type = semanticModel.GetTypeInfo(defaultLiteral, cancellationToken).ConvertedType;
if (type != null && type.TypeKind != TypeKind.Error)
{
if (IsFlagsEnum(type, semanticModel.Compilation) &&
type.GetMembers("None").FirstOrDefault() is IFieldSymbol field && IsZero(field.ConstantValue))
{
return GenerateMemberAccess("None");
}
else if (type.Equals(semanticModel.Compilation.GetTypeByMetadataName(typeof(CancellationToken).FullName)))
{
return GenerateMemberAccess(nameof(CancellationToken.None));
}
else if (type.SpecialType == SpecialType.System_IntPtr || type.SpecialType == SpecialType.System_UIntPtr)
{
return GenerateMemberAccess(nameof(IntPtr.Zero));
}
else if (semanticModel.GetConstantValue(defaultLiteral, cancellationToken) is var constant && constant.HasValue)
{
var newLiteral = generator.LiteralExpression(constant.Value);
return (newLiteral, newLiteral.ToString());
}
else if (!type.ContainsAnonymousType())
{
var defaultExpression = generator.DefaultExpression(type);
return (defaultExpression, $"default({type.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)})");
}
}
return default;
(SyntaxNode newExpression, string displayText) GenerateMemberAccess(string memberName)
{
var memberAccess = generator.MemberAccessExpression(generator.TypeExpression(type), memberName);
return (memberAccess, $"{type.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)}.{memberName}");
}
}
private static bool IsFlagsEnum(ITypeSymbol type, Compilation compilation)
{
var flagsAttribute = compilation.GetTypeByMetadataName(typeof(FlagsAttribute).FullName);
return type.TypeKind == TypeKind.Enum &&
type.GetAttributes().Any(attribute => attribute.AttributeClass.Equals(flagsAttribute));
}
private static bool IsZero(object o)
{
switch (o)
{
case default(int):
case default(uint):
case default(byte):
case default(sbyte):
case default(short):
case default(ushort):
case default(long):
case default(ulong):
return true;
default:
return false;
}
}
private sealed class MyCodeAction : CodeAction.DocumentChangeAction
{
public MyCodeAction(Func<CancellationToken, Task<Document>> createChangedDocument, string literal)
: base(string.Format(CSharpFeaturesResources.Use_0, literal), createChangedDocument, CSharpFeaturesResources.Use_0)
{
}
}
}
}
......@@ -117,6 +117,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -117,6 +117,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -117,6 +117,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -117,6 +117,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -117,6 +117,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -117,6 +117,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -117,6 +117,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -117,6 +117,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -117,6 +117,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -117,6 +117,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -117,6 +117,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -117,6 +117,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -117,6 +117,11 @@
<target state="new">Sort accessibility modifiers</target>
<note />
</trans-unit>
<trans-unit id="Use_0">
<source>Use '{0}'</source>
<target state="new">Use '{0}'</target>
<note />
</trans-unit>
<trans-unit id="if_statement_can_be_simplified">
<source>'if' statement can be simplified</source>
<target state="new">'if' statement can be simplified</target>
......
......@@ -41,6 +41,7 @@ internal static class PredefinedCodeFixProviderNames
public const string MoveToTopOfFile = nameof(MoveToTopOfFile);
public const string PopulateSwitch = nameof(PopulateSwitch);
public const string QualifyMemberAccess = nameof(QualifyMemberAccess);
public const string ReplaceDefaultLiteral = nameof(ReplaceDefaultLiteral);
public const string RemoveUnnecessaryCast = nameof(RemoveUnnecessaryCast);
public const string DeclareAsNullable = nameof(DeclareAsNullable);
public const string RemoveUnnecessaryImports = nameof(RemoveUnnecessaryImports);
......
......@@ -107,6 +107,7 @@ public static class Features
public const string CodeActionsOrderModifiers = "CodeActions.OrderModifiers";
public const string CodeActionsPopulateSwitch = "CodeActions.PopulateSwitch";
public const string CodeActionsQualifyMemberAccess = "CodeActions.QualifyMemberAccess";
public const string CodeActionsReplaceDefaultLiteral = "CodeActions.ReplaceDefaultLiteral";
public const string CodeActionsReplaceDocCommentTextWithTag = "CodeActions.ReplaceDocCommentTextWithTag";
public const string CodeActionsReplaceMethodWithProperty = "CodeActions.ReplaceMethodWithProperty";
public const string CodeActionsReplacePropertyWithMethods = "CodeActions.ReplacePropertyWithMethods";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册