Skip to content

Commit c7517d1

Browse files
committed
fix: suppress S6640 for intentional unsafe pointer overloads
SonarAnalyzer.CSharp 10.28+ reports the unsafe blocks in the char* Append/AppendIf/AppendLine/AppendLineIf overloads (and their tests) as build errors. These pointer overloads are an intentional high-performance API surface, so suppress S6640 locally instead of removing them.
1 parent 23e5739 commit c7517d1

7 files changed

Lines changed: 26 additions & 0 deletions

src/NetEvolve.CodeBuilder/CSharpCodeBuilder.Append.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public CSharpCodeBuilder Append(char[]? value)
124124
/// <param name="length">The number of characters to append.</param>
125125
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
126126
/// <remarks>If the pointer is null or length is negative, the method returns without appending anything.</remarks>
127+
#pragma warning disable S6640 // Unsafe code is intentional to support pointer-based, high-performance overloads
127128
public unsafe CSharpCodeBuilder Append(char* value, int length)
128129
{
129130
if (value == null || length < 0)
@@ -135,6 +136,7 @@ public unsafe CSharpCodeBuilder Append(char* value, int length)
135136
_ = _builder.Append(value, length);
136137
return this;
137138
}
139+
#pragma warning restore S6640 // Unsafe code is intentional to support pointer-based, high-performance overloads
138140

139141
/// <summary>
140142
/// Appends a read-only memory of characters to the current builder.

src/NetEvolve.CodeBuilder/CSharpCodeBuilder.AppendIf.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ public CSharpCodeBuilder AppendIf(bool condition, char[]? value, int startIndex,
6868
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
6969
/// <remarks>If the pointer is null or length is negative, the method returns without appending anything.</remarks>
7070
[MethodImpl(MethodImplOptions.AggressiveInlining)]
71+
#pragma warning disable S6640 // Unsafe code is intentional to support pointer-based, high-performance overloads
7172
public unsafe CSharpCodeBuilder AppendIf(bool condition, char* value, int length) =>
7273
condition ? Append(value, length) : this;
74+
#pragma warning restore S6640 // Unsafe code is intentional to support pointer-based, high-performance overloads
7375

7476
/// <summary>
7577
/// Appends a read-only memory of characters to the current builder if the specified condition is true.

src/NetEvolve.CodeBuilder/CSharpCodeBuilder.AppendLine.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ public CSharpCodeBuilder AppendLine(char[]? value, int startIndex, int charCount
101101
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
102102
/// <remarks>If the pointer is null or length is negative, only the line terminator is appended.</remarks>
103103
[MethodImpl(MethodImplOptions.AggressiveInlining)]
104+
#pragma warning disable S6640 // Unsafe code is intentional to support pointer-based, high-performance overloads
104105
public unsafe CSharpCodeBuilder AppendLine(char* value, int length) => Append(value, length).AppendLine();
106+
#pragma warning restore S6640 // Unsafe code is intentional to support pointer-based, high-performance overloads
105107

106108
/// <summary>
107109
/// Appends a character followed by a line terminator to the current builder.

src/NetEvolve.CodeBuilder/CSharpCodeBuilder.AppendLineIf.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ public CSharpCodeBuilder AppendLineIf(bool condition, char[]? value, int startIn
103103
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
104104
/// <remarks>If the pointer is null or length is negative, the method returns without appending anything.</remarks>
105105
[MethodImpl(MethodImplOptions.AggressiveInlining)]
106+
#pragma warning disable S6640 // Unsafe code is intentional to support pointer-based, high-performance overloads
106107
public unsafe CSharpCodeBuilder AppendLineIf(bool condition, char* value, int length) =>
107108
condition ? AppendLine(value, length) : this;
109+
#pragma warning restore S6640 // Unsafe code is intentional to support pointer-based, high-performance overloads
108110

109111
/// <summary>
110112
/// Appends a character followed by a line terminator to the current builder if the specified condition is true.

tests/NetEvolve.CodeBuilder.Tests.Unit/CSharpCodeBuilderTests.AppendIf.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,15 @@ public async Task AppendIf_CharPointer_Condition_True_Should_Append_Characters()
206206
var builder = new CSharpCodeBuilder(10);
207207
var chars = "abc".ToCharArray();
208208

209+
#pragma warning disable S6640 // Unsafe code is intentional for pointer-based test scenarios
209210
unsafe
210211
{
211212
fixed (char* ptr = chars)
212213
{
213214
_ = builder.AppendIf(true, ptr, chars.Length);
214215
}
215216
}
217+
#pragma warning restore S6640 // Unsafe code is intentional for pointer-based test scenarios
216218

217219
_ = await Assert.That(builder.ToString()).IsEqualTo("abc");
218220
}
@@ -223,13 +225,15 @@ public async Task AppendIf_CharPointer_Condition_False_Should_Not_Append()
223225
var builder = new CSharpCodeBuilder(10);
224226
var chars = "abc".ToCharArray();
225227

228+
#pragma warning disable S6640 // Unsafe code is intentional for pointer-based test scenarios
226229
unsafe
227230
{
228231
fixed (char* ptr = chars)
229232
{
230233
_ = builder.AppendIf(false, ptr, chars.Length);
231234
}
232235
}
236+
#pragma warning restore S6640 // Unsafe code is intentional for pointer-based test scenarios
233237

234238
_ = await Assert.That(builder.ToString()).IsEqualTo("");
235239
}
@@ -241,13 +245,15 @@ public async Task AppendIf_CharPointer_Should_Return_Same_Instance()
241245
var chars = "abc".ToCharArray();
242246
CSharpCodeBuilder result;
243247

248+
#pragma warning disable S6640 // Unsafe code is intentional for pointer-based test scenarios
244249
unsafe
245250
{
246251
fixed (char* ptr = chars)
247252
{
248253
result = builder.AppendIf(true, ptr, chars.Length);
249254
}
250255
}
256+
#pragma warning restore S6640 // Unsafe code is intentional for pointer-based test scenarios
251257

252258
_ = await Assert.That(result).IsEqualTo(builder);
253259
}

