コード例 #1
0
        public async Task Test_BoolField()
        {
            await VerifyRefactoringAsync(@"
class C
{
    public const bool KB = true;

    void M()
    {
        bool b = [|KB|];
    }
}
", @"
class C
{
    public const bool KB = true;

    void M()
    {
        bool b = true;
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #2
0
        public async Task Test_EndOfText()
        {
            await VerifyRefactoringAsync(@"
class C
{
    /// <summary>
    /// x [|null|]
    /// </summary>
    void M()
    {
    }
}
", @"
class C
{
    /// <summary>
    /// x <c>null</c>
    /// </summary>
    void M()
    {
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #3
0
        public async Task Test()
        {
            await VerifyFixAsync(@"
using System;

class C
{
    DateTime M()
    {
        return;
    }
}
", @"
using System;

class C
{
    DateTime M()
    {
        return default;
    }
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId));
        }
コード例 #4
0
        public async Task Test_IntroduceLocal()
        {
            await VerifyFixAsync(@"
using System;

class C
{
    void M()
    {
        DateTime.Now;
    }
}
", @"
using System;

class C
{
    void M()
    {
        var dateTime = DateTime.Now;
    }
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId, CodeFixIdentifiers.IntroduceLocalVariable));
        }
        public async Task Test_Method()
        {
            await VerifyFixAsync(@"
class C : B
{
    public override (string x, string yy, string z) M()
    {
        return (x: null, yy: null, z: null);
    }
}

class B
{
    public virtual (string x, string y, string z) M()
    {
        return default;
    }
}
", @"
class C : B
{
    public override (string x, string y, string z) M()
    {
        return (x: null, y: null, z: null);
    }
}

class B
{
    public virtual (string x, string y, string z) M()
    {
        return default;
    }
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId));
        }
        public async Task Test_Indexer_ExplicitlyImplemented()
        {
            await VerifyFixAsync(@"
interface IFoo
{
    object this[object p] { get; }
}

class C : IFoo
{
    object IFoo.this[object p, object p2] => null;
}
", @"
interface IFoo
{
    object this[object p, object p2] { get; }
}

class C : IFoo
{
    object IFoo.this[object p, object p2] => null;
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId, "P:IFoo.Item(System.Object)"));
        }
コード例 #7
0
        public async Task TestNoRefactoring2()
        {
            await VerifyNoRefactoringAsync(@"
class C
{
    void M()
    {
        var c = this;

        C a = c?.[||]A;
    }

    C A
    {
        get { return B; }
    }

    C B
    {
        get { return null; }
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #8
0
        public async Task Test_MakeFieldWritable()
        {
            await VerifyFixAsync(@"
class C
{
    private readonly string _f;

    void M(out string p)
    {
        M(out _f)
    }
}
", @"
class C
{
    private string _f;

    void M(out string p)
    {
        M(out _f)
    }
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId, CodeFixIdentifiers.MakeFieldWritable));
        }
コード例 #9
0
        public async Task Test_MakeContainingMethodNonStatic()
        {
            await VerifyFixAsync(@"
class A
{
    public static void M()
    {
        int x = P;
    }

    public int P => 1;
}
", @"
class A
{
    public void M()
    {
        int x = P;
    }

    public int P => 1;
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId, CodeFixIdentifiers.MakeMemberNonStatic));
        }
コード例 #10
0
        public async Task Test_Field_VerbatimLiteral()
        {
            await VerifyRefactoringAsync(@"
class C
{
    public const string K = @""x"";

    void M(string s)
    {
        s = [|K|];
    }
}
", @"
class C
{
    public const string K = @""x"";

    void M(string s)
    {
        s = @""x"";
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #11
0
        public async Task Test_Field_QualifiedWithClassName()
        {
            await VerifyRefactoringAsync(@"
class C
{
    public const string K = @""x"";

    void M(string s)
    {
        s = [|C.K|];
    }
}
", @"
class C
{
    public const string K = @""x"";

    void M(string s)
    {
        s = @""x"";
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #12
0
        public async Task Test_LongFieldAsync()
        {
            await VerifyRefactoringAsync(@"
class C
{
    public const long KL = 1;

    void M()
    {
        long l = [|KL|];
    }
}
", @"
class C
{
    public const long KL = 1;

    void M()
    {
        long l = 1;
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #13
0
        public async Task Test_IntFieldAsync()
        {
            await VerifyRefactoringAsync(@"
class C
{
    public const int KI = int.MaxValue;

    void M()
    {
        int i = [|KI|];
    }
}
", @"
class C
{
    public const int KI = int.MaxValue;

    void M()
    {
        int i = 2147483647;
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #14
0
        public async Task Test_CharFieldAsync()
        {
            await VerifyRefactoringAsync(@"
class C
{
    public const char KC = '\n';

    void M()
    {
        char c = [|KC|];
    }
}
", @"
class C
{
    public const char KC = '\n';

    void M()
    {
        char c = '\n';
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
        public async Task Test()
        {
            await VerifyRefactoringAsync(@"
class C
{
    void M()
    {
        object x = null;

        var y = (C)[||]x;
    }
}
", @"
class C
{
    void M()
    {
        object x = null;

        var y = x as C;
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #16
0
        public async Task Test_NullableType()
        {
            await VerifyRefactoringAsync(@"
using System;

class C
{
    void M(int? [||]p)
    {
    }
}
", @"
using System;

class C
{
    void M(int? p)
    {
        if (p == null)
            throw new ArgumentNullException(nameof(p));
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #17
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            if (!Settings.IsCodeFixEnabled(CodeFixIdentifiers.RemoveSwitchLabel))
            {
                return;
            }

            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out CaseSwitchLabelSyntax caseSwitchLabel))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case CompilerDiagnosticIdentifiers.SwitchStatementContainsMultipleCasesWithSameLabelValue:
                {
                    if (!(caseSwitchLabel.Parent.Parent is SwitchStatementSyntax switchStatement))
                    {
                        break;
                    }

                    CodeAction codeAction = CodeAction.Create(
                        "Remove duplicate cases",
                        cancellationToken => RefactorAsync(context.Document, switchStatement, cancellationToken),
                        EquivalenceKey.Create(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
コード例 #18
0
        public async Task Test_LocalVariable()
        {
            await VerifyRefactoringAsync(@"
using System.Collections.Generic;

class C
{
    void M()
    {
        [||]IEnumerable<object> x = new List<object>();
    }
}
", @"
using System.Collections.Generic;

class C
{
    void M()
    {
        List<object> x = new List<object>();
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #19
0
        public async Task Test_MissingCommaInInitializer_Singleline()
        {
            await VerifyFixAsync(@"
class C
{
    public void M()
    {
        string s = null;

        var items = new string[] { s s s };
    }
}
", @"
class C
{
    public void M()
    {
        string s = null;

        var items = new string[] { s, s, s };
    }
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId));
        }
コード例 #20
0
        public async Task Test_If_SingleStatement()
        {
            await VerifyRefactoringAsync(@"
class C
{
    void M(bool f = false)
    {
        {
            [||]if (f)
            {
                return;
            }

            M();
        }
    }
}
", @"
class C
{
    void M(bool f = false)
    {
        {
            if (!f)
            {
                M();
            }
            else
            {
                return;
            }
        }
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
        public async Task Test_AccessorWithPublicAccessibility()
        {
            await VerifyFixAsync(@"
class B
{
    public virtual object P { get; set; }
}

class C : B
{
    public override object P { get; private set; }
}
", @"
class B
{
    public virtual object P { get; set; }
}

class C : B
{
    public override object P { get; set; }
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId));
        }
コード例 #22
0
        public async Task Test_If_LastStatementIsJumpStatement()
        {
            await VerifyRefactoringAsync(@"
class C
{
    void M(bool f = false)
    {
        [||]if (f)
        {
            M();
            return;
        }

        M2();
        return;
    }

    void M2() => M();
}
", @"
class C
{
    void M(bool f = false)
    {
        if (!f)
        {
            M2();
            return;
        }
        M();
    }

    void M2() => M();
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #23
0
        public async Task Test_AssignmentAndIfToAssignmentWithConditionalExpression()
        {
            await VerifyRefactoringAsync(@"
class C
{
    void M(bool f, string x, string y, string z)
    {
[|        z = y;
        if (f)
        {
            z = x;
        }|]
    }
}
", @"
class C
{
    void M(bool f, string x, string y, string z)
    {
        z = (f) ? x : y;
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #24
0
        public async Task Test_Dictionary_TopLevelStatement()
        {
            await VerifyRefactoringAsync(@"
using System.Collections.Generic;

var dic = new Dictionary<object, object>();

foreach ([||]var kvp in dic)
{
    var k = kvp.Key;
    var v = kvp.Value.ToString();
}
", @"
using System.Collections.Generic;

var dic = new Dictionary<object, object>();

foreach (var (key, value) in dic)
{
    var k = key;
    var v = value.ToString();
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #25
0
        public async Task Test_LocalFunction()
        {
            await VerifyFixAsync(@"
class C
{
    void M()
    {
        LF();

        return;

        LF();

        void LF()
        {
            return;
        }
    }
}
", @"
class C
{
    void M()
    {
        LF();

        return;

        void LF()
        {
            return;
        }
    }
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId));
        }
コード例 #26
0
        public async Task Test_Tuple()
        {
            await VerifyRefactoringAsync(@"
using System.Collections.Generic;

class C
{
    void M()
    {
        var items = new List<(object, string)>();

        foreach ([||]var item in items)
        {
            var k = item.Item1;
            var v = item.Item2.ToString();
        }
    }
}
", @"
using System.Collections.Generic;

class C
{
    void M()
    {
        var items = new List<(object, string)>();

        foreach (var (item1, item2) in items)
        {
            var k = item1;
            var v = item2.ToString();
        }
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
        public async Task Test_ReadOnlySpanOfT()
        {
            await VerifyFixAsync(@"
using System;

class C
{
    void M()
    {
        var bytes = stackalloc byte[10];
    }
}
", @"
using System;

class C
{
    void M()
    {
        ReadOnlySpan<byte> bytes = stackalloc byte[10];
    }
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId, CodeFixIdentifiers.UseExplicitTypeInsteadOfVar, "System_ReadOnlySpan_T"));
        }
コード例 #28
0
        public async Task Test_WithOpenCloseBraces()
        {
            await VerifyRefactoringAsync(@"
public record R([|string P, object O|])
{
}

namespace System.Runtime.CompilerServices { internal static class IsExternalInit {} }
", @"
public record R
{
    public R(string p, object o)
    {
        P = p;
        O = o;
    }

    public string P { get; init; }
    public object O { get; init; }
}

namespace System.Runtime.CompilerServices { internal static class IsExternalInit {} }
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #29
0
        public async Task Test_LocalFunction_ExpressionBody()
        {
            await VerifyRefactoringAsync(@"
using System.Threading.Tasks;

class C
{
    void M()
    {
        [||]async Task<object> GetAsync() => await GetAsync();
    }
}
", @"
using System.Threading.Tasks;

class C
{
    void M()
    {
        Task<object> GetAsync() => GetAsync();
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
コード例 #30
0
        public async Task Test_Field_Null()
        {
            await VerifyRefactoringAsync(@"
class C
{
    public const string K = null;

    void M(string s)
    {
        s = [|K|];
    }
}
", @"
class C
{
    public const string K = null;

    void M(string s)
    {
        s = null;
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }