Skip to content

Commit 624ee29

Browse files
authored
Enable some of the unnecessary code rules (#799)
* CA1823 Avoid unused private fields * CA1847 Use string.Contains(char) instead of string.Contains(string) with single characters * IDE0005 Remove unnecessary import * IDE0051 Private member is unused * IDE0052 Private member is unread
1 parent 6f4c479 commit 624ee29

27 files changed

+35
-70
lines changed

.editorconfig

+18-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,21 @@ csharp_new_line_before_else = true
4444
csharp_new_line_before_catch = true
4545
csharp_new_line_before_finally = true
4646
csharp_new_line_before_members_in_object_initializers = true
47-
csharp_new_line_before_members_in_anonymous_types = true
47+
csharp_new_line_before_members_in_anonymous_types = true
48+
49+
# https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/unnecessary-code-rules
50+
51+
# Avoid unused private fields
52+
dotnet_diagnostic.CA1823.severity = error
53+
54+
# Use string.Contains(char) instead of string.Contains(string) with single characters
55+
dotnet_diagnostic.CA1847.severity = error
56+
57+
# Remove unnecessary import
58+
dotnet_diagnostic.IDE0005.severity = error
59+
60+
# Private member is unused
61+
dotnet_diagnostic.IDE0051.severity = error
62+
63+
# Private member is unread
64+
dotnet_diagnostic.IDE0052.severity = error

Directory.Build.props

+2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)\kubernetes-client.ruleset</CodeAnalysisRuleSet>
5+
<EnableNETAnalyzers>true</EnableNETAnalyzers>
6+
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
57
</PropertyGroup>
68
</Project>

src/KubernetesClient/Authentication/ExecTokenProvider.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System.Diagnostics;
21
using System.Net.Http.Headers;
32
using System.Threading;
43
using System.Threading.Tasks;
5-
using k8s.Exceptions;
64
using k8s.KubeConfigModels;
75
using k8s.Autorest;
86

src/KubernetesClient/Autorest/BasicAuthenticationCredentials.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Globalization;
65
using System.Net.Http;
76
using System.Net.Http.Headers;

src/KubernetesClient/Autorest/HttpExtensions.cs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Net.Http;
55
using System.Net.Http.Headers;
6-
using System.Text;
76

