Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Establish basic style analyzer rules #3759

Merged
merged 9 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 101 additions & 13 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,107 @@ csharp_indent_switch_labels = true
csharp_indent_case_contents = true
csharp_indent_labels = one_less_than_current

# Globalization rules
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
# Style rules
# Can't be in .globalconfig because dotnet format doesn't respect that https://github.com/dotnet/format/issues/1643

dotnet_diagnostic.CA1305.severity = error
dotnet_diagnostic.CA2101.severity = suggestion
# Remove unnecessary cast
dotnet_diagnostic.IDE0004.severity = warning
# Remove unnecessary import
dotnet_diagnostic.IDE0005.severity = warning
# Use var instead of explicit type
dotnet_diagnostic.IDE0007.severity = suggestion
TiKevin83 marked this conversation as resolved.
Show resolved Hide resolved
# Use explicit type instead of var
dotnet_diagnostic.IDE0008.severity = suggestion
# Inline variable declaration
dotnet_diagnostic.IDE0018.severity = warning
# Use pattern matching to avoid as followed by a null check
dotnet_diagnostic.IDE0019.severity = warning
# Use pattern matching to avoid is check followed by a cast (with variable)
dotnet_diagnostic.IDE0020.severity = warning
# Use expression body for methods
dotnet_diagnostic.IDE0022.severity = suggestion
# Use expression body for properties
dotnet_diagnostic.IDE0025.severity = suggestion
# Use expression body for indexers
dotnet_diagnostic.IDE0026.severity = suggestion
# Use expression body for accessors
dotnet_diagnostic.IDE0027.severity = suggestion
# Null check can be simplified
dotnet_diagnostic.IDE0029.severity = warning
# Null check can be simplified
dotnet_diagnostic.IDE0030.severity = warning
# Use null propagation
dotnet_diagnostic.IDE0031.severity = warning
# Use auto property
dotnet_diagnostic.IDE0032.severity = suggestion
# Simplify default expression
dotnet_diagnostic.IDE0034.severity = suggestion
# Use pattern matching to avoid is check followed by a cast (without variable)
dotnet_diagnostic.IDE0038.severity = warning
# Use is null check
dotnet_diagnostic.IDE0041.severity = warning
# Deconstruct variable declaration
dotnet_diagnostic.IDE0042.severity = suggestion
# dotnet_diagnostic.IDE0049.severity = error # see SA1121
# Remove unused private member
dotnet_diagnostic.IDE0051.severity = suggestion
# Remove unread private member
dotnet_diagnostic.IDE0052.severity = silent # TODO: should be warning imo, but there's too much violation currently
# Use compound assignment
dotnet_diagnostic.IDE0054.severity = warning
# Use index operator
dotnet_diagnostic.IDE0056.severity = warning
# Use range operator
dotnet_diagnostic.IDE0057.severity = warning
# Use simple using statement
dotnet_diagnostic.IDE0063.severity = suggestion
# Make struct fields writable
dotnet_diagnostic.IDE0064.severity = error
# using directive placement
dotnet_diagnostic.IDE0065.severity = error
# Use switch expression
dotnet_diagnostic.IDE0066.severity = suggestion
# Use System.HashCode.Combine
dotnet_diagnostic.IDE0070.severity = warning
# Simplify interpolation
dotnet_diagnostic.IDE0071.severity = suggestion
# Use coalesce compound assignment
dotnet_diagnostic.IDE0074.severity = suggestion
# Use pattern matching
dotnet_diagnostic.IDE0078.severity = suggestion
# Convert typeof to nameof
dotnet_diagnostic.IDE0082.severity = warning
# Use pattern matching (not operator)
dotnet_diagnostic.IDE0083.severity = warning
# Simplify new expression
dotnet_diagnostic.IDE0090.severity = suggestion
# Remove unnecessary equality operator
dotnet_diagnostic.IDE0100.severity = warning
# Remove unnecessary discard
dotnet_diagnostic.IDE0110.severity = warning
# Simplify LINQ expression
dotnet_diagnostic.IDE0120.severity = error
# Namespace does not match folder structure
dotnet_diagnostic.IDE0130.severity = silent # should be warning imo
# Use tuple to swap values
dotnet_diagnostic.IDE0180.severity = suggestion
# Use UTF-8 string literal
dotnet_diagnostic.IDE0230.severity = warning
# Nullable directive is redundant
dotnet_diagnostic.IDE0240.severity = warning
# Nullable directive is unnecessary
dotnet_diagnostic.IDE0241.severity = warning
# Struct can be made 'readonly'
dotnet_diagnostic.IDE0250.severity = suggestion
# Use pattern matching
dotnet_diagnostic.IDE0260.severity = suggestion
# Use nameof
dotnet_diagnostic.IDE0280.severity = error

# Performance rules
dotnet_code_quality.CA1826.exclude_ordefault_methods = true
csharp_style_var_when_type_is_apparent = true
csharp_style_var_elsewhere = true

