Skip to content

Commit 362269c

Browse files
authored
Establish basic style analyzer rules (#3759)
* Fix analyzer nullable error can't disable them in CA1305 xdd * .editorconfig -> .globalconfig * Add basic set of style analyzer rules * dotnet format output * Some additional fixes * Apply manual simplification * Transform some enum comparisons * fix typo * add TODO
1 parent 7193313 commit 362269c

File tree

254 files changed

+547
-587
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

254 files changed

+547
-587
lines changed

.editorconfig

+101-13
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,107 @@ csharp_indent_switch_labels = true
1212
csharp_indent_case_contents = true
1313
csharp_indent_labels = one_less_than_current
1414

15-
# Globalization rules
16-
dotnet_code_quality.CA1305.excluded_symbol_names = T:System.Byte|T:System.SByte|T:System.Int16|T:System.UInt16|T:System.Int32|T:System.UInt32|T:System.Int64|T:System.UInt64|T:System.String|T:System.Text.StringBuilder|T:System.Convert
15+
# Style rules
16+
# Can't be in .globalconfig because dotnet format doesn't respect that https://github.com/dotnet/format/issues/1643
1717

18-
dotnet_diagnostic.CA1305.severity = error
19-
dotnet_diagnostic.CA2101.severity = suggestion
18+
# Remove unnecessary cast
19+
dotnet_diagnostic.IDE0004.severity = warning
20+
# Remove unnecessary import
21+
dotnet_diagnostic.IDE0005.severity = warning
22+
# Use var instead of explicit type
23+
dotnet_diagnostic.IDE0007.severity = suggestion
24+
# Use explicit type instead of var
25+
dotnet_diagnostic.IDE0008.severity = suggestion
26+
# Inline variable declaration
27+
dotnet_diagnostic.IDE0018.severity = warning
28+
# Use pattern matching to avoid as followed by a null check
29+
dotnet_diagnostic.IDE0019.severity = warning
30+
# Use pattern matching to avoid is check followed by a cast (with variable)
31+
dotnet_diagnostic.IDE0020.severity = warning
32+
# Use expression body for methods
33+
dotnet_diagnostic.IDE0022.severity = suggestion
34+
# Use expression body for properties
35+
dotnet_diagnostic.IDE0025.severity = suggestion
36+
# Use expression body for indexers
37+
dotnet_diagnostic.IDE0026.severity = suggestion
38+
# Use expression body for accessors
39+
dotnet_diagnostic.IDE0027.severity = suggestion
40+
# Null check can be simplified
41+
dotnet_diagnostic.IDE0029.severity = warning
42+
# Null check can be simplified
43+
dotnet_diagnostic.IDE0030.severity = warning
44+
# Use null propagation
45+
dotnet_diagnostic.IDE0031.severity = warning
46+
# Use auto property
47+
dotnet_diagnostic.IDE0032.severity = suggestion
48+
# Simplify default expression
49+
dotnet_diagnostic.IDE0034.severity = suggestion
50+
# Use pattern matching to avoid is check followed by a cast (without variable)
51+
dotnet_diagnostic.IDE0038.severity = warning
52+
# Use is null check
53+
dotnet_diagnostic.IDE0041.severity = warning
54+
# Deconstruct variable declaration
55+
dotnet_diagnostic.IDE0042.severity = suggestion
56+
# dotnet_diagnostic.IDE0049.severity = error # see SA1121
57+
# Remove unused private member
58+
dotnet_diagnostic.IDE0051.severity = suggestion
59+
# Remove unread private member
60+
dotnet_diagnostic.IDE0052.severity = silent # TODO: should be warning imo, but there's too much violation currently
61+
# Use compound assignment
62+
dotnet_diagnostic.IDE0054.severity = warning
63+
# Use index operator
64+
dotnet_diagnostic.IDE0056.severity = warning
65+
# Use range operator
66+
dotnet_diagnostic.IDE0057.severity = warning
67+
# Use simple using statement
68+
dotnet_diagnostic.IDE0063.severity = suggestion
69+
# Make struct fields writable
70+
dotnet_diagnostic.IDE0064.severity = error
71+
# using directive placement
72+
dotnet_diagnostic.IDE0065.severity = error
73+
# Use switch expression
74+
dotnet_diagnostic.IDE0066.severity = suggestion
75+
# Use System.HashCode.Combine
76+
dotnet_diagnostic.IDE0070.severity = warning
77+
# Simplify interpolation
78+
dotnet_diagnostic.IDE0071.severity = suggestion
79+
# Use coalesce compound assignment
80+
dotnet_diagnostic.IDE0074.severity = suggestion
81+
# Use pattern matching
82+
dotnet_diagnostic.IDE0078.severity = suggestion
83+
# Convert typeof to nameof
84+
dotnet_diagnostic.IDE0082.severity = warning
85+
# Use pattern matching (not operator)
86+
dotnet_diagnostic.IDE0083.severity = warning
87+
# Simplify new expression
88+
dotnet_diagnostic.IDE0090.severity = suggestion
89+
# Remove unnecessary equality operator
90+
dotnet_diagnostic.IDE0100.severity = warning
91+
# Remove unnecessary discard
92+
dotnet_diagnostic.IDE0110.severity = warning
93+
# Simplify LINQ expression
94+
dotnet_diagnostic.IDE0120.severity = error
95+
# Namespace does not match folder structure
96+
dotnet_diagnostic.IDE0130.severity = silent # should be warning imo
97+
# Use tuple to swap values
98+
dotnet_diagnostic.IDE0180.severity = suggestion
99+
# Use UTF-8 string literal
100+
dotnet_diagnostic.IDE0230.severity = warning
101+
# Nullable directive is redundant
102+
dotnet_diagnostic.IDE0240.severity = warning
103+
# Nullable directive is unnecessary
104+
dotnet_diagnostic.IDE0241.severity = warning
105+
# Struct can be made 'readonly'
106+
dotnet_diagnostic.IDE0250.severity = suggestion
107+
# Use pattern matching
108+
dotnet_diagnostic.IDE0260.severity = suggestion
109+
# Use nameof
110+
dotnet_diagnostic.IDE0280.severity = error
20111

21-
# Performance rules
22-
dotnet_code_quality.CA1826.exclude_ordefault_methods = true
112+
csharp_style_var_when_type_is_apparent = true
113+
csharp_style_var_elsewhere = true
23114

24-
dotnet_diagnostic.CA1805.severity = silent
25-
dotnet_diagnostic.CA1822.severity = silent
26-
dotnet_diagnostic.CA1838.severity = suggestion
27-
28-
# Usage rules
29-
dotnet_diagnostic.CA1816.severity = none
30-
dotnet_diagnostic.CA2201.severity = suggestion
115+
csharp_style_expression_bodied_methods = when_on_single_line
116+
csharp_style_expression_bodied_properties = when_on_single_line
117+
csharp_style_expression_bodied_indexers = when_on_single_line
118+
csharp_style_expression_bodied_accessors = when_on_single_line

.globalconfig

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
is_global = true
2+
3+
# Globalization rules
4+
dotnet_code_quality.CA1305.excluded_symbol_names = T:System.Byte|T:System.SByte|T:System.Int16|T:System.UInt16|T:System.Int32|T:System.UInt32|T:System.Int64|T:System.UInt64|T:System.String|T:System.Text.StringBuilder|T:System.Convert
5+
6+
dotnet_diagnostic.CA1305.severity = error
7+
dotnet_diagnostic.CA2101.severity = suggestion
8+
9+
# Performance rules
10+
dotnet_code_quality.CA1826.exclude_ordefault_methods = true
11+
12+
dotnet_diagnostic.CA1805.severity = silent
13+
dotnet_diagnostic.CA1822.severity = silent
14+
dotnet_diagnostic.CA1838.severity = suggestion
15+
16+
# Usage rules
17+
dotnet_diagnostic.CA1816.severity = none
18+
dotnet_diagnostic.CA2201.severity = suggestion

Common.props

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<AnalysisModeReliability>Recommended</AnalysisModeReliability>
77
<AnalysisModePerformance>Recommended</AnalysisModePerformance>
88
<AnalysisModeUsage>Recommended</AnalysisModeUsage>
9+
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
910
<CodeAnalysisRuleSet>$(MSBuildProjectDirectory)/../../Common.ruleset</CodeAnalysisRuleSet>
1011
<ContinuousIntegrationBuild Condition=" '$(GITLAB_CI)' != '' Or '$(APPVEYOR)' != '' ">true</ContinuousIntegrationBuild>
1112
<EnableNETAnalyzers>true</EnableNETAnalyzers>

src/BizHawk.BizInvoke/BizExvoker.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ public DelegateStorage(Type type)
6767

6868
foreach (var a in methods)
6969
{
70-
MethodBuilder unused;
71-
var delegateType = BizInvokeUtilities.CreateDelegateType(a.Info, a.Attr!.CallingConvention, typeBuilder, out unused).CreateType()!;
70+
var delegateType = BizInvokeUtilities.CreateDelegateType(a.Info, a.Attr!.CallingConvention, typeBuilder, out _).CreateType()!;
7271
DelegateTypes.Add(new StoredDelegateInfo(a.Info, delegateType, a.Attr.EntryPoint ?? a.Info.Name));
7372
}
7473
StorageType = typeBuilder.CreateType()!;

src/BizHawk.BizInvoke/CallingConventionAdapter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static WaterboxAdapter()
167167
{
168168
WaterboxWrapper = OSTailoredCode.IsUnixHost
169169
? new NativeConvention()
170-
: (ICallingConventionAdapter)new MsHostSysVGuest();
170+
: new MsHostSysVGuest();
171171
}
172172

173173
private readonly Dictionary<Delegate, int>? _slots;
@@ -247,7 +247,7 @@ static MsHostSysVGuest()
247247
public MsHostSysVGuest()
248248
{
249249
const int size = 4 * 1024 * 1024;
250-
_memory = new((ulong)size);
250+
_memory = new(size);
251251
_refs = new WeakReference[size / BlockSize];
252252
}
253253

src/BizHawk.BizInvoke/MemoryBlock.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using System.IO;
3-
using System.Linq;
4-
using System.Runtime.InteropServices;
53
using BizHawk.Common;
64

75
namespace BizHawk.BizInvoke
@@ -19,7 +17,7 @@ public MemoryBlock(ulong size)
1917
Size = WaterboxUtils.AlignUp(size);
2018

2119
_pal = OSTailoredCode.IsUnixHost
22-
? (IMemoryBlockPal)new MemoryBlockLinuxPal(Size)
20+
? new MemoryBlockLinuxPal(Size)
2321
: new MemoryBlockWindowsPal(Size);
2422
Start = _pal!.Start;
2523
EndExclusive = Start + Size;

src/BizHawk.BizInvoke/MemoryViewStream.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.IO;
3-
using System.Runtime.InteropServices;
43
using BizHawk.Common;
54

65
namespace BizHawk.BizInvoke

src/BizHawk.BizInvoke/POSIXLibC.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Runtime.InteropServices;
33

4-
using static BizHawk.BizInvoke.MemoryBlock;
5-
64
namespace BizHawk.BizInvoke
75
{
86
public static class POSIXLibC

src/BizHawk.Bizware.BizwareGL/BitmapBuffer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ private static int NextHigher(int k)
527527
k--;
528528
for (var i = 1; i < 32; i <<= 1)
529529
{
530-
k = k | k >> i;
530+
k |= k >> i;
531531
}
532532

533533
var candidate = k + 1;

src/BizHawk.Bizware.Graphics/OpenGL/SDL2OpenGLContext.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22

3-
using Silk.NET.OpenGL.Legacy;
4-
53
using static SDL2.SDL;
64

75
namespace BizHawk.Bizware.Graphics

src/BizHawk.Client.Common/Api/Classes/EmuClientApi.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Drawing;
33
using System.IO;
4-
5-
using BizHawk.Common;
64
using BizHawk.Emulation.Common;
75

86
namespace BizHawk.Client.Common

src/BizHawk.Client.Common/Api/Classes/JoypadApi.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void Set(IReadOnlyDictionary<string, bool> buttons, int? controller = nul
5858
// ALL button names with P{controller} prefixes
5959
foreach (var button in _inputManager.ActiveController.ToBoolButtonNameList(controller))
6060
{
61-
Set(button, buttons.TryGetValue(button, out var state) ? state : (bool?) null, controller);
61+
Set(button, buttons.TryGetValue(button, out var state) ? state : null, controller);
6262
}
6363
}
6464

src/BizHawk.Client.Common/Api/Classes/MemoryApi.cs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Linq;
44

55
using BizHawk.Common;
6-
using BizHawk.Common.BufferExtensions;
76
using BizHawk.Emulation.Common;
87

98
namespace BizHawk.Client.Common

src/BizHawk.Client.Common/Api/HttpCommunication.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#pragma warning disable CA2007
2-
31
using System;
42
using System.Collections.Generic;
53
using System.Net.Http;

src/BizHawk.Client.Common/DisplayManager/DisplayManagerBase.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,6 @@ private FilterProgram UpdateSourceInternal(JobInfo job)
763763
}
764764

765765
var videoProvider = job.VideoProvider;
766-
var glTextureProvider = videoProvider as IGLTextureProvider;
767766
var simulate = job.Simulate;
768767
var chainOutsize = job.ChainOutsize;
769768

@@ -824,7 +823,7 @@ private FilterProgram UpdateSourceInternal(JobInfo job)
824823
Texture2d videoTexture = null;
825824
if (!simulate)
826825
{
827-
if (glTextureProvider != null && _gl.DispMethodEnum == EDispMethod.OpenGL)
826+
if (videoProvider is IGLTextureProvider glTextureProvider && _gl.DispMethodEnum == EDispMethod.OpenGL)
828827
{
829828
// FYI: this is a million years from happening on n64, since it's all geriatric non-FBO code
830829
videoTexture = _gl.WrapGLTexture2d(new(glTextureProvider.GetGLTexture()), bufferWidth, bufferHeight);

src/BizHawk.Client.Common/DisplayManager/IBlitter.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System.Drawing;
22

3-
using BizHawk.Bizware.BizwareGL;
4-
53
namespace BizHawk.Client.Common
64
{
75
/// <summary>

src/BizHawk.Client.Common/DisplayManager/OSDManager.cs

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using System.Text;
44
using System.Drawing;
55
using System.Collections.Generic;
6-
7-
using BizHawk.Bizware.BizwareGL;
86
using BizHawk.Emulation.Common;
97

108
namespace BizHawk.Client.Common

src/BizHawk.Client.Common/lua/CommonLibs/SQLiteLuaLibrary.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
4-
using System.Linq;
54

65
// ReSharper disable UnusedMember.Global
76
namespace BizHawk.Client.Common

src/BizHawk.Client.Common/lua/LuaHelperLibs/NESLuaLibrary.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void SetDisplayBackground(bool show)
143143
nhs.DispBackground = show;
144144
Settings = nhs;
145145
break;
146-
case QuickNES.QuickNESSettings _:
146+
case QuickNES.QuickNESSettings:
147147
return;
148148
default:
149149
throw new InvalidOperationException();
@@ -209,7 +209,7 @@ public void SetScanlines(int top, int bottom, bool pal = false)
209209

210210
Settings = nhs;
211211
break;
212-
case QuickNES.QuickNESSettings _:
212+
case QuickNES.QuickNESSettings:
213213
return;
214214
default:
215215
throw new InvalidOperationException();

src/BizHawk.Client.Common/movie/tasproj/StateDictionary.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4-
using System.Text;
54
using System.IO;
65
using BizHawk.Common;
76

src/BizHawk.Client.Common/movie/tasproj/TasLagLog.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public bool? this[int frame]
1515
get
1616
{
1717
var result = _lagLog.TryGetValue(frame, out var lag);
18-
return result ? (bool?)lag : null;
18+
return result ? lag : null;
1919
}
2020

2121
set

src/BizHawk.Client.Common/rewind/ZeldaWinder.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.IO;
3-
using System.Runtime.InteropServices;
43
using System.Threading.Tasks;
54
using BizHawk.Common;
65
using BizHawk.Emulation.Common;

src/BizHawk.Client.Common/tools/Cheat.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public Cheat(Cheat cheat)
6767

6868
public long? Address => _watch.Address;
6969

70-
public int? Value => IsSeparator ? (int?)null : _val;
70+
public int? Value => IsSeparator ? null : _val;
7171

72-
public bool? BigEndian => IsSeparator ? (bool?)null : _watch.BigEndian;
72+
public bool? BigEndian => IsSeparator ? null : _watch.BigEndian;
7373

7474
public int? Compare => _compare.HasValue && !IsSeparator ? _compare : null;
7575

@@ -304,7 +304,7 @@ public override int GetHashCode()
304304
public static bool operator ==(Cheat a, Cheat b)
305305
{
306306
// If one is null, but not both, return false.
307-
if ((object)a == null || (object)b == null)
307+
if (a is null || b is null)
308308
{
309309
return false;
310310
}
@@ -320,7 +320,7 @@ public override int GetHashCode()
320320
public static bool operator ==(Cheat a, Watch b)
321321
{
322322
// If one is null, but not both, return false.
323-
if ((object)a == null || (object)b == null)
323+
if (a is null || b is null)
324324
{
325325
return false;
326326
}

src/BizHawk.Client.Common/tools/Watch/ByteWatch.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public string FormatValue(byte val)
141141
/// Get a string representation of difference
142142
/// between current value and the previous one
143143
/// </summary>
144-
public override string Diff => $"{_value - (short)_previous:+#;-#;0}";
144+
public override string Diff => $"{_value - _previous:+#;-#;0}";
145145

146146
/// <summary>
147147
/// Returns true if the Watch is valid, false otherwise

0 commit comments

Comments
 (0)