From ee11385f10a04ef15c2d6ae1134de91c49b14068 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Thu, 21 Jul 2022 03:46:59 +1000 Subject: [PATCH] Enable MA0031 and fix noncompliance "Optimize Enumerable.Count() usage" --- Common.ruleset | 2 +- .../movie/tasproj/TasMovie.Editing.cs | 10 +++----- .../CustomControls/InputRoll/InputRoll.cs | 2 +- .../tools/Cheats/Cheats.cs | 15 ++++++------ .../tools/Debugger/BreakpointControl.cs | 10 +++----- .../tools/Debugger/GenericDebugger.cs | 3 ++- .../tools/TAStudio/BookmarksBranchesBox.cs | 15 ++++++------ .../tools/TAStudio/TAStudio.MenuItems.cs | 11 +++++---- .../tools/Watch/RamPoke.cs | 7 ++---- .../tools/Watch/RamWatch.cs | 23 +++++++++---------- .../tools/Watch/WatchEditor.cs | 4 +++- .../Extensions/CollectionExtensions.cs | 11 +++++++++ .../AmstradCPC/SoundProviderMixer.cs | 8 +------ .../Sound/SyncSoundMixer.cs | 15 +----------- 14 files changed, 61 insertions(+), 75 deletions(-) diff --git a/Common.ruleset b/Common.ruleset index 50c3acdcbb..cbbeb53ab7 100644 --- a/Common.ruleset +++ b/Common.ruleset @@ -204,7 +204,7 @@ - + diff --git a/src/BizHawk.Client.Common/movie/tasproj/TasMovie.Editing.cs b/src/BizHawk.Client.Common/movie/tasproj/TasMovie.Editing.cs index bceddd5b69..2e0de988ec 100644 --- a/src/BizHawk.Client.Common/movie/tasproj/TasMovie.Editing.cs +++ b/src/BizHawk.Client.Common/movie/tasproj/TasMovie.Editing.cs @@ -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()); diff --git a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs index 3d1e62268c..2beec192f7 100644 --- a/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs +++ b/src/BizHawk.Client.EmuHawk/CustomControls/InputRoll/InputRoll.cs @@ -589,7 +589,7 @@ namespace BizHawk.Client.EmuHawk public void ToggleSelectAll() { - if (SelectedRows.Count() == RowCount) DeselectAll(); + if (SelectedRows.CountIsExactly(RowCount)) DeselectAll(); else SelectAll(); } diff --git a/src/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs b/src/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs index 630ab95e19..20fa025e29 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Cheats/Cheats.cs @@ -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(); - if (selected.Select(x => x.Domain).Distinct().Count() > 1) - { - ViewInHexEditor(selected[0].Domain, new List { 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); } } diff --git a/src/BizHawk.Client.EmuHawk/tools/Debugger/BreakpointControl.cs b/src/BizHawk.Client.EmuHawk/tools/Debugger/BreakpointControl.cs index c79455e77c..a2af7d2755 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Debugger/BreakpointControl.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Debugger/BreakpointControl.cs @@ -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) diff --git a/src/BizHawk.Client.EmuHawk/tools/Debugger/GenericDebugger.cs b/src/BizHawk.Client.EmuHawk/tools/Debugger/GenericDebugger.cs index 58bc97af1b..9f428e430d 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Debugger/GenericDebugger.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Debugger/GenericDebugger.cs @@ -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 { diff --git a/src/BizHawk.Client.EmuHawk/tools/TAStudio/BookmarksBranchesBox.cs b/src/BizHawk.Client.EmuHawk/tools/TAStudio/BookmarksBranchesBox.cs index d524124174..81875fe42e 100644 --- a/src/BizHawk.Client.EmuHawk/tools/TAStudio/BookmarksBranchesBox.cs +++ b/src/BizHawk.Client.EmuHawk/tools/TAStudio/BookmarksBranchesBox.cs @@ -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) diff --git a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs index 485d7389d1..13bb02afae 100644 --- a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs +++ b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs @@ -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; diff --git a/src/BizHawk.Client.EmuHawk/tools/Watch/RamPoke.cs b/src/BizHawk.Client.EmuHawk/tools/Watch/RamPoke.cs index 17aebcf7dc..eebd9a4347 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Watch/RamPoke.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Watch/RamPoke.cs @@ -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(); } diff --git a/src/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs b/src/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs index deb44590a6..f7ca0f4cb4 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Watch/RamWatch.cs @@ -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(); - - if (selected.Select(x => x.Domain).Distinct().Count() > 1) - { - ViewInHexEditor(selected[0].Domain, new List { 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); } } diff --git a/src/BizHawk.Client.EmuHawk/tools/Watch/WatchEditor.cs b/src/BizHawk.Client.EmuHawk/tools/Watch/WatchEditor.cs index 92d478e4c1..327c1c4bdf 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Watch/WatchEditor.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Watch/WatchEditor.cs @@ -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; } diff --git a/src/BizHawk.Common/Extensions/CollectionExtensions.cs b/src/BizHawk.Common/Extensions/CollectionExtensions.cs index f2433be6f5..3c0813bfd3 100644 --- a/src/BizHawk.Common/Extensions/CollectionExtensions.cs +++ b/src/BizHawk.Common/Extensions/CollectionExtensions.cs @@ -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(this IEnumerable collection, int n) + => collection is ICollection countable + ? countable.Count >= n + : collection.Skip(n - 1).Any(); + + public static bool CountIsExactly(this IEnumerable collection, int n) + => collection is ICollection countable + ? countable.Count == n + : collection.Take(n + 1).Count() == n; + /// /// /// (This is an extension method which reimplements for other collections. diff --git a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/SoundProviderMixer.cs b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/SoundProviderMixer.cs index 347a72dced..263a2f5c32 100644 --- a/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/SoundProviderMixer.cs +++ b/src/BizHawk.Emulation.Cores/Computers/AmstradCPC/SoundProviderMixer.cs @@ -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(); } diff --git a/src/BizHawk.Emulation.Cores/Sound/SyncSoundMixer.cs b/src/BizHawk.Emulation.Cores/Sound/SyncSoundMixer.cs index 9ce1c1ae09..c721a72a71 100644 --- a/src/BizHawk.Emulation.Cores/Sound/SyncSoundMixer.cs +++ b/src/BizHawk.Emulation.Cores/Sound/SyncSoundMixer.cs @@ -109,20 +109,7 @@ namespace BizHawk.Emulation.Cores.Components /// 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(); }