Exemplo n.º 1
0
        public async Task Test_Constructor()
        {
            await VerifyRefactoringAsync(@"
class C
{
    public C()
    {
    [||]}
}
", @"
class C
{
    public C()
    {
    }

    public C()
    {
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId), options : Options.AddAllowedCompilerDiagnosticId("CS0111"));
        }
        public async Task TestNoRefactoring_SelectedStatements_TypesAreNotEqual()
        {
            await VerifyNoRefactoringAsync(@"
using System.Collections.Generic;

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

        [|int i = 0;
        long j = 0;
        while (i < items.Count)
        {
            items[i] = null;
            i++;
        }|]
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
Exemplo n.º 3
0
        public async Task Test_VoidMethodWithStatement()
        {
            await VerifyFixAsync(@"
class C
{
    void M(object p1, out object p2, out object p3)
    {
        p1 = null;
    }
}
", @"
class C
{
    void M(object p1, out object p2, out object p3)
    {
        p1 = null;
        p2 = null;
        p3 = null;
    }
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId));
        }
        public async Task TestNoRefactoring()
        {
            await VerifyNoRefactoringAsync(@"
using System;

class C
{
    void M()
    {
        StringSplitOptions x = StringSplitOptions.None;

        [||]switch (x)
        {
            case StringSplitOptions.None:
            case StringSplitOptions.RemoveEmptyEntries:
            default:
                break;
        }
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
Exemplo n.º 5
0
        public async Task Test_VerbatimAndInterpolated()
        {
            await VerifyRefactoringAsync(@"
class C
{
    void M()
    {
        string s = null;
        s = [|@"" """" {} "" + $"" \"" {s} ""|];
    }
}
", @"
class C
{
    void M()
    {
        string s = null;
        s = $"" \"" {{}}  \"" {s} "";
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
        public async Task Test()
        {
            await VerifyFixAsync(@"
class C
{
    void M()
    {
        const string s = null;
        s = null;
    }
}
", @"
class C
{
    void M()
    {
        string s = null;
        s = null;
    }
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId));
        }
Exemplo n.º 7
0
        public async Task Test_ReplaceComparisonWithAssignment()
        {
            await VerifyFixAsync(@"
class C
{
    void M(string s)
    {
        s == null;
        s == """";
    }
}
", @"
class C
{
    void M(string s)
    {
        s = null;
        s = """";
    }
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId, CodeFixIdentifiers.ReplaceComparisonWithAssignment));
        }
Exemplo n.º 8
0
        public async Task Test_BoolMethodWithReturnStatement()
        {
            await VerifyFixAsync(@"
class C
{
    bool M(object p1, out object p2, out object p3)
    {
        return false;
    }
}
", @"
class C
{
    bool M(object p1, out object p2, out object p3)
    {
        p2 = null;
        p3 = null;
        return false;
    }
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId));
        }
        public async Task Test_OverrideMethod()
        {
            await VerifyRefactoringAsync(@"
class B
{
    public virtual string M() => null;
}
class C : B
{
    [||]public override string M() => null;
}
", @"
class B
{
    internal virtual string M() => null;
}
class C : B
{
    internal override string M() => null;
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId, nameof(Accessibility.Internal)));
        }
Exemplo n.º 10
0
        public async Task Test()
        {
            await VerifyRefactoringAsync(@"
#pragma warning disable RCS1018 // [||]Add accessibility modifiers.

class C
{
    void M()
    {
    }
}
", @"
#pragma warning disable RCS1018

class C
{
    void M()
    {
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
        public async Task Test_SelectedStatements()
        {
            await VerifyRefactoringAsync(@"
using System.Collections.Generic;

class C
{
    void M()
    {
        var items = new List<string>();
        int i = 0;

        [|M();
        M();
        while (i < items.Count)
        {
            items[i] = null;
            i++;
        }|]
    }
}
", @"
using System.Collections.Generic;

class C
{
    void M()
    {
        var items = new List<string>();
        int i = 0;

        for (M(), M(); i < items.Count; i++)
        {
            items[i] = null;
        }
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
Exemplo n.º 12
0
        public async Task Test_SimpleIf_EmbeddedStatement()
        {
            await VerifyRefactoringAsync(@"
class C
{
    bool M(bool a, bool b, bool c, bool d)
    {
        if (a)
            [||]if (b || c && d)
            {
                return true;
            }

        return false;
    }
}
", @"
class C
{
    bool M(bool a, bool b, bool c, bool d)
    {
        if (a)
        {
            if (b)
            {
                return true;
            }

            if (c && d)
            {
                return true;
            }
        }

        return false;
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
Exemplo n.º 13
0
        public static void ComputeCodeFix(
            CodeFixContext context,
            Diagnostic diagnostic,
            ExpressionSyntax expression,
            SemanticModel semanticModel)
        {
            TypeInfo typeInfo = semanticModel.GetTypeInfo(expression, context.CancellationToken);

            ITypeSymbol type = typeInfo.Type;

            if (type is not INamedTypeSymbol namedTypeSymbol)
            {
                return;
            }

            ITypeSymbol convertedType = typeInfo.ConvertedType;

            if (SymbolEqualityComparer.Default.Equals(type, convertedType))
            {
                return;
            }

            if (namedTypeSymbol.ConstructedFrom.SpecialType != SpecialType.System_Collections_Generic_IEnumerable_T)
            {
                return;
            }

            if (!SymbolEqualityComparer.Default.Equals(namedTypeSymbol.TypeArguments[0], convertedType))
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Replace yield return with foreach",
                ct => RefactorAsync(context.Document, expression, semanticModel, ct),
                EquivalenceKey.Create(diagnostic, CodeFixIdentifiers.ReplaceYieldReturnWithForEach));

            context.RegisterCodeFix(codeAction, diagnostic);
        }
Exemplo n.º 14
0
        public async Task Test_ObjectInitializer_Span2()
        {
            await VerifyRefactoringAsync(@"
class C
{
    public string P0 { get; set; }
    public string P1 { get; set; }
}

class C2 : C
{
    void M()
    {
        var x = new C2()
        {[||]
        };
    }
}
", @"
class C
{
    public string P0 { get; set; }
    public string P1 { get; set; }
}

class C2 : C
{
    void M()
    {
        var x = new C2()
        {
            P0 = ,
            P1 =
        };
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
        public async Task Test_TupleWithNamedFields()
        {
            await VerifyRefactoringAsync(@"
using System.Collections.Generic;

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

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

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

        foreach (var (p12, p2) in items)
        {
            var k = p12;
            var v = p2.ToString();
        }
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
Exemplo n.º 16
0
        public async Task Test_MultipleParametersSelected_TwoNullChecksAdded()
        {
            await VerifyRefactoringAsync(@"
using System;

class C
{
    void M(object p, [|object p2, object p3, object p4, int? pi, int i, object p5 = null, object p6 = default, object p7 = default(object)|])
    {
        if (p2 == null)
            throw new ArgumentNullException(nameof(p2));

        if (p3 is null)
            throw new ArgumentNullException(nameof(p3));
    }
}
", @"
using System;

class C
{
    void M(object p, object p2, object p3, object p4, int? pi, int i, object p5 = null, object p6 = default, object p7 = default(object))
    {
        if (p2 == null)
            throw new ArgumentNullException(nameof(p2));

        if (p3 is null)
            throw new ArgumentNullException(nameof(p3));

        if (p4 == null)
            throw new ArgumentNullException(nameof(p4));

        if (pi == null)
            throw new ArgumentNullException(nameof(pi));
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
        public async Task Test_IfStatementAndStatements()
        {
            await VerifyRefactoringAsync(@"
class C
{
    int M()
    {
        bool f1 = false;

[|        if (f1)
            return 1;

        object x = null;

        return 0;|]
    }
}
", @"
class C
{
    int M()
    {
        bool f1 = false;

        if (f1)
        {
            return 1;
        }
        else
        {
            object x = null;

            return 0;
        }
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
Exemplo n.º 18
0
        public async Task Test_EmptyRecordInitializer()
        {
            await VerifyRefactoringAsync(@"
using System;

record C
{
    public string P1 { get; set; }
    public int? P3 { get; set; }
    public int P2 { get; set; }
    public DateTime P4 { get; set; }

    void M()
    {
        var x = this with {[||] };
    }
}

namespace System.Runtime.CompilerServices { internal static class IsExternalInit {} }
", @"
using System;

record C
{
    public string P1 { get; set; }
    public int? P3 { get; set; }
    public int P2 { get; set; }
    public DateTime P4 { get; set; }

    void M()
    {
        var x = this with { P1 = , P2 = , P3 = , P4 = };
    }
}

namespace System.Runtime.CompilerServices { internal static class IsExternalInit {} }
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
Exemplo n.º 19
0
        public async Task Test_LengthToCount_ConditionalAccess()
        {
            await VerifyFixAsync(@"
using System.Collections.Generic;
using System.Collections.ObjectModel;

class C
{
    void M()
    {
        int? ni = 0;

        var list = new List<object>();
        var collection = new Collection<object>();

        ni = list?.Length;
        ni = collection?.Length;
    }
}
", @"
using System.Collections.Generic;
using System.Collections.ObjectModel;

class C
{
    void M()
    {
        int? ni = 0;

        var list = new List<object>();
        var collection = new Collection<object>();

        ni = list?.Count;
        ni = collection?.Count;
    }
}
", EquivalenceKey.Create(DiagnosticId));
        }
        public async Task Test_TwoVariables()
        {
            await VerifyRefactoringAsync(@"
using System.Collections.Generic;

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

        int i = 0;
        int j = 0;
        [||]while (i < items.Count && j < items.Count)
        {
            items[i] = null;
            i++;
            j++;
        }
    }
}
", @"
using System.Collections.Generic;

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

        for (int i = 0, j = 0; i < items.Count && j < items.Count; i++, j++)
        {
            items[i] = null;
        }
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
        public async Task Test_StringLiteral()
        {
            await VerifyRefactoringAsync(@"
class C
{
    void M(string s)
    {
        switch (s)
        {
[|            case ""d"":
            case ""a"":
            case ""c"":|]
            case ""b"":
                break;
            default:
                break;
        }
    }
}
", @"
class C
{
    void M(string s)
    {
        switch (s)
        {
            case ""a"":
            case ""c"":
            case ""d"":
            case ""b"":
                break;
            default:
                break;
        }
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
Exemplo n.º 22
0
        public async Task Test_ObjectInitializer_Accessibility()
        {
            await VerifyRefactoringAsync(@"
class C
{
    public string P0 { get; set; }
    public string P1 { get; set; }
    public string P2 { get; protected set; }
    public string P3 { get; private set; }
    public string P4 { get; }
}

class C2 : C
{
    void M()
    {
        var x = new C2() { P0 = null, [||] };
    }
}
", @"
class C
{
    public string P0 { get; set; }
    public string P1 { get; set; }
    public string P2 { get; protected set; }
    public string P3 { get; private set; }
    public string P4 { get; }
}

class C2 : C
{
    void M()
    {
        var x = new C2() { P0 = null, P1 = , P2 = };
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
Exemplo n.º 23
0
        public async Task Test_CopyMemberAfter()
        {
            await VerifyRefactoringAsync(@"
class C
{
    void M()
    {
    }

    void N()
    {
    [||]}

    void O()
    {
    }
}
", @"
class C
{
    void M()
    {
    }

    void N()
    {
    }

    void N2()
    {
    }

    void O()
    {
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
Exemplo n.º 24
0
        public async Task Test_LengthToCount()
        {
            await VerifyFixAsync(@"
using System.Collections.Generic;
using System.Collections.ObjectModel;

class C
{
    void M()
    {
        int i = 0;

        var list = new List<object>();
        var collection = new Collection<object>();

        i = list.Length;
        i = collection.Length;
    }
}
", @"
using System.Collections.Generic;
using System.Collections.ObjectModel;

class C
{
    void M()
    {
        int i = 0;

        var list = new List<object>();
        var collection = new Collection<object>();

        i = list.Count;
        i = collection.Count;
    }
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId));
        }
Exemplo n.º 25
0
        public async Task Test()
        {
            await VerifyFixAsync(@"
#nullable enable

class C
{
    public C(string p)
    {
        P = p;
    }

    public string? P { get; }

    void M()
    {
        var x = new C(P);
    }
}
", @"
#nullable enable

class C
{
    public C(string p)
    {
        P = p;
    }

    public string? P { get; }

    void M()
    {
        var x = new C(P!);
    }
}
", equivalenceKey : EquivalenceKey.Create(DiagnosticId));
        }
        //[Fact]
        public async Task Test_BoolLocalFunctionWithReturnStatements()
        {
            await VerifyFixAsync(@"
class C
{
    void M()
    {
        bool LF(bool f, out object p2, out object p3)
        {
            if (f)
                return false;

            return false;
        }
    }
}
", @"
class C
{
    void M()
    {
        bool LF(bool f, out object p2, out object p3)
        {
            if (f)
            {
                p2 = null;
                p3 = null;
                return false;
            }

            p2 = null;
            p3 = null;
            return false;
        }
    }
}
", EquivalenceKey.Create(DiagnosticId));
        }
Exemplo n.º 27
0
        public async Task Test_LocalFunction()
        {
            await VerifyRefactoringAsync(@"
class C
{
    void M()
    {
        string LF()
        [|{
[||]            return null;
        }|]
    }
}
", @"
class C
{
    void M()
    {
        string LF() => null;
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
Exemplo n.º 28
0
        public async Task Test_VoidLocalFunction_Throw()
        {
            await VerifyRefactoringAsync(@"
class C
{
    void M()
    {
        void LF()
        [|{
            throw new System.NotImplementedException();
        }|]
    }
}
", @"
class C
{
    void M()
    {
        void LF() => throw new System.NotImplementedException();
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
Exemplo n.º 29
0
        public async Task Test_VoidLocalFunction()
        {
            await VerifyRefactoringAsync(@"
class C
{
    void M()
    {
        void LF()
        [|{
[||]            M();
        }|]
    }
}
", @"
class C
{
    void M()
    {
        void LF() => M();
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }
Exemplo n.º 30
0
        public async Task Test_StaticProperty()
        {
            await VerifyRefactoringAsync(@"
static class C
{
    private static string value;

    public static string [||]Value { get; set; } = null;
}
", @"
static class C
{
    private static string value;
    private static string value2 = null;

    public static string Value
    {
        get { return value2; }
        set { value2 = value; }
    }
}
", equivalenceKey : EquivalenceKey.Create(RefactoringId));
        }