dotnet_diagnostic.CA1805.severity = silent
dotnet_diagnostic.CA1822.severity = silent
dotnet_diagnostic.CA1838.severity = suggestion

# Usage rules
dotnet_diagnostic.CA1816.severity = none
dotnet_diagnostic.CA2201.severity = suggestion
csharp_style_expression_bodied_methods = when_on_single_line
csharp_style_expression_bodied_properties = when_on_single_line
csharp_style_expression_bodied_indexers = when_on_single_line
csharp_style_expression_bodied_accessors = when_on_single_line
18 changes: 18 additions & 0 deletions .globalconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
is_global = true

# Globalization rules
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

dotnet_diagnostic.CA1305.severity = error
dotnet_diagnostic.CA2101.severity = suggestion
TiKevin83 marked this conversation as resolved.
Show resolved Hide resolved

# Performance rules
dotnet_code_quality.CA1826.exclude_ordefault_methods = true

dotnet_diagnostic.CA1805.severity = silent
dotnet_diagnostic.CA1822.severity = silent
TiKevin83 marked this conversation as resolved.
Show resolved Hide resolved
dotnet_diagnostic.CA1838.severity = suggestion

# Usage rules
dotnet_diagnostic.CA1816.severity = none
TiKevin83 marked this conversation as resolved.
Show resolved Hide resolved
dotnet_diagnostic.CA2201.severity = suggestion
1 change: 1 addition & 0 deletions Common.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<AnalysisModeReliability>Recommended</AnalysisModeReliability>
<AnalysisModePerformance>Recommended</AnalysisModePerformance>
<AnalysisModeUsage>Recommended</AnalysisModeUsage>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<CodeAnalysisRuleSet>$(MSBuildProjectDirectory)/../../Common.ruleset</CodeAnalysisRuleSet>
<ContinuousIntegrationBuild Condition=" '$(GITLAB_CI)' != '' Or '$(APPVEYOR)' != '' ">true</ContinuousIntegrationBuild>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
Expand Down
3 changes: 1 addition & 2 deletions src/BizHawk.BizInvoke/BizExvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public DelegateStorage(Type type)

foreach (var a in methods)
{
MethodBuilder unused;
var delegateType = BizInvokeUtilities.CreateDelegateType(a.Info, a.Attr!.CallingConvention, typeBuilder, out unused).CreateType()!;
var delegateType = BizInvokeUtilities.CreateDelegateType(a.Info, a.Attr!.CallingConvention, typeBuilder, out _).CreateType()!;
DelegateTypes.Add(new StoredDelegateInfo(a.Info, delegateType, a.Attr.EntryPoint ?? a.Info.Name));
}
StorageType = typeBuilder.CreateType()!;
Expand Down
4 changes: 2 additions & 2 deletions src/BizHawk.BizInvoke/CallingConventionAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ static WaterboxAdapter()
{
WaterboxWrapper = OSTailoredCode.IsUnixHost
? new NativeConvention()
: (ICallingConventionAdapter)new MsHostSysVGuest();
: new MsHostSysVGuest();
}

private readonly Dictionary<Delegate, int>? _slots;
Expand Down Expand Up @@ -247,7 +247,7 @@ static MsHostSysVGuest()
public MsHostSysVGuest()
{
const int size = 4 * 1024 * 1024;
_memory = new((ulong)size);
_memory = new(size);
_refs = new WeakReference[size / BlockSize];
}

Expand Down
4 changes: 1 addition & 3 deletions src/BizHawk.BizInvoke/MemoryBlock.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using BizHawk.Common;

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

_pal = OSTailoredCode.IsUnixHost
? (IMemoryBlockPal)new MemoryBlockLinuxPal(Size)
? new MemoryBlockLinuxPal(Size)
: new MemoryBlockWindowsPal(Size);
Start = _pal!.Start;
EndExclusive = Start + Size;
Expand Down
1 change: 0 additions & 1 deletion src/BizHawk.BizInvoke/MemoryViewStream.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using BizHawk.Common;

namespace BizHawk.BizInvoke
Expand Down
2 changes: 0 additions & 2 deletions src/BizHawk.BizInvoke/POSIXLibC.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Runtime.InteropServices;

using static BizHawk.BizInvoke.MemoryBlock;