87
namespace k8s.Autorest
98
{

src/KubernetesClient/Autorest/HttpMessageWrapper.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System.Collections.Generic;
5-
using System.Linq;
64
using System.Net.Http.Headers;
75

86
namespace k8s.Autorest

src/KubernetesClient/Autorest/HttpOperationException.cs

-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.Net.Http;
64
using System.Runtime.Serialization;
7-
using System.Security.Permissions;
85

96
namespace k8s.Autorest
107
{

src/KubernetesClient/Autorest/HttpOperationResponse.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Net.Http;
65

76
namespace k8s.Autorest

src/KubernetesClient/Autorest/HttpRequestMessageWrapper.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.Collections.Generic;
64
using System.Net.Http;
75

86
namespace k8s.Autorest

src/KubernetesClient/Autorest/HttpResponseMessageWrapper.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Net;
65
using System.Net.Http;
76

src/KubernetesClient/Autorest/RestException.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Runtime.Serialization;
65

76
namespace k8s.Autorest

src/KubernetesClient/Autorest/TokenCredentials.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Net.Http;
65
using System.Threading;
76
using System.Threading.Tasks;

src/KubernetesClient/CertUtils.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
using Org.BouncyCastle.Pkcs;
66
using Org.BouncyCastle.Security;
77
using Org.BouncyCastle.X509;
8+
#else
9+
using System.Runtime.InteropServices;
10+
using System.Text;
811
#endif
912
using System.IO;
10-
using System.Runtime.InteropServices;
1113
using System.Security.Cryptography.X509Certificates;
12-
using System.Text;
1314

1415
namespace k8s
1516
{

src/KubernetesClient/Kubernetes.ConfigInit.cs

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System.Net;
21
using System.Net.Http;
32
using System.Net.Security;
4-
using System.Net.Sockets;
5-
using System.Reflection;
6-
using System.Runtime.InteropServices;
73
using System.Security.Cryptography.X509Certificates;
84
using System.Threading;
95
using k8s.Exceptions;

src/KubernetesClient/Kubernetes.cs

-16
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,6 @@ public Uri BaseUri
3131

3232
public HttpClient HttpClient { get; protected set; }
3333

34-
private IEnumerable<HttpMessageHandler> HttpMessageHandlers
35-
{
36-
get
37-
{
38-
var handler = FirstMessageHandler;
39-
40-
while (handler != null)
41-
{
42-
yield return handler;
43-
44-
DelegatingHandler delegating = handler as DelegatingHandler;
45-
handler = delegating != null ? delegating.InnerHandler : null;
46-
}
47-
}
48-
}
49-
5034
/// <summary>
5135
/// Reference to the first HTTP handler (which is the start of send HTTP
5236
/// pipeline).

src/KubernetesClient/KubernetesList.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using k8s.Autorest;
2-
31
namespace k8s.Models
42
{
53
public class KubernetesList<T> : IMetadata<V1ListMeta>, IItems<T>

src/KubernetesClient/Watcher.cs

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class Watcher<T> : IDisposable
4646
private readonly Func<Task<TextReader>> _streamReaderCreator;
4747

4848
private bool disposedValue;
49+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "IDE0052:Remove unread private members", Justification = "Used in an async task loop")]
4950
private readonly Task _watcherLoop;
5051

5152

src/LibKubernetesGenerator/ApiGenerator.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System.Collections.Generic;
2-
using System.IO;
32
using System.Linq;
43
using Microsoft.CodeAnalysis;
54
using NSwag;
6-
using Nustache.Core;
75

86
namespace LibKubernetesGenerator
97
{

src/LibKubernetesGenerator/GeneralNameHelper.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Linq;
43
using System.Text.RegularExpressions;

src/LibKubernetesGenerator/GeneratorExecutionContextExt.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.Text;
33
using Nustache.Core;
4-
using System;
5-
using System.Collections.Generic;
64
using System.IO;
75
using System.Text;
86

src/LibKubernetesGenerator/KubernetesClientSourceGenerator.cs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Nustache.Core;
55
using System;
66
using System.Collections.Generic;
7-
using System.Diagnostics;
87
using System.Linq;
98
using System.Reflection;
109

src/LibKubernetesGenerator/ModelExtGenerator.cs

-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
using System;
21
using System.Collections.Generic;
3-
using System.IO;
42
using System.Linq;
53
using Microsoft.CodeAnalysis;
64
using NSwag;
7-
using Nustache.Core;
85

96
namespace LibKubernetesGenerator
107
{

src/LibKubernetesGenerator/ModelGenerator.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System.IO;
21
using Microsoft.CodeAnalysis;
32
using NSwag;
4-
using Nustache.Core;
53

64
namespace LibKubernetesGenerator
75
{

src/LibKubernetesGenerator/UtilHelper.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using System.Runtime.CompilerServices;
43
using NSwag;
54
using Nustache.Core;
65

tests/KubernetesClient.Tests/StreamDemuxerTests.cs

-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ namespace k8s.Tests
1212
{
1313
public class StreamDemuxerTests
1414
{
15-
private readonly ITestOutputHelper testOutput;
16-
17-
public StreamDemuxerTests(ITestOutputHelper testOutput)
18-
{
19-
this.testOutput = testOutput;
20-
}
21-
2215
[Fact]
2316
public async Task SendDataRemoteCommand()
2417
{

tests/KubernetesClient.Tests/Util/Informer/Cache/CacheTest.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ namespace k8s.Tests.Util.Informer.Cache
1111
public class CacheTest
1212
{
1313
[Fact(DisplayName = "Create default cache success")]
14-
private void CreateCacheSuccess()
14+
public void CreateCacheSuccess()
1515
{
1616
var cache = new Cache<V1Node>();
1717
cache.Should().NotBeNull();
1818
cache.GetIndexers().ContainsKey(Caches.NamespaceIndex).Should().BeTrue();
1919
}
2020

2121
[Fact(DisplayName = "Add cache item success")]
22-
private void AddCacheItemSuccess()
22+
public void AddCacheItemSuccess()
2323
{
2424
var aPod = Helpers.CreatePods(1).First();
2525
var cache = new Cache<V1Pod>();
@@ -30,7 +30,7 @@ private void AddCacheItemSuccess()
3030
}
3131

3232
[Fact(DisplayName = "Update cache item success")]
33-
private void UpdateCacheItemSuccess()
33+
public void UpdateCacheItemSuccess()
3434
{
3535
var aPod = Helpers.CreatePods(1).First();
3636

@@ -44,7 +44,7 @@ private void UpdateCacheItemSuccess()
4444
}
4545

4646
[Fact(DisplayName = "Delete cache item success")]
47-
private void DeleteCacheItemSuccess()
47+
public void DeleteCacheItemSuccess()
4848
{
4949
var aPod = Helpers.CreatePods(1).First();
5050

@@ -58,7 +58,7 @@ private void DeleteCacheItemSuccess()
5858
}
5959

6060
[Fact(DisplayName = "Replace cache items success")]
61-
private void ReplaceCacheItemsSuccess()
61+
public void ReplaceCacheItemsSuccess()
6262
{
6363
var pods = Helpers.CreatePods(3);
6464
var aPod = pods.First();

tests/KubernetesClient.Tests/Util/Informer/Cache/ListerTest.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace k8s.Tests.Util.Informer.Cache
99
public class ListerTest
1010
{
1111
[Fact(DisplayName = "Create default lister success")]
12-
private void CreateListerDefaultsSuccess()
12+
public void CreateListerDefaultsSuccess()
1313
{
1414
var cache = new Cache<V1Pod>();
1515
var lister = new Lister<V1Pod>(cache);
@@ -18,7 +18,7 @@ private void CreateListerDefaultsSuccess()
1818
}
1919

2020
[Fact(DisplayName = "List with null namespace success")]
21-
private void ListNullNamespaceSuccess()
21+
public void ListNullNamespaceSuccess()
2222
{
2323
var aPod = Helpers.CreatePods(1).First();
2424
var cache = new Cache<V1Pod>();
@@ -33,7 +33,7 @@ private void ListNullNamespaceSuccess()
3333
}
3434

3535
[Fact(DisplayName = "List with custom namespace success")]
36-
private void ListCustomNamespaceSuccess()
36+
public void ListCustomNamespaceSuccess()
3737
{
3838
var aPod = Helpers.CreatePods(1).First();
3939
var cache = new Cache<V1Pod>();
@@ -48,7 +48,7 @@ private void ListCustomNamespaceSuccess()
4848
}
4949

5050
[Fact(DisplayName = "Get with null namespace success")]
51-
private void GetNullNamespaceSuccess()
51+
public void GetNullNamespaceSuccess()
5252
{
5353
var aPod = Helpers.CreatePods(1).First();
5454
var cache = new Cache<V1Pod>();
@@ -63,7 +63,7 @@ private void GetNullNamespaceSuccess()
6363
}
6464

6565
[Fact(DisplayName = "Get with custom namespace success")]
66-
private void GetCustomNamespaceSuccess()
66+
public void GetCustomNamespaceSuccess()
6767
{
6868
var aPod = Helpers.CreatePods(1).First();
6969
var cache = new Cache<V1Pod>();
@@ -76,7 +76,7 @@ private void GetCustomNamespaceSuccess()
7676
}
7777

7878
[Fact(DisplayName = "Set custom namespace success")]
79-
private void SetCustomNamespaceSuccess()
79+
public void SetCustomNamespaceSuccess()
8080
{
8181
var aPod = Helpers.CreatePods(1).First();
8282
var cache = new Cache<V1Pod>();

0 commit comments

Comments
 (0)