Enable MA0020 and fix noncompliance

"Use direct methods instead of LINQ methods"
This commit is contained in:
YoshiRulz 2022-07-21 00:30:17 +10:00
parent 697c10e3e6
commit 26b6a1c4a9
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
16 changed files with 38 additions and 74 deletions

View File

@ -171,7 +171,7 @@
<Rule Id="MA0019" Action="Error" /> <Rule Id="MA0019" Action="Error" />
<!-- Use direct methods instead of LINQ methods --> <!-- Use direct methods instead of LINQ methods -->
<Rule Id="MA0020" Action="Hidden" /> <Rule Id="MA0020" Action="Error" />
<!-- Use StringComparer.GetHashCode instead of string.GetHashCode --> <!-- Use StringComparer.GetHashCode instead of string.GetHashCode -->
<Rule Id="MA0021" Action="Hidden" /> <Rule Id="MA0021" Action="Hidden" />

View File

@ -120,9 +120,8 @@ namespace BizHawk.Client.Common
public IEnumerator<PathEntry> GetEnumerator() => Paths.GetEnumerator(); public IEnumerator<PathEntry> GetEnumerator() => Paths.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public PathEntry this[string system, string type] => public PathEntry this[string system, string type]
Paths.FirstOrDefault(p => p.IsSystem(system) && p.Type == type) => Paths.Find(p => p.IsSystem(system) && p.Type == type) ?? TryGetDebugPath(system, type);
?? TryGetDebugPath(system, type);
private PathEntry TryGetDebugPath(string system, string type) private PathEntry TryGetDebugPath(string system, string type)
{ {
@ -143,11 +142,7 @@ namespace BizHawk.Client.Common
// Add missing entries // Add missing entries
foreach (var defaultPath in Defaults.Value) foreach (var defaultPath in Defaults.Value)
{ {
var path = Paths.FirstOrDefault(p => p.System == defaultPath.System && p.Type == defaultPath.Type); if (!Paths.Any(p => p.System == defaultPath.System && p.Type == defaultPath.Type)) Paths.Add(defaultPath);
if (path == null)
{
Paths.Add(defaultPath);
}
} }
var entriesToRemove = new List<PathEntry>(); var entriesToRemove = new List<PathEntry>();

View File

@ -15,8 +15,8 @@ namespace BizHawk.Client.Common
public LuaFunctionList(Action onChanged) => Changed = onChanged; public LuaFunctionList(Action onChanged) => Changed = onChanged;
public NamedLuaFunction this[string guid] => public NamedLuaFunction this[string guid]
_functions.FirstOrDefault(nlf => nlf.Guid.ToString() == guid); => _functions.Find(nlf => nlf.Guid.ToString() == guid);
public void Add(NamedLuaFunction nlf) public void Add(NamedLuaFunction nlf)
{ {

View File

@ -68,9 +68,8 @@ namespace BizHawk.Client.Common
public Cheat this[int index] => _cheatList[index]; public Cheat this[int index] => _cheatList[index];
public Cheat this[MemoryDomain domain, long address] => public Cheat this[MemoryDomain domain, long address]
_cheatList.FirstOrDefault(cheat => cheat.Domain == domain && cheat.Address == address); => _cheatList.Find(cheat => cheat.Domain == domain && cheat.Address == address);
public IEnumerator<Cheat> GetEnumerator() => _cheatList.GetEnumerator(); public IEnumerator<Cheat> GetEnumerator() => _cheatList.GetEnumerator();

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.IO; using System.IO;
using System.Numerics; using System.Numerics;
@ -28,14 +27,14 @@ namespace BizHawk.Client.EmuHawk
_capacity = capacity; _capacity = capacity;
} }
private T[] GetBufferInternal(int length, bool zerofill, Func<T[], bool> criteria) private T[] GetBufferInternal(int length, bool zerofill, Predicate<T[]> criteria)
{ {
if (_inUse.Count == _capacity) if (_inUse.Count == _capacity)
{ {
throw new InvalidOperationException(); throw new InvalidOperationException();
} }
T[] candidate = _available.FirstOrDefault(criteria); var candidate = _available.Find(criteria);
if (candidate == null) if (candidate == null)
{ {
if (_available.Count + _inUse.Count == _capacity) if (_available.Count + _inUse.Count == _capacity)

View File

@ -171,9 +171,7 @@ namespace BizHawk.Client.EmuHawk
} }
private bool IsDuplicate(string binding) private bool IsDuplicate(string binding)
{ => _bindings.Contains(binding);
return _bindings.FirstOrDefault(x => x == binding) != null;
}
protected override void OnKeyUp(KeyEventArgs e) protected override void OnKeyUp(KeyEventArgs e)
{ {

View File

@ -53,15 +53,10 @@ namespace BizHawk.Client.EmuHawk
var selectedSystemId = _preferredPlatformsForExtensions[FileExtension]; var selectedSystemId = _preferredPlatformsForExtensions[FileExtension];
if (!string.IsNullOrEmpty(selectedSystemId)) if (!string.IsNullOrEmpty(selectedSystemId))
{ {
var selectedSystem = _availableSystems.FirstOrDefault(s => s.SystemId == selectedSystemId); var selectedSystem = _availableSystems.Find(s => s.SystemId == selectedSystemId)?.FullName ?? string.Empty;
if (PlatformDropdown.Items.Contains(selectedSystem))
var selectedItem = PlatformDropdown.Items
.OfType<string>()
.FirstOrDefault(item => item == (selectedSystem?.FullName ?? ""));
if (selectedItem != null)
{ {
PlatformDropdown.SelectedItem = selectedItem; PlatformDropdown.SelectedItem = selectedSystem;
} }
else else
{ {

View File

@ -25,14 +25,14 @@ namespace BizHawk.Client.EmuHawk
public void WindowClosed(IntPtr handle) public void WindowClosed(IntPtr handle)
{ {
var form = _luaForms.FirstOrDefault(form => form.Handle == handle); var i = _luaForms.FindIndex(form => form.Handle == handle);
if (form != null) _luaForms.Remove(form); if (i is not -1) _luaForms.RemoveAt(i);
} }
private LuaWinform GetForm(int formHandle) private LuaWinform GetForm(int formHandle)
{ {
var ptr = new IntPtr(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) private static void SetLocation(Control control, int x, int y)

View File

@ -455,21 +455,10 @@ namespace BizHawk.Client.EmuHawk
/// </summary> /// </summary>
/// <typeparam name="T">Type of tool to check</typeparam> /// <typeparam name="T">Type of tool to check</typeparam>
public bool IsLoaded<T>() where T : IToolForm public bool IsLoaded<T>() where T : IToolForm
{ => _tools.OfType<T>().FirstOrDefault()?.IsActive is true;
var existingTool = _tools.FirstOrDefault(t => t is T);
if (existingTool != null)
{
return existingTool.IsActive;
}
return false;
}
public bool IsLoaded(Type toolType) public bool IsLoaded(Type toolType)
{ => _tools.Find(t => t.GetType() == toolType)?.IsActive is true;
var existingTool = _tools.FirstOrDefault(t => t.GetType() == toolType);
return existingTool != null && existingTool.IsActive;
}
public bool IsOnScreen(Point topLeft) public bool IsOnScreen(Point topLeft)
{ {
@ -511,8 +500,8 @@ namespace BizHawk.Client.EmuHawk
/// <typeparam name="T">Type of tool to update</typeparam> /// <typeparam name="T">Type of tool to update</typeparam>
public void UpdateValues<T>() where T : IToolForm public void UpdateValues<T>() where T : IToolForm
{ {
var tool = _tools.FirstOrDefault(t => t is T); var tool = _tools.OfType<T>().FirstOrDefault();
if (tool != null && tool.IsActive) if (tool?.IsActive is true)
{ {
tool.UpdateValues(ToolFormUpdateType.General); tool.UpdateValues(ToolFormUpdateType.General);
} }
@ -568,10 +557,7 @@ namespace BizHawk.Client.EmuHawk
/// </summary> /// </summary>
/// <typeparam name="T">Type of tool to restart</typeparam> /// <typeparam name="T">Type of tool to restart</typeparam>
public void Restart<T>() where T : IToolForm public void Restart<T>() where T : IToolForm
{ => _tools.OfType<T>().FirstOrDefault()?.Restart();
var tool = _tools.FirstOrDefault(t => t is T);
tool?.Restart();
}
/// <summary> /// <summary>
/// Runs AskSave on every tool dialog, false is returned if any tool returns false /// Runs AskSave on every tool dialog, false is returned if any tool returns false
@ -594,7 +580,7 @@ namespace BizHawk.Client.EmuHawk
/// <typeparam name="T">Type of tool to close</typeparam> /// <typeparam name="T">Type of tool to close</typeparam>
public void Close<T>() where T : IToolForm public void Close<T>() where T : IToolForm
{ {
var tool = _tools.FirstOrDefault(t => t is T); var tool = _tools.OfType<T>().FirstOrDefault();
if (tool != null) if (tool != null)
{ {
tool.Close(); tool.Close();
@ -604,8 +590,7 @@ namespace BizHawk.Client.EmuHawk
public void Close(Type toolType) public void Close(Type toolType)
{ {
var tool = _tools.FirstOrDefault(toolType.IsInstanceOfType); var tool = _tools.Find(toolType.IsInstanceOfType);
if (tool != null) if (tool != null)
{ {
tool.Close(); tool.Close();

View File

@ -346,8 +346,7 @@ namespace BizHawk.Client.EmuHawk
.OrderBy(x => x.TabIndex) .OrderBy(x => x.TabIndex)
.ToList(); .ToList();
var selected = radios.FirstOrDefault(x => x.Checked); var index = radios.FindIndex(static x => x.Checked);
var index = radios.IndexOf(selected);
if (reverse) if (reverse)
{ {
@ -382,8 +381,7 @@ namespace BizHawk.Client.EmuHawk
.OrderBy(x => x.TabIndex) .OrderBy(x => x.TabIndex)
.ToList(); .ToList();
var selected = radios.FirstOrDefault(x => x.Checked); var index = radios.FindIndex(static x => x.Checked);
var index = radios.IndexOf(selected);
if (reverse) if (reverse)
{ {

View File

@ -1,5 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace BizHawk.Emulation.Common namespace BizHawk.Emulation.Common
{ {
@ -38,8 +37,7 @@ namespace BizHawk.Emulation.Common
}; };
public SystemInfo this[string systemId] public SystemInfo this[string systemId]
=> _systems.FirstOrDefault(s => s.SystemId == systemId) => _systems.Find(s => s.SystemId == systemId) ?? new("Unknown", "Unknown");
?? new SystemInfo("Unknown", "Unknown");
public IEnumerable<SystemInfo> AllSystems => _systems; public IEnumerable<SystemInfo> AllSystems => _systems;

View File

@ -2493,9 +2493,8 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
cmdByte = cByte; cmdByte = cByte;
// lookup the command // lookup the command
var cmd = CommandList.FirstOrDefault(a => a.CommandCode == cmdByte); var i = CommandList.FindIndex(a => a.CommandCode == cmdByte);
if (i is -1)
if (cmd == null)
{ {
// no command found - use invalid // no command found - use invalid
CMDIndex = CommandList.Count - 1; CMDIndex = CommandList.Count - 1;
@ -2503,7 +2502,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
else else
{ {
// valid command found // valid command found
CMDIndex = CommandList.FindIndex(a => a.CommandCode == cmdByte); CMDIndex = i;
// check validity of command byte flags // check validity of command byte flags
// if a flag is set but not valid for this command then it is invalid // if a flag is set but not valid for this command then it is invalid

View File

@ -2517,9 +2517,8 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
cmdByte = cByte; cmdByte = cByte;
// lookup the command // lookup the command
var cmd = CommandList.FirstOrDefault(a => a.CommandCode == cmdByte); var i = CommandList.FindIndex(a => a.CommandCode == cmdByte);
if (i is -1)
if (cmd == null)
{ {
// no command found - use invalid // no command found - use invalid
CMDIndex = CommandList.Count - 1; CMDIndex = CommandList.Count - 1;
@ -2527,7 +2526,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
else else
{ {
// valid command found // valid command found
CMDIndex = CommandList.FindIndex(a => a.CommandCode == cmdByte); CMDIndex = i;
// check validity of command byte flags // check validity of command byte flags
// if a flag is set but not valid for this command then it is invalid // if a flag is set but not valid for this command then it is invalid

View File

@ -62,7 +62,7 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
} }
// now process the blocks // 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 IMGEblocks = blocks.Where(a => a.RecordType == RecordHeaderType.IMGE).ToList();
var DATAblocks = blocks.Where(a => a.RecordType == RecordHeaderType.DATA); var DATAblocks = blocks.Where(a => a.RecordType == RecordHeaderType.DATA);

View File

@ -72,13 +72,12 @@ namespace BizHawk.Emulation.Cores.Waterbox
var devices = portInfo.Devices; var devices = portInfo.Devices;
var device = devices.FirstOrDefault(a => a.ShortName == deviceName); var device = devices.Find(a => a.ShortName == deviceName);
if (device == null) if (device == null)
{ {
Console.WriteLine($"Warn: unknown controller device {deviceName}"); Console.WriteLine($"Warn: unknown controller device {deviceName}");
device = devices.FirstOrDefault(a => a.ShortName == portInfo.DefaultDeviceShortName); device = devices.Find(a => a.ShortName == portInfo.DefaultDeviceShortName)
if (device == null) ?? throw new InvalidOperationException($"Fail: unknown controller device {portInfo.DefaultDeviceShortName}");
throw new InvalidOperationException($"Fail: unknown controller device {portInfo.DefaultDeviceShortName}");
} }
ActualPortData.Add(new PortResult ActualPortData.Add(new PortResult

View File

@ -50,7 +50,7 @@ namespace BizHawk.Emulation.DiscSystem
throw new Blob_WaveFile_Exception("Not a RIFF WAVE file"); 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"); throw new Blob_WaveFile_Exception("Not a valid RIFF WAVE file (missing fmt chunk");
} }