From c25e5b5248adccb6d47d19fbbc6dd056a5eb28b7 Mon Sep 17 00:00:00 2001 From: naminodarie Date: Thu, 11 Feb 2021 22:51:25 +0900 Subject: [PATCH 1/2] Update CreateOperatorCodeFixProvider --- CHANGELOG.md | 1 + .../CreateOperatorCodeFixProvider.cs | 2 +- .../Helpers/SimplifyTypeSyntaxRewriter.cs | 12 +-- .../CreateOperatorCodeFixProviderTest.cs | 98 ++++++++++++++++++- 4 files changed, 101 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index be3544c4..2c5b6387 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - 2021-02-11 ### Changed +- Update CreateOperatorCodeFixProvider - Use Span.Fill - CeilPow2 AggressiveInlining diff --git a/Source/AtCoderAnalyzer/CreateOperatorCodeFixProvider.cs b/Source/AtCoderAnalyzer/CreateOperatorCodeFixProvider.cs index 7d7892da..50d52422 100644 --- a/Source/AtCoderAnalyzer/CreateOperatorCodeFixProvider.cs +++ b/Source/AtCoderAnalyzer/CreateOperatorCodeFixProvider.cs @@ -70,7 +70,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) var writtenTypeSyntax = writtenTypeSyntaxes[i]; var originalType = originalTypes[i]; var constraintTypes = originalType.ConstraintTypes; - var writtenType = writtenTypes[i] as INamedTypeSymbol; + var writtenType = writtenTypes[i] as ITypeSymbol; if (!constraintTypes .OfType() diff --git a/Source/AtCoderAnalyzer/Helpers/SimplifyTypeSyntaxRewriter.cs b/Source/AtCoderAnalyzer/Helpers/SimplifyTypeSyntaxRewriter.cs index 165d08a5..9fc8aa0e 100644 --- a/Source/AtCoderAnalyzer/Helpers/SimplifyTypeSyntaxRewriter.cs +++ b/Source/AtCoderAnalyzer/Helpers/SimplifyTypeSyntaxRewriter.cs @@ -10,15 +10,11 @@ internal class SimplifyTypeSyntaxRewriter : CSharpSyntaxRewriter private ImmutableHashSet Usings { get; } public SimplifyTypeSyntaxRewriter(ImmutableHashSet usings) => Usings = usings; - public override SyntaxNode Visit(SyntaxNode node) + public override SyntaxNode VisitQualifiedName(QualifiedNameSyntax node) { - if (node is QualifiedNameSyntax qualified) - { - if (Usings.Contains(qualified.Left.ToString())) - return qualified.Right; - return qualified; - } - return base.Visit(node); + if (Usings.Contains(node.Left.ToString())) + return base.Visit(node.Right); + return base.VisitQualifiedName(node); } } } diff --git a/Test/AtCoderAnalyzer.Test/CreateOperatorCodeFixProviderTest.cs b/Test/AtCoderAnalyzer.Test/CreateOperatorCodeFixProviderTest.cs index 32203c8d..67953df0 100644 --- a/Test/AtCoderAnalyzer.Test/CreateOperatorCodeFixProviderTest.cs +++ b/Test/AtCoderAnalyzer.Test/CreateOperatorCodeFixProviderTest.cs @@ -654,7 +654,7 @@ struct Op : System.Collections.Generic.IComparer public int Compare(short x, short y) => default; }"; await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic().WithSpan(6, 5, 6, 23).WithArguments("Op"), + VerifyCS.Diagnostic("AC0008").WithSpan(6, 5, 6, 23).WithArguments("Op"), fixedSource); } @@ -716,7 +716,7 @@ public void Init(ref (int, long) val, out bool success, params int[] nums) public (int, long) Prop2 { set; get; } }"; await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic().WithSpan(12, 5, 12, 29).WithArguments("Op"), + VerifyCS.Diagnostic("AC0008").WithSpan(12, 5, 12, 29).WithArguments("Op"), fixedSource); } @@ -770,8 +770,100 @@ public void Init() public (int n, long m) Prop2 { set; get; } }"; await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic().WithSpan(14, 9, 14, 31).WithArguments("Op"), + VerifyCS.Diagnostic("AC0008").WithSpan(14, 9, 14, 31).WithArguments("Op"), fixedSource); } + + [Fact] + public async Task Array() + { + var source = @" +using AtCoder; +using System.Runtime.CompilerServices; + +[IsOperator] +public interface IAny { + T Prop { get; } +} +class Program +{ + static void M() where TOp : struct, IAny {} + static void Run() + { + M(); + } +} +"; + var fixedSource = @" +using AtCoder; +using System.Runtime.CompilerServices; + +[IsOperator] +public interface IAny { + T Prop { get; } +} +class Program +{ + static void M() where TOp : struct, IAny {} + static void Run() + { + M(); + } +} + +struct BigOp : IAny +{ + public System.Numerics.BigInteger[] Prop => default; +}"; + await VerifyCS.VerifyCodeFixAsync(source, + VerifyCS.Diagnostic("AC0008").WithSpan(14, 9, 14, 47).WithArguments("BigOp"), + fixedSource); + } + + [Fact] + public async Task Generic() + { + var source = @" +using AtCoder; +using System.Runtime.CompilerServices; + +[IsOperator] +public interface IAny { + T Prop { get; } +} +class Program +{ + static void M() where TOp : struct, IAny {} + static void Run() + { + M, ModOp>(); + } +} +"; + var fixedSource = @" +using AtCoder; +using System.Runtime.CompilerServices; + +[IsOperator] +public interface IAny { + T Prop { get; } +} +class Program +{ + static void M() where TOp : struct, IAny {} + static void Run() + { + M, ModOp>(); + } +} + +struct ModOp : IAny> +{ + public StaticModInt Prop => default; +}"; + await VerifyCS.VerifyCodeFixAsync(source, + VerifyCS.Diagnostic("AC0008").WithSpan(14, 9, 14, 46).WithArguments("ModOp"), + fixedSource); + } } } From 566fff78d00cee0e472fd4410fa402c0b53f14ba Mon Sep 17 00:00:00 2001 From: naminodarie Date: Thu, 11 Feb 2021 22:58:40 +0900 Subject: [PATCH 2/2] Fix rewrite namespace --- Source/AtCoderAnalyzer/Helpers/SimplifyTypeSyntaxRewriter.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/AtCoderAnalyzer/Helpers/SimplifyTypeSyntaxRewriter.cs b/Source/AtCoderAnalyzer/Helpers/SimplifyTypeSyntaxRewriter.cs index 9fc8aa0e..de22c3d3 100644 --- a/Source/AtCoderAnalyzer/Helpers/SimplifyTypeSyntaxRewriter.cs +++ b/Source/AtCoderAnalyzer/Helpers/SimplifyTypeSyntaxRewriter.cs @@ -12,9 +12,10 @@ internal class SimplifyTypeSyntaxRewriter : CSharpSyntaxRewriter public override SyntaxNode VisitQualifiedName(QualifiedNameSyntax node) { + var right = (SimpleNameSyntax)Visit(node.Right); if (Usings.Contains(node.Left.ToString())) - return base.Visit(node.Right); - return base.VisitQualifiedName(node); + return right; + return SyntaxFactory.QualifiedName(node.Left, right); } } }