From 26b6a1c4a9a598b27be659455c518135e4b307a4 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Thu, 21 Jul 2022 00:30:17 +1000 Subject: [PATCH] Enable MA0020 and fix noncompliance "Use direct methods instead of LINQ methods" --- Common.ruleset | 2 +- .../config/PathEntryCollection.cs | 11 ++----- .../lua/LuaFunctionList.cs | 4 +-- src/BizHawk.Client.Common/tools/CheatList.cs | 5 ++-- src/BizHawk.Client.EmuHawk/AVOut/NutMuxer.cs | 5 ++-- .../CustomControls/InputWidget.cs | 4 +-- .../config/FileExtensionPreferencesPicker.cs | 11 ++----- .../tools/Lua/Libraries/FormsLuaLibrary.cs | 6 ++-- .../tools/ToolManager.cs | 29 +++++-------------- .../tools/Watch/RamSearch.cs | 6 ++-- src/BizHawk.Emulation.Common/SystemLookup.cs | 4 +-- .../AmstradCPC/Hardware/Disk/NECUPD765.FDC.cs | 7 ++--- .../Hardware/Disk/NECUPD765.FDC.cs | 7 ++--- .../Media/Disk/IPFFormat/IPFFloppyDisk.cs | 2 +- .../Waterbox/NymaCore.Controller.cs | 7 ++--- .../DiscFormats/Blobs/Blob_WaveFile.cs | 2 +- 16 files changed, 38 insertions(+), 74 deletions(-) diff --git a/Common.ruleset b/Common.ruleset index 60d36c96ad..27718bbaf4 100644 --- a/Common.ruleset +++ b/Common.ruleset @@ -171,7 +171,7 @@ - + diff --git a/src/BizHawk.Client.Common/config/PathEntryCollection.cs b/src/BizHawk.Client.Common/config/PathEntryCollection.cs index 9760ae2185..77d7f5aee6 100644 --- a/src/BizHawk.Client.Common/config/PathEntryCollection.cs +++ b/src/BizHawk.Client.Common/config/PathEntryCollection.cs @@ -120,9 +120,8 @@ namespace BizHawk.Client.Common public IEnumerator GetEnumerator() => Paths.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - public PathEntry this[string system, string type] => - Paths.FirstOrDefault(p => p.IsSystem(system) && p.Type == type) - ?? TryGetDebugPath(system, type); + public PathEntry this[string system, string type] + => Paths.Find(p => p.IsSystem(system) && p.Type == type) ?? TryGetDebugPath(system, type); private PathEntry TryGetDebugPath(string system, string type) { @@ -143,11 +142,7 @@ namespace BizHawk.Client.Common // Add missing entries foreach (var defaultPath in Defaults.Value) { - var path = Paths.FirstOrDefault(p => p.System == defaultPath.System && p.Type == defaultPath.Type); - if (path == null) - { - Paths.Add(defaultPath); - } + if (!Paths.Any(p => p.System == defaultPath.System && p.Type == defaultPath.Type)) Paths.Add(defaultPath); } var entriesToRemove = new List(); diff --git a/src/BizHawk.Client.Common/lua/LuaFunctionList.cs b/src/BizHawk.Client.Common/lua/LuaFunctionList.cs index d5b9bc8a1c..2eb3f409db 100644 --- a/src/BizHawk.Client.Common/lua/LuaFunctionList.cs +++ b/src/BizHawk.Client.Common/lua/LuaFunctionList.cs @@ -15,8 +15,8 @@ namespace BizHawk.Client.Common public LuaFunctionList(Action onChanged) => Changed = onChanged; - public NamedLuaFunction this[string guid] => - _functions.FirstOrDefault(nlf => nlf.Guid.ToString() == guid); + public NamedLuaFunction this[string guid] + => _functions.Find(nlf => nlf.Guid.ToString() == guid); public void Add(NamedLuaFunction nlf) { diff --git a/src/BizHawk.Client.Common/tools/CheatList.cs b/src/BizHawk.Client.Common/tools/CheatList.cs index 7fa9a2300d..f20edda1da 100644 --- a/src/BizHawk.Client.Common/tools/CheatList.cs +++ b/src/BizHawk.Client.Common/tools/CheatList.cs @@ -68,9 +68,8 @@ namespace BizHawk.Client.Common public Cheat this[int index] => _cheatList[index]; - public Cheat this[MemoryDomain domain, long address] => - _cheatList.FirstOrDefault(cheat => cheat.Domain == domain && cheat.Address == address); - + public Cheat this[MemoryDomain domain, long address] + => _cheatList.Find(cheat => cheat.Domain == domain && cheat.Address == address); public IEnumerator GetEnumerator() => _cheatList.GetEnumerator(); diff --git a/src/BizHawk.Client.EmuHawk/AVOut/NutMuxer.cs b/src/BizHawk.Client.EmuHawk/AVOut/NutMuxer.cs index 555552b1e0..66a65e220c 100644 --- a/src/BizHawk.Client.EmuHawk/AVOut/NutMuxer.cs +++ b/src/BizHawk.Client.EmuHawk/AVOut/NutMuxer.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Text; using System.IO; using System.Numerics; @@ -28,14 +27,14 @@ namespace BizHawk.Client.EmuHawk _capacity = capacity; } - private T[] GetBufferInternal(int length, bool zerofill, Func criteria) + private T[] GetBufferInternal(int length, bool zerofill, Predicate criteria) { if (_inUse.Count == _capacity) { throw new InvalidOperationException(); } - T[] candidate = _available.FirstOrDefault(criteria); + var candidate = _available.Find(criteria); if (candidate == null) { if (_available.Count + _inUse.Count == _capacity) diff --git a/src/BizHawk.Client.EmuHawk/CustomControls/InputWidget.cs b/src/BizHawk.Client.EmuHawk/CustomControls/InputWidget.cs index f2381c2117..47ba9d8d2d 100644 --- a/src/BizHawk.Client.EmuHawk/CustomControls/InputWidget.cs +++ b/src/BizHawk.Client.EmuHawk/CustomControls/InputWidget.cs @@ -171,9 +171,7 @@ namespace BizHawk.Client.EmuHawk } private bool IsDuplicate(string binding) - { - return _bindings.FirstOrDefault(x => x == binding) != null; - } + => _bindings.Contains(binding); protected override void OnKeyUp(KeyEventArgs e) { diff --git a/src/BizHawk.Client.EmuHawk/config/FileExtensionPreferencesPicker.cs b/src/BizHawk.Client.EmuHawk/config/FileExtensionPreferencesPicker.cs index 9af438da62..e35051e763 100644 --- a/src/BizHawk.Client.EmuHawk/config/FileExtensionPreferencesPicker.cs +++ b/src/BizHawk.Client.EmuHawk/config/FileExtensionPreferencesPicker.cs @@ -53,15 +53,10 @@ namespace BizHawk.Client.EmuHawk var selectedSystemId = _preferredPlatformsForExtensions[FileExtension]; if (!string.IsNullOrEmpty(selectedSystemId)) { - var selectedSystem = _availableSystems.FirstOrDefault(s => s.SystemId == selectedSystemId); - - var selectedItem = PlatformDropdown.Items - .OfType() - .FirstOrDefault(item => item == (selectedSystem?.FullName ?? "")); - - if (selectedItem != null) + var selectedSystem = _availableSystems.Find(s => s.SystemId == selectedSystemId)?.FullName ?? string.Empty; + if (PlatformDropdown.Items.Contains(selectedSystem)) { - PlatformDropdown.SelectedItem = selectedItem; + PlatformDropdown.SelectedItem = selectedSystem; } else { diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs index c8ef03274f..6fc45531d7 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/FormsLuaLibrary.cs @@ -25,14 +25,14 @@ namespace BizHawk.Client.EmuHawk public void WindowClosed(IntPtr handle) { - var form = _luaForms.FirstOrDefault(form => form.Handle == handle); - if (form != null) _luaForms.Remove(form); + var i = _luaForms.FindIndex(form => form.Handle == handle); + if (i is not -1) _luaForms.RemoveAt(i); } private LuaWinform GetForm(int formHandle) { var ptr = new IntPtr(formHandle); - return _luaForms.FirstOrDefault(form => form.Handle == ptr); + return _luaForms.Find(form => form.Handle == ptr); } private static void SetLocation(Control control, int x, int y) diff --git a/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs index 2bb1593268..f108196f3a 100644 --- a/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs +++ b/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs @@ -455,21 +455,10 @@ namespace BizHawk.Client.EmuHawk /// /// Type of tool to check public bool IsLoaded() where T : IToolForm - { - var existingTool = _tools.FirstOrDefault(t => t is T); - if (existingTool != null) - { - return existingTool.IsActive; - } - - return false; - } + => _tools.OfType().FirstOrDefault()?.IsActive is true; public bool IsLoaded(Type toolType) - { - var existingTool = _tools.FirstOrDefault(t => t.GetType() == toolType); - return existingTool != null && existingTool.IsActive; - } + => _tools.Find(t => t.GetType() == toolType)?.IsActive is true; public bool IsOnScreen(Point topLeft) { @@ -511,8 +500,8 @@ namespace BizHawk.Client.EmuHawk /// Type of tool to update public void UpdateValues() where T : IToolForm { - var tool = _tools.FirstOrDefault(t => t is T); - if (tool != null && tool.IsActive) + var tool = _tools.OfType().FirstOrDefault(); + if (tool?.IsActive is true) { tool.UpdateValues(ToolFormUpdateType.General); } @@ -568,10 +557,7 @@ namespace BizHawk.Client.EmuHawk /// /// Type of tool to restart public void Restart() where T : IToolForm - { - var tool = _tools.FirstOrDefault(t => t is T); - tool?.Restart(); - } + => _tools.OfType().FirstOrDefault()?.Restart(); /// /// Runs AskSave on every tool dialog, false is returned if any tool returns false @@ -594,7 +580,7 @@ namespace BizHawk.Client.EmuHawk /// Type of tool to close public void Close() where T : IToolForm { - var tool = _tools.FirstOrDefault(t => t is T); + var tool = _tools.OfType().FirstOrDefault(); if (tool != null) { tool.Close(); @@ -604,8 +590,7 @@ namespace BizHawk.Client.EmuHawk public void Close(Type toolType) { - var tool = _tools.FirstOrDefault(toolType.IsInstanceOfType); - + var tool = _tools.Find(toolType.IsInstanceOfType); if (tool != null) { tool.Close(); diff --git a/src/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs b/src/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs index 8307dc2ef7..8a67b5c541 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Watch/RamSearch.cs @@ -346,8 +346,7 @@ namespace BizHawk.Client.EmuHawk .OrderBy(x => x.TabIndex) .ToList(); - var selected = radios.FirstOrDefault(x => x.Checked); - var index = radios.IndexOf(selected); + var index = radios.FindIndex(static x => x.Checked); if (reverse) { @@ -382,8 +381,7 @@ namespace BizHawk.Client.EmuHawk .OrderBy(x => x.TabIndex) .ToList(); - var selected = radios.FirstOrDefault(x => x.Checked); - var index = radios.IndexOf(selected); + var index = radios.FindIndex(static x => x.Checked); if (reverse) { diff --git a/src/BizHawk.Emulation.Common/SystemLookup.cs b/src/BizHawk.Emulation.Common/SystemLookup.cs index fc3597cde1..096b916003 100644 --- a/src/BizHawk.Emulation.Common/SystemLookup.cs +++ b/src/BizHawk.Emulation.Common/SystemLookup.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; namespace BizHawk.Emulation.Common { @@ -38,8 +37,7 @@ namespace BizHawk.Emulation.Common }; public SystemInfo this[string systemId] - => _systems.FirstOrDefault(s => s.SystemId == systemId) - ?? new SystemInfo("Unknown", "Unknown"); + => _systems.Find(s => s.SystemId == systemId) ?? new("Unknown", "Unknown"); public IEnumerable AllSystems => _systems; diff --git a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.FDC.cs b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.FDC.cs index e66b09f074..5bddd1f04b 100644 --- a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.FDC.cs +++ b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/Hardware/Disk/NECUPD765.FDC.cs @@ -2493,9 +2493,8 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC cmdByte = cByte; // lookup the command - var cmd = CommandList.FirstOrDefault(a => a.CommandCode == cmdByte); - - if (cmd == null) + var i = CommandList.FindIndex(a => a.CommandCode == cmdByte); + if (i is -1) { // no command found - use invalid CMDIndex = CommandList.Count - 1; @@ -2503,7 +2502,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC else { // valid command found - CMDIndex = CommandList.FindIndex(a => a.CommandCode == cmdByte); + CMDIndex = i; // check validity of command byte flags // if a flag is set but not valid for this command then it is invalid diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDC.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDC.cs index 0d92eea2dc..41fafc7bab 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDC.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Hardware/Disk/NECUPD765.FDC.cs @@ -2517,9 +2517,8 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum cmdByte = cByte; // lookup the command - var cmd = CommandList.FirstOrDefault(a => a.CommandCode == cmdByte); - - if (cmd == null) + var i = CommandList.FindIndex(a => a.CommandCode == cmdByte); + if (i is -1) { // no command found - use invalid CMDIndex = CommandList.Count - 1; @@ -2527,7 +2526,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum else { // valid command found - CMDIndex = CommandList.FindIndex(a => a.CommandCode == cmdByte); + CMDIndex = i; // check validity of command byte flags // if a flag is set but not valid for this command then it is invalid diff --git a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/IPFFormat/IPFFloppyDisk.cs b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/IPFFormat/IPFFloppyDisk.cs index 98c122544a..fa80538db6 100644 --- a/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/IPFFormat/IPFFloppyDisk.cs +++ b/src/BizHawk.Emulation.Cores/Computers/SinclairSpectrum/Media/Disk/IPFFormat/IPFFloppyDisk.cs @@ -62,7 +62,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum } // now process the blocks - var infoBlock = blocks.FirstOrDefault(a => a.RecordType == RecordHeaderType.INFO); + var infoBlock = blocks.Find(static a => a.RecordType == RecordHeaderType.INFO); var IMGEblocks = blocks.Where(a => a.RecordType == RecordHeaderType.IMGE).ToList(); var DATAblocks = blocks.Where(a => a.RecordType == RecordHeaderType.DATA); diff --git a/src/BizHawk.Emulation.Cores/Waterbox/NymaCore.Controller.cs b/src/BizHawk.Emulation.Cores/Waterbox/NymaCore.Controller.cs index 0e52b1d538..0805483d75 100644 --- a/src/BizHawk.Emulation.Cores/Waterbox/NymaCore.Controller.cs +++ b/src/BizHawk.Emulation.Cores/Waterbox/NymaCore.Controller.cs @@ -72,13 +72,12 @@ namespace BizHawk.Emulation.Cores.Waterbox var devices = portInfo.Devices; - var device = devices.FirstOrDefault(a => a.ShortName == deviceName); + var device = devices.Find(a => a.ShortName == deviceName); if (device == null) { Console.WriteLine($"Warn: unknown controller device {deviceName}"); - device = devices.FirstOrDefault(a => a.ShortName == portInfo.DefaultDeviceShortName); - if (device == null) - throw new InvalidOperationException($"Fail: unknown controller device {portInfo.DefaultDeviceShortName}"); + device = devices.Find(a => a.ShortName == portInfo.DefaultDeviceShortName) + ?? throw new InvalidOperationException($"Fail: unknown controller device {portInfo.DefaultDeviceShortName}"); } ActualPortData.Add(new PortResult diff --git a/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/Blob_WaveFile.cs b/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/Blob_WaveFile.cs index a446d1214d..c06e4a498f 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/Blob_WaveFile.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscFormats/Blobs/Blob_WaveFile.cs @@ -50,7 +50,7 @@ namespace BizHawk.Emulation.DiscSystem throw new Blob_WaveFile_Exception("Not a RIFF WAVE file"); } - if (!(rm.riff.subchunks.FirstOrDefault(chunk => chunk.tag == "fmt ") is RiffMaster.RiffSubchunk_fmt fmt)) + if (rm.riff.subchunks.Find(static chunk => chunk.tag == "fmt ") is not RiffMaster.RiffSubchunk_fmt fmt) { throw new Blob_WaveFile_Exception("Not a valid RIFF WAVE file (missing fmt chunk"); }