-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathIgnoreStringMethodReturnValueAnalyzerTests.cs
More file actions
227 lines (188 loc) · 7.41 KB
/
Copy pathIgnoreStringMethodReturnValueAnalyzerTests.cs
File metadata and controls
227 lines (188 loc) · 7.41 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using VerifyCS = MSTest.Analyzers.Test.CSharpCodeFixVerifier<
MSTest.Analyzers.IgnoreStringMethodReturnValueAnalyzer,
Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>;
namespace MSTest.Analyzers.Test;
[TestClass]
public sealed class IgnoreStringMethodReturnValueAnalyzerTests
{
[TestMethod]
public async Task WhenStringMethodReturnValueIsUsed_NoDiagnostic()
{
string code = """
using System;
public class TestClass
{
public void TestMethod()
{
string str = "Hello World";
// Return values are used - should not trigger diagnostic
bool result1 = str.Contains("Hello");
bool result2 = str.StartsWith("Hello");
bool result3 = str.EndsWith("World");
// Used in conditions
if (str.Contains("Hello"))
{
Console.WriteLine("Found");
}
// Used in expressions
bool combined = str.StartsWith("Hello") && str.EndsWith("World");
// Used in return statements
bool IsValid() => str.Contains("test");
// Used in assignments with method chaining
bool hasHello = str.ToLower().Contains("hello");
}
}
""";
await VerifyCS.VerifyAnalyzerAsync(code);
}
[TestMethod]
public async Task WhenStringMethodReturnValueIsIgnored_Diagnostic()
{
string code = """
using System;
public class TestClass
{
public void TestMethod()
{
string str = "Hello World";
// Return values are ignored - should trigger diagnostics
[|str.Contains("Hello")|];
[|str.StartsWith("Hello")|];
[|str.EndsWith("World")|];
// Multiple calls on same line
[|str.Contains("test")|]; [|str.StartsWith("test")|];
}
}
""";
await VerifyCS.VerifyAnalyzerAsync(code);
}
[TestMethod]
public async Task WhenNonStringMethodsCalled_NoDiagnostic()
{
string code = """
using System;
using System.Collections.Generic;
public class TestClass
{
public void TestMethod()
{
var list = new List<string>();
// These are not string methods, should not trigger
list.Add("test");
Console.WriteLine("test");
// Custom Contains method - should not trigger
var custom = new CustomClass();
custom.Contains("test");
}
}
public class CustomClass
{
public bool Contains(string value) => true;
}
""";
await VerifyCS.VerifyAnalyzerAsync(code);
}
[TestMethod]
public async Task WhenStringMethodsFromOtherSources_Diagnostic()
{
string code = """
public class TestClass
{
public void TestMethod()
{
string str1 = "test";
string str2 = GetString();
// Return values from different sources are ignored
[|str1.Contains("e")|];
[|str2.StartsWith("t")|];
[|GetString().EndsWith("t")|];
[|"literal".Contains("l")|];
}
private string GetString() => "test";
}
""";
await VerifyCS.VerifyAnalyzerAsync(code);
}
[TestMethod]
public async Task WhenStringMethodsWithParameters_Diagnostic()
{
string code = """
using System;
public class TestClass
{
public void TestMethod()
{
string str = "Hello World";
// Methods with different parameter overloads
[|str.Contains("Hello")|];
[|str.Contains("Hello", StringComparison.OrdinalIgnoreCase)|];
[|str.StartsWith("Hello")|];
[|str.StartsWith("Hello", StringComparison.OrdinalIgnoreCase)|];
[|str.EndsWith("World")|];
[|str.EndsWith("World", StringComparison.OrdinalIgnoreCase)|];
}
}
""";
await VerifyCS.VerifyAnalyzerAsync(code);
}
[TestMethod]
public async Task WhenExplicitDiscardAssignment_NoDiagnostic()
{
string code = """
public class TestClass
{
public void TestMethod()
{
string str = "Hello World";
// Discard assignment: outer operation is ISimpleAssignmentOperation, not IInvocationOperation,
// so the analyzer's early-return guard fires and no diagnostic is reported.
_ = str.Contains("Hello");
_ = str.StartsWith("Hello");
_ = str.EndsWith("World");
}
}
""";
await VerifyCS.VerifyAnalyzerAsync(code);
}
[TestMethod]
public async Task WhenStringMethodReturnValueIgnoredInsideLambda_Diagnostic()
{
string code = """
using System;
public class TestClass
{
public void TestMethod()
{
string str = "Hello World";
// ExpressionStatement inside a lambda block body is still an ExpressionStatement
// operation, so the analyzer fires inside the lambda just as it does at method level.
Action action = () => { [|str.Contains("Hello")|]; };
Action action2 = () => { [|str.StartsWith("World")|]; };
}
}
""";
await VerifyCS.VerifyAnalyzerAsync(code);
}
[TestMethod]
public async Task WhenStringMethodResultUsedAsReceiverForChainedCall_NoDiagnostic()
{
string code = """
public class TestClass
{
public void TestMethod()
{
string str = "Hello World";
// The ExpressionStatement's outermost operation is GetHashCode() (on System.Boolean),
// not Contains/StartsWith/EndsWith on System.String, so no diagnostic is reported.
// The string method's return value IS used — as the receiver of GetHashCode().
str.Contains("Hello").GetHashCode();
str.StartsWith("Hello").GetHashCode();
str.EndsWith("World").GetHashCode();
}
}
""";
await VerifyCS.VerifyAnalyzerAsync(code);
}
}