Enable MA0031 and fix noncompliance

"Optimize Enumerable.Count() usage"
This commit is contained in:
YoshiRulz 2022-07-21 03:46:59 +10:00
parent b1ad34839a
commit ee11385f10
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
14 changed files with 61 additions and 75 deletions

View File

@ -204,7 +204,7 @@
<Rule Id="MA0030" Action="Error" />
<!-- Optimize Enumerable.Count() usage -->
<Rule Id="MA0031" Action="Hidden" />
<Rule Id="MA0031" Action="Error" />
<!-- Use an overload with a CancellationToken argument -->
<Rule Id="MA0032" Action="Hidden" />

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using BizHawk.Emulation.Common;
@ -260,12 +261,7 @@ namespace BizHawk.Client.Common
public void InsertEmptyFrame(int frame, int count = 1)
{
#pragma warning disable CA1829 //TODO check StreamStringLog.Count is counting the same things as its enumerator
if (frame > Log.Count())
{
frame = Log.Count();
}
#pragma warning restore CA1829
frame = Math.Min(frame, Log.Count);
var lg = LogGeneratorInstance(Session.MovieController);
Log.InsertRange(frame, Enumerable.Repeat(lg.EmptyEntry, count).ToList());

View File

@ -589,7 +589,7 @@ namespace BizHawk.Client.EmuHawk
public void ToggleSelectAll()
{
if (SelectedRows.Count() == RowCount) DeselectAll();
if (SelectedRows.CountIsExactly(RowCount)) DeselectAll();
else SelectAll();
}

View File

@ -10,6 +10,7 @@ using BizHawk.Emulation.Common;
using BizHawk.Client.Common;
using BizHawk.Client.EmuHawk.Properties;
using BizHawk.Client.EmuHawk.ToolExtensions;
using BizHawk.Common.CollectionExtensions;
namespace BizHawk.Client.EmuHawk
{
@ -607,14 +608,12 @@ namespace BizHawk.Client.EmuHawk
{
Tools.Load<HexEditor>();
if (selected.Select(x => x.Domain).Distinct().Count() > 1)
{
ViewInHexEditor(selected[0].Domain, new List<long> { selected.First().Address ?? 0 }, selected.First().Size);
}
else
{
ViewInHexEditor(selected.First().Domain, selected.Select(x => x.Address ?? 0), selected.First().Size);
}
ViewInHexEditor(
selected[0].Domain,
selected.Select(static x => x.Domain).Distinct().CountIsAtLeast(2)
? new[] { selected[0].Address ?? 0 }
: selected.Select(static x => x.Address ?? 0),
selected[0].Size);
}
}

View File

@ -202,13 +202,9 @@ namespace BizHawk.Client.EmuHawk
private void UpdateBreakpointRemoveButton()
{
ToggleButton.Enabled =
RemoveBreakpointButton.Enabled =
EditableItems.Any();
DuplicateBreakpointButton.Enabled =
EditBreakpointButton.Enabled =
EditableItems.Count() == 1;
var editableCount = EditableItems.Count();
ToggleButton.Enabled = RemoveBreakpointButton.Enabled = editableCount > 0;
DuplicateBreakpointButton.Enabled = EditBreakpointButton.Enabled = editableCount == 1;
}
private void BreakpointView_SelectedIndexChanged(object sender, EventArgs e)

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk
@ -52,7 +53,7 @@ namespace BizHawk.Client.EmuHawk
{
try
{
if (CanSetCpu && Disassembler.AvailableCpus.Count() > 1)
if (CanSetCpu && Disassembler.AvailableCpus.CountIsAtLeast(2))
{
var c = new ComboBox
{

View File

@ -8,6 +8,7 @@ using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Emulation.Common;
using BizHawk.Client.EmuHawk.Properties;
using BizHawk.Common.CollectionExtensions;
namespace BizHawk.Client.EmuHawk
{
@ -225,10 +226,10 @@ namespace BizHawk.Client.EmuHawk
RemoveBranchContextMenuItem.Enabled = SelectedBranch != null;
UpdateBranchContextMenuItem.Enabled =
LoadBranchContextMenuItem.Enabled =
EditBranchTextContextMenuItem.Enabled =
JumpToBranchContextMenuItem.Enabled =
BranchView.SelectedRows.Count() == 1;
LoadBranchContextMenuItem.Enabled =
EditBranchTextContextMenuItem.Enabled =
JumpToBranchContextMenuItem.Enabled =
BranchView.SelectedRows.CountIsExactly(1);
}
private void AddBranchToolStripMenuItem_Click(object sender, EventArgs e)
@ -506,9 +507,9 @@ namespace BizHawk.Client.EmuHawk
private void UpdateButtons()
{
UpdateBranchButton.Enabled =
LoadBranchButton.Enabled =
JumpToBranchButton.Enabled =
BranchView.SelectedRows.Count() == 1;
LoadBranchButton.Enabled =
JumpToBranchButton.Enabled =
BranchView.SelectedRows.CountIsExactly(1);
}
private void Select(int index, bool value)

View File

@ -7,6 +7,7 @@ using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Client.EmuHawk.ToolExtensions;
using BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk
@ -739,7 +740,8 @@ namespace BizHawk.Client.EmuHawk
private void SetMarkersMenuItem_Click(object sender, EventArgs e)
{
if (TasView.SelectedRows.Count() > 50)
var selectedRows = TasView.SelectedRows.ToList();
if (selectedRows.Count > 50)
{
var result = DialogController.ShowMessageBox2("Are you sure you want to add more than 50 markers?", "Add markers", EMsgBoxIcon.Question, useOKCancel: true);
if (!result)
@ -748,7 +750,7 @@ namespace BizHawk.Client.EmuHawk
}
}
foreach (var index in TasView.SelectedRows)
foreach (var index in selectedRows)
{
MarkerControl.AddMarker(index, false);
}
@ -1403,13 +1405,14 @@ namespace BizHawk.Client.EmuHawk
(Clipboard.GetDataObject()?.GetDataPresent(DataFormats.StringFormat) ?? false)
&& TasView.AnyRowsSelected;
var selectionIsSingleRow = TasView.SelectedRows.CountIsExactly(1);
StartNewProjectFromNowMenuItem.Visible =
TasView.SelectedRows.Count() == 1
selectionIsSingleRow
&& TasView.IsRowSelected(Emulator.Frame)
&& !CurrentTasMovie.StartsFromSaveRam;
StartANewProjectFromSaveRamMenuItem.Visible =
TasView.SelectedRows.Count() == 1
selectionIsSingleRow
&& SaveRamEmulator != null
&& !CurrentTasMovie.StartsFromSavestate;

View File

@ -52,11 +52,8 @@ namespace BizHawk.Client.EmuHawk
if (_watchList.Count > 1)
{
bool hasMixedSizes = _watchList.Select(x => x.Size).Distinct().Count() > 1;
bool hasMixedTypes = _watchList.Select(x => x.Type).Distinct().Count() > 1;
bool hasMixedEndian = _watchList.Select(x => x.BigEndian).Distinct().Count() > 1;
if (hasMixedSizes || hasMixedTypes || hasMixedEndian)
var first = _watchList[0];
if (_watchList.Skip(1).Any(watch => watch.Size != first.Size || watch.Type != first.Type || watch.BigEndian != first.BigEndian))
{
UnSupportedConfiguration();
}

View File

@ -12,6 +12,7 @@ using BizHawk.Emulation.Common;
using BizHawk.Client.Common;
using BizHawk.Client.EmuHawk.Properties;
using BizHawk.Client.EmuHawk.ToolExtensions;
using BizHawk.Common.CollectionExtensions;
namespace BizHawk.Client.EmuHawk
{
@ -437,9 +438,10 @@ namespace BizHawk.Client.EmuHawk
{
Changes();
for (int i = 0; i < SelectedSeparators.Count(); i++)
var selection = SelectedSeparators.ToList();
for (var i = 0; i < selection.Count; i++)
{
var sep = SelectedSeparators.ToList()[i];
var sep = selection[i];
sep.Notes = inputPrompt.PromptText;
_watches[indexes[i]] = sep;
}
@ -1103,7 +1105,7 @@ namespace BizHawk.Client.EmuHawk
UnfreezeAllContextMenuItem.Visible = MainForm.CheatList.AnyActive;
ViewInHexEditorContextMenuItem.Visible = SelectedWatches.Count() == 1;
ViewInHexEditorContextMenuItem.Visible = SelectedWatches.CountIsExactly(1);
newToolStripMenuItem.Visible = !WatchListView.AnyRowsSelected;
}
@ -1119,15 +1121,12 @@ namespace BizHawk.Client.EmuHawk
if (selected.Any())
{
Tools.Load<HexEditor>();
if (selected.Select(x => x.Domain).Distinct().Count() > 1)
{
ViewInHexEditor(selected[0].Domain, new List<long> { selected.First().Address }, selected.First().Size);
}
else
{
ViewInHexEditor(selected.First().Domain, selected.Select(x => x.Address), selected.First().Size);
}
ViewInHexEditor(
selected[0].Domain,
selected.Select(static x => x.Domain).Distinct().CountIsAtLeast(2)
? new[] { selected[0].Address }
: selected.Select(static x => x.Address),
selected[0].Size);
}
}

View File

@ -5,6 +5,8 @@ using System.Linq;
using System.Windows.Forms;
using BizHawk.Client.Common;
using BizHawk.Common.CollectionExtensions;
using Emu = BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk
@ -76,7 +78,7 @@ namespace BizHawk.Client.EmuHawk
BigEndianCheckBox.ThreeState = true;
if (Watches.Select(s => s.Size).Distinct().Count() > 1)
if (Watches.Select(static s => s.Size).Distinct().CountIsAtLeast(2))
{
DisplayTypeDropDown.Enabled = false;
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
@ -120,6 +121,16 @@ namespace BizHawk.Common.CollectionExtensions
foreach (var item in collection) list.Add(item);
}
public static bool CountIsAtLeast<T>(this IEnumerable<T> collection, int n)
=> collection is ICollection countable
? countable.Count >= n
: collection.Skip(n - 1).Any();
public static bool CountIsExactly<T>(this IEnumerable<T> collection, int n)
=> collection is ICollection countable
? countable.Count == n
: collection.Take(n + 1).Count() == n;
/// <inheritdoc cref="IList{T}.IndexOf"/>
/// <remarks>
/// (This is an extension method which reimplements <see cref="IList{T}.IndexOf"/> for other <see cref="IReadOnlyList{T}">collections</see>.

View File

@ -90,13 +90,7 @@ namespace BizHawk.Emulation.Cores.Computers.AmstradCPC
public void DisableSource(ISoundProvider source)
{
var sp = SoundProviders.Where(a => a.SoundProvider == source);
if (sp.Count() == 1)
SoundProviders.Remove(sp.First());
else if (sp.Count() > 1)
foreach (var s in sp)
SoundProviders.Remove(s);
SoundProviders.RemoveAll(a => a.SoundProvider == source);
EqualizeVolumes();
}

View File

@ -109,20 +109,7 @@ namespace BizHawk.Emulation.Cores.Components
/// </summary>
public void UnPinSource(ISoundProvider source)
{
var sp = _soundProviders.Where(a => a.SoundProvider == source);
if (sp.Count() == 1)
{
_soundProviders.Remove(sp.First());
}
else if (sp.Count() > 1)
{
foreach (var s in sp)
{
_soundProviders.Remove(s);
}
}
_soundProviders.RemoveAll(a => a.SoundProvider == source);
EqualizeVolumes();
}