tests/NetEvolve.CodeBuilder.Tests.Unit/CSharpCodeBuilderTests.AppendLine.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,15 @@ public async Task AppendLine_Unsafe_CharPointer_Should_Append_Characters_With_Ne
206206
CSharpCodeBuilder result;
207207
string builderResult;
208208

209+
#pragma warning disable S6640 // Unsafe code is intentional for pointer-based test scenarios
209210
unsafe
210211
{
211212
fixed (char* ptr = text)
212213
{
213214
result = builder.AppendLine(ptr, text.Length);
214215
}
215216
}
217+
#pragma warning restore S6640 // Unsafe code is intentional for pointer-based test scenarios
216218

217219
builderResult = builder.ToString();
218220
_ = await Assert.That(result).IsEqualTo(builder);
@@ -226,10 +228,12 @@ public async Task AppendLine_Unsafe_CharPointer_Null_Should_Append_Only_Newline(
226228
CSharpCodeBuilder result;
227229
string builderResult;
228230

231+
#pragma warning disable S6640 // Unsafe code is intentional for pointer-based test scenarios
229232
unsafe
230233
{
231234
result = builder.AppendLine(null, 0);
232235
}
236+
#pragma warning restore S6640 // Unsafe code is intentional for pointer-based test scenarios
233237

234238
builderResult = builder.ToString();
235239
_ = await Assert.That(result).IsEqualTo(builder);
@@ -244,13 +248,15 @@ public async Task AppendLine_Unsafe_CharPointer_Negative_Length_Should_Append_On
244248
CSharpCodeBuilder result;
245249
string builderResult;
246250

251+
#pragma warning disable S6640 // Unsafe code is intentional for pointer-based test scenarios
247252
unsafe
248253
{
249254
fixed (char* ptr = text)
250255
{
251256
result = builder.AppendLine(ptr, -1);
252257
}
253258
}
259+
#pragma warning restore S6640 // Unsafe code is intentional for pointer-based test scenarios
254260

255261
builderResult = builder.ToString();
256262
_ = await Assert.That(result).IsEqualTo(builder);

tests/NetEvolve.CodeBuilder.Tests.Unit/CSharpCodeBuilderTests.AppendLineIf.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,15 @@ public async Task AppendLineIf_CharPointer_Condition_True_Should_Append_Characte
254254
var builder = new CSharpCodeBuilder(10);
255255
var chars = "abc".ToCharArray();
256256

257+
#pragma warning disable S6640 // Unsafe code is intentional for pointer-based test scenarios
257258
unsafe
258259
{
259260
fixed (char* ptr = chars)
260261
{
261262
_ = builder.AppendLineIf(true, ptr, chars.Length);
262263
}
263264
}
265+
#pragma warning restore S6640 // Unsafe code is intentional for pointer-based test scenarios
264266

265267
_ = await Assert.That(builder.ToString()).IsEqualTo("abc" + Environment.NewLine);
266268
}
@@ -271,13 +273,15 @@ public async Task AppendLineIf_CharPointer_Condition_False_Should_Not_Append()
271273
var builder = new CSharpCodeBuilder(10);
272274
var chars = "abc".ToCharArray();
273275

276+
#pragma warning disable S6640 // Unsafe code is intentional for pointer-based test scenarios
274277
unsafe
275278
{
276279
fixed (char* ptr = chars)
277280
{
278281
_ = builder.AppendLineIf(false, ptr, chars.Length);
279282
}
280283
}
284+
#pragma warning restore S6640 // Unsafe code is intentional for pointer-based test scenarios
281285

282286
_ = await Assert.That(builder.ToString()).IsEqualTo("");
283287
}
@@ -289,13 +293,15 @@ public async Task AppendLineIf_CharPointer_Should_Return_Same_Instance()
289293
var chars = "abc".ToCharArray();
290294
CSharpCodeBuilder result;
291295

296+
#pragma warning disable S6640 // Unsafe code is intentional for pointer-based test scenarios
292297
unsafe
293298
{
294299
fixed (char* ptr = chars)
295300
{
296301
result = builder.AppendLineIf(true, ptr, chars.Length);
297302
}
298303
}
304+
#pragma warning restore S6640 // Unsafe code is intentional for pointer-based test scenarios
299305

300306
_ = await Assert.That(result).IsEqualTo(builder);
301307
}

0 commit comments

Comments
 (0)