/
RCS1246UseElementAccessTests.cs
240 lines (207 loc) · 6.85 KB
/
RCS1246UseElementAccessTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Roslynator.CSharp.CodeFixes;
using Roslynator.Testing.CSharp;
using Xunit;
namespace Roslynator.CSharp.Analysis.Tests;
public class RCS1246UseElementAccessTests : AbstractCSharpDiagnosticVerifier<InvocationExpressionAnalyzer, OptimizeLinqMethodCallCodeFixProvider>
{
public override DiagnosticDescriptor Descriptor { get; } = DiagnosticRules.UseElementAccess;
[Theory, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
[InlineData("((List<object>)x).[|First()|]", "((List<object>)x)[0]")]
[InlineData("((IList<object>)x).[|First()|]", "((IList<object>)x)[0]")]
[InlineData("((IReadOnlyList<object>)x).[|First()|]", "((IReadOnlyList<object>)x)[0]")]
[InlineData("((Collection<object>)x).[|First()|]", "((Collection<object>)x)[0]")]
[InlineData("((ImmutableArray<object>)x).[|First()|]", "((ImmutableArray<object>)x)[0]")]
[InlineData("((object[])x).[|First()|]", "((object[])x)[0]")]
[InlineData("((string)x).[|First()|]", "((string)x)[0]")]
public async Task Test_UseElementAccessInsteadOfFirst(string source, string expected)
{
await VerifyDiagnosticAndFixAsync(@"
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
class C
{
void M()
{
object x = null;
var y = [||];
}
}
", source, expected);
}
[Theory, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
[InlineData("((List<object>)x).[|ElementAt(1)|]", "((List<object>)x)[1]")]
[InlineData("((IList<object>)x).[|ElementAt(1)|]", "((IList<object>)x)[1]")]
[InlineData("((IReadOnlyList<object>)x).[|ElementAt(1)|]", "((IReadOnlyList<object>)x)[1]")]
[InlineData("((Collection<object>)x).[|ElementAt(1)|]", "((Collection<object>)x)[1]")]
[InlineData("((ImmutableArray<object>)x).[|ElementAt(1)|]", "((ImmutableArray<object>)x)[1]")]
[InlineData("((object[])x).[|ElementAt(1)|]", "((object[])x)[1]")]
[InlineData("((string)x).[|ElementAt(1)|]", "((string)x)[1]")]
public async Task Test_UseElementAccessInsteadOfElementAt(string source, string expected)
{
await VerifyDiagnosticAndFixAsync(@"
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
class C
{
void M()
{
object x = null;
var y = [||];
}
}
", source, expected);
}
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
public async Task TestNoDiagnostic_UseElementAccessInsteadOfElementAt()
{
await VerifyNoDiagnosticAsync(@"
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
class C
{
void M()
{
object x = null;
x = ((ICollection<object>)x).ElementAt(1);
x = ((IReadOnlyCollection<object>)x).ElementAt(1);
x = ((IEnumerable<object>)x).ElementAt(1);
x = ((Dictionary<object, object>)x).ElementAt(1);
}
}
");
}
[Theory, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
[InlineData("((List<object>)x).[|Last()|]", "((List<object>)x)[^1]")]
[InlineData("((IList<object>)x).[|Last()|]", "((IList<object>)x)[^1]")]
[InlineData("((IReadOnlyList<object>)x).[|Last()|]", "((IReadOnlyList<object>)x)[^1]")]
[InlineData("((Collection<object>)x).[|Last()|]", "((Collection<object>)x)[^1]")]
[InlineData("((ImmutableArray<object>)x).[|Last()|]", "((ImmutableArray<object>)x)[^1]")]
[InlineData("((object[])x).[|Last()|]", "((object[])x)[^1]")]
[InlineData("((string)x).[|Last()|]", "((string)x)[^1]")]
public async Task Test_UseElementAccessInsteadOfLast(string source, string expected)
{
await VerifyDiagnosticAndFixAsync(@"
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
class C
{
void M()
{
object x = null;
var y = [||];
}
}
", source, expected);
}
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
public async Task TestNoDiagnostic_UseElementAccessInsteadOfLast()
{
await VerifyNoDiagnosticAsync(@"
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
class C
{
void M()
{
object x = null;
x = ((ICollection<object>)x).Last();
x = ((IReadOnlyCollection<object>)x).Last();
x = ((IEnumerable<object>)x).Last();
x = ((Dictionary<object, object>)x).Last();
}
}
");
}
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
public async Task TestNoDiagnostic_UseElementAccessInsteadOfFirst()
{
await VerifyNoDiagnosticAsync(@"
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
class C
{
void M()
{
object x = null;
x = ((ICollection<object>)x).First();
x = ((IReadOnlyCollection<object>)x).First();
x = ((IEnumerable<object>)x).First();
x = ((Dictionary<object, object>)x).First();
}
}
");
}
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
public async Task TestNoDiagnostic_UseElementAccessOnElementAccess()
{
await VerifyNoDiagnosticAsync(@"
using System.Linq;
using System.Collections.Generic;
class C
{
List<object> this[int index] => null;
void M()
{
C x = default;
var first = x[0].First();
var second = x[0].ElementAt(1);
}
}
");
}
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
public async Task TestNoDiagnostic_UseElementAccess_ExpressionStatement()
{
await VerifyNoDiagnosticAsync(@"
using System.Linq;
using System.Collections.Generic;
class C
{
void M()
{
object x = null;
((List<object>)x).First();
((List<object>)x).ElementAt(1);
}
}
");
}
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
public async Task TestNoDiagnostic_UseElementAccessInsteadOfElementAt_InfiniteRecursion()
{
await VerifyNoDiagnosticAsync(@"
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class C : IReadOnlyList<int>
{
public int this[int index] => this.ElementAt(index);
public int Count => throw new NotImplementedException();
public IEnumerator<int> GetEnumerator() => throw new NotImplementedException();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
");
}
}