namespace BizHawk.BizInvoke
{
public static class POSIXLibC
Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Bizware.BizwareGL/BitmapBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ private static int NextHigher(int k)
k--;
for (var i = 1; i < 32; i <<= 1)
{
k = k | k >> i;
k |= k >> i;
}

var candidate = k + 1;
Expand Down
2 changes: 0 additions & 2 deletions src/BizHawk.Bizware.Graphics/OpenGL/SDL2OpenGLContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;

using Silk.NET.OpenGL.Legacy;

using static SDL2.SDL;

namespace BizHawk.Bizware.Graphics
Expand Down
2 changes: 0 additions & 2 deletions src/BizHawk.Client.Common/Api/Classes/EmuClientApi.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Drawing;
using System.IO;

using BizHawk.Common;
using BizHawk.Emulation.Common;

namespace BizHawk.Client.Common
Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Client.Common/Api/Classes/JoypadApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void Set(IReadOnlyDictionary<string, bool> buttons, int? controller = nul
// ALL button names with P{controller} prefixes
foreach (var button in _inputManager.ActiveController.ToBoolButtonNameList(controller))
{
Set(button, buttons.TryGetValue(button, out var state) ? state : (bool?) null, controller);
Set(button, buttons.TryGetValue(button, out var state) ? state : null, controller);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/BizHawk.Client.Common/Api/Classes/MemoryApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;

using BizHawk.Common;
using BizHawk.Common.BufferExtensions;
using BizHawk.Emulation.Common;

namespace BizHawk.Client.Common
Expand Down
2 changes: 0 additions & 2 deletions src/BizHawk.Client.Common/Api/HttpCommunication.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#pragma warning disable CA2007

using System;
using System.Collections.Generic;
using System.Net.Http;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,6 @@ private FilterProgram UpdateSourceInternal(JobInfo job)
}

var videoProvider = job.VideoProvider;
var glTextureProvider = videoProvider as IGLTextureProvider;
var simulate = job.Simulate;
var chainOutsize = job.ChainOutsize;

Expand Down Expand Up @@ -824,7 +823,7 @@ private FilterProgram UpdateSourceInternal(JobInfo job)
Texture2d videoTexture = null;
if (!simulate)
{
if (glTextureProvider != null && _gl.DispMethodEnum == EDispMethod.OpenGL)
if (videoProvider is IGLTextureProvider glTextureProvider && _gl.DispMethodEnum == EDispMethod.OpenGL)
{
// FYI: this is a million years from happening on n64, since it's all geriatric non-FBO code
videoTexture = _gl.WrapGLTexture2d(new(glTextureProvider.GetGLTexture()), bufferWidth, bufferHeight);
Expand Down
2 changes: 0 additions & 2 deletions src/BizHawk.Client.Common/DisplayManager/IBlitter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Drawing;

using BizHawk.Bizware.BizwareGL;

namespace BizHawk.Client.Common
{
/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions src/BizHawk.Client.Common/DisplayManager/OSDManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System.Text;
using System.Drawing;
using System.Collections.Generic;

using BizHawk.Bizware.BizwareGL;
using BizHawk.Emulation.Common;

namespace BizHawk.Client.Common
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

// ReSharper disable UnusedMember.Global
namespace BizHawk.Client.Common
Expand Down
4 changes: 2 additions & 2 deletions src/BizHawk.Client.Common/lua/LuaHelperLibs/NESLuaLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void SetDisplayBackground(bool show)
nhs.DispBackground = show;
Settings = nhs;
break;
case QuickNES.QuickNESSettings _:
case QuickNES.QuickNESSettings:
return;
default:
throw new InvalidOperationException();
Expand Down Expand Up @@ -209,7 +209,7 @@ public void SetScanlines(int top, int bottom, bool pal = false)

Settings = nhs;
break;
case QuickNES.QuickNESSettings _:
case QuickNES.QuickNESSettings:
return;
default:
throw new InvalidOperationException();
Expand Down
1 change: 0 additions & 1 deletion src/BizHawk.Client.Common/movie/tasproj/StateDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using BizHawk.Common;

Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Client.Common/movie/tasproj/TasLagLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public bool? this[int frame]
get
{
var result = _lagLog.TryGetValue(frame, out var lag);
return result ? (bool?)lag : null;
return result ? lag : null;
}

set
Expand Down
1 change: 0 additions & 1 deletion src/BizHawk.Client.Common/rewind/ZeldaWinder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using BizHawk.Common;
using BizHawk.Emulation.Common;
Expand Down
8 changes: 4 additions & 4 deletions src/BizHawk.Client.Common/tools/Cheat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public Cheat(Cheat cheat)

public long? Address => _watch.Address;

public int? Value => IsSeparator ? (int?)null : _val;
public int? Value => IsSeparator ? null : _val;

public bool? BigEndian => IsSeparator ? (bool?)null : _watch.BigEndian;
public bool? BigEndian => IsSeparator ? null : _watch.BigEndian;

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

Expand Down Expand Up @@ -304,7 +304,7 @@ public override int GetHashCode()
public static bool operator ==(Cheat a, Cheat b)
{
// If one is null, but not both, return false.
if ((object)a == null || (object)b == null)
if (a is null || b is null)
{
return false;
}
Expand All @@ -320,7 +320,7 @@ public override int GetHashCode()
public static bool operator ==(Cheat a, Watch b)
{
// If one is null, but not both, return false.
if ((object)a == null || (object)b == null)
if (a is null || b is null)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Client.Common/tools/Watch/ByteWatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public string FormatValue(byte val)
/// Get a string representation of difference
/// between current value and the previous one
/// </summary>
public override string Diff => $"{_value - (short)_previous:+#;-#;0}";
public override string Diff => $"{_value - _previous:+#;-#;0}";

/// <summary>
/// Returns true if the Watch is valid, false otherwise
Expand Down
Loading