Skip to content

Commit 5093c17

Browse files
[Infra] Suppress code analysis warnings (#6905)
1 parent 1afaad6 commit 5093c17

4 files changed

Lines changed: 27 additions & 52 deletions

File tree

docs/logs/extending-the-sdk/extending-the-sdk.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<NoWarn>$(NoWarn);CA2000;CA1848;CA1510;CA1305</NoWarn>
3+
<NoWarn>$(NoWarn);CA2000;CA1848;CA1873;CA1510;CA1305</NoWarn>
44
</PropertyGroup>
55

66
<ItemGroup>

src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ public override ExportResult Export(in Batch<Activity> batch)
7878
Content = new JsonContent(this, batch),
7979
};
8080

81+
#pragma warning disable CA2025 // Do not pass 'IDisposable' instances into unawaited tasks
8182
#if NET
8283
using var response = this.synchronousSendSupportedByCurrentPlatform
8384
? this.httpClient.Send(request, CancellationToken.None)
8485
: this.httpClient.SendAsync(request, CancellationToken.None).GetAwaiter().GetResult();
8586
#else
8687
using var response = this.httpClient.SendAsync(request, CancellationToken.None).GetAwaiter().GetResult();
8788
#endif
89+
#pragma warning restore CA2025 // Do not pass 'IDisposable' instances into unawaited tasks
8890

8991
response.EnsureSuccessStatusCode();
9092

@@ -211,9 +213,7 @@ public JsonContent(ZipkinExporter exporter, in Batch<Activity> batch)
211213

212214
#if NET
213215
protected override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken)
214-
{
215-
this.SerializeToStreamInternal(stream);
216-
}
216+
=> this.SerializeToStreamInternal(stream);
217217
#endif
218218

219219
protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context)

test/Benchmarks/Logs/LogBenchmarks.cs

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,28 @@ public void GlobalCleanup()
9494

9595
[Benchmark]
9696
public void NoListenerStringInterpolation()
97-
{
9897
#pragma warning disable CA2254 // Template should be a static expression
9998
#pragma warning disable CA1848 // Use the LoggerMessage delegates
100-
this.loggerWithNoListener.LogInformation($"Hello from {FoodName} {FoodPrice}.");
99+
=> this.loggerWithNoListener.LogInformation($"Hello from {FoodName} {FoodPrice}.");
101100
#pragma warning restore CA1848 // Use the LoggerMessage delegates
102101
#pragma warning restore CA2254 // Template should be a static expression
103-
}
104102

105103
[Benchmark]
106104
public void NoListenerExtensionMethod()
107-
{
108105
#pragma warning disable CA1848 // Use the LoggerMessage delegates
109-
this.loggerWithNoListener.LogInformation("Hello from {Name} {Price}.", FoodName, FoodPrice);
106+
#pragma warning disable CA1873 // Avoid potentially expensive logging
107+
=> this.loggerWithNoListener.LogInformation("Hello from {Name} {Price}.", FoodName, FoodPrice);
108+
#pragma warning restore CA1873 // Avoid potentially expensive logging
110109
#pragma warning restore CA1848 // Use the LoggerMessage delegates
111-
}
112110

113111
[Benchmark]
114-
public void NoListener()
115-
{
112+
public void NoListener() =>
116113
this.loggerWithNoListener.FoodRecallNotice(
117114
brandName: "Contoso",
118115
productDescription: "Salads",
119116
productType: "Food & Beverages",
120117
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
121118
companyName: "Contoso Fresh Vegetables, Inc.");
122-
}
123119

124120
[Benchmark]
125121
public void UnnecessaryIsEnabledCheck()
@@ -148,58 +144,46 @@ public void CreateLoggerRepeatedly()
148144
}
149145

150146
[Benchmark]
151-
public void OneProcessor()
152-
{
147+
public void OneProcessor() =>
153148
this.loggerWithOneProcessor.FoodRecallNotice(
154149
brandName: "Contoso",
155150
productDescription: "Salads",
156151
productType: "Food & Beverages",
157152
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
158153
companyName: "Contoso Fresh Vegetables, Inc.");
159-
}
160154

161155
[Benchmark]
162-
public void BatchProcessor()
163-
{
156+
public void BatchProcessor() =>
164157
this.loggerWithBatchProcessor.FoodRecallNotice(
165158
brandName: "Contoso",
166159
productDescription: "Salads",
167160
productType: "Food & Beverages",
168161
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
169162
companyName: "Contoso Fresh Vegetables, Inc.");
170-
}
171163

172164
[Benchmark]
173-
public void TwoProcessors()
174-
{
165+
public void TwoProcessors() =>
175166
this.loggerWithTwoProcessors.FoodRecallNotice(
176167
brandName: "Contoso",
177168
productDescription: "Salads",
178169
productType: "Food & Beverages",
179170
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
180171
companyName: "Contoso Fresh Vegetables, Inc.");
181-
}
182172

183173
[Benchmark]
184-
public void ThreeProcessors()
185-
{
174+
public void ThreeProcessors() =>
186175
this.loggerWithThreeProcessors.FoodRecallNotice(
187176
brandName: "Contoso",
188177
productDescription: "Salads",
189178
productType: "Food & Beverages",
190179
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
191180
companyName: "Contoso Fresh Vegetables, Inc.");
192-
}
193181

194-
internal sealed class NoopLogProcessor : BaseProcessor<LogRecord>
195-
{
196-
}
182+
internal sealed class NoopLogProcessor : BaseProcessor<LogRecord>;
197183

198184
internal sealed class NoopExporter : BaseExporter<LogRecord>
199185
{
200186
public override ExportResult Export(in Batch<LogRecord> batch)
201-
{
202-
return ExportResult.Success;
203-
}
187+
=> ExportResult.Success;
204188
}
205189
}

test/OpenTelemetry.Tests/Logs/LogRecordTests.cs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace OpenTelemetry.Logs.Tests;
1414

15+
#pragma warning disable CA1873 // Avoid potentially expensive logging
16+
1517
public sealed class LogRecordTests
1618
{
1719
private enum Field
@@ -1085,7 +1087,7 @@ public void CheckOriginalFormatAtArbitraryPosition(bool includeFormattedMessage,
10851087

10861088
private static ILoggerFactory InitializeLoggerFactory(out List<LogRecord> exportedItems, Action<OpenTelemetryLoggerOptions>? configure = null)
10871089
{
1088-
var items = exportedItems = new List<LogRecord>();
1090+
var items = exportedItems = [];
10891091

10901092
return LoggerFactory.Create(builder =>
10911093
{
@@ -1119,19 +1121,14 @@ public StructState(params KeyValuePair<string, object>[] items)
11191121
public KeyValuePair<string, object> this[int index] => this.list[index];
11201122

11211123
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
1122-
{
1123-
return this.list.GetEnumerator();
1124-
}
1124+
=> this.list.GetEnumerator();
11251125

11261126
IEnumerator IEnumerable.GetEnumerator()
1127-
{
1128-
return this.list.GetEnumerator();
1129-
}
1127+
=> this.list.GetEnumerator();
11301128
}
11311129

11321130
internal sealed class DisposingState : IReadOnlyList<KeyValuePair<string, object?>>, IDisposable
11331131
{
1134-
private string? value;
11351132
private bool disposed;
11361133

11371134
public DisposingState(string? value)
@@ -1154,9 +1151,9 @@ public string? Value
11541151
}
11551152
#endif
11561153

1157-
return this.value;
1154+
return field;
11581155
}
1159-
private set => this.value = value;
1156+
private set;
11601157
}
11611158

11621159
public KeyValuePair<string, object?> this[int index] => index switch
@@ -1168,9 +1165,7 @@ public string? Value
11681165
};
11691166

11701167
public void Dispose()
1171-
{
1172-
this.disposed = true;
1173-
}
1168+
=> this.disposed = true;
11741169

11751170
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()
11761171
{
@@ -1200,7 +1195,7 @@ public override void OnEnd(LogRecord logRecord)
12001195
}
12011196
else if (this.fieldToUpdate == Field.StateValues)
12021197
{
1203-
logRecord.StateValues = new List<KeyValuePair<string, object?>> { new("newStateValueKey", "newStateValueValue") };
1198+
logRecord.StateValues = [new("newStateValueKey", "newStateValueValue")];
12041199
}
12051200
else
12061201
{
@@ -1219,14 +1214,10 @@ public ListState(params KeyValuePair<string, object>[] items)
12191214
}
12201215

12211216
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
1222-
{
1223-
return this.list.GetEnumerator();
1224-
}
1217+
=> this.list.GetEnumerator();
12251218

12261219
IEnumerator IEnumerable.GetEnumerator()
1227-
{
1228-
return this.list.GetEnumerator();
1229-
}
1220+
=> this.list.GetEnumerator();
12301221
}
12311222

12321223
private sealed class CustomState
@@ -1248,7 +1239,7 @@ public ScopeProcessor(bool buffer)
12481239
this.buffer = buffer;
12491240
}
12501241

1251-
public List<object?> Scopes { get; } = new();
1242+
public List<object?> Scopes { get; } = [];
12521243

12531244
public override void OnEnd(LogRecord data)
12541245
{

0 commit comments

Comments
 (0)