Add official `BannedApiAnalyzers` and ban some string parsing methods

This commit is contained in:
YoshiRulz 2024-08-25 22:43:52 +10:00
parent 846253760e
commit c882fe4ea5
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
9 changed files with 35 additions and 19 deletions

View File

@ -376,6 +376,11 @@ dotnet_diagnostic.MEN015.severity = silent
# Use object-oriented methods instead of top-level statements
dotnet_diagnostic.MEN016.severity = silent
## Microsoft.CodeAnalysis.BannedApiAnalyzers rules
# Do not use banned APIs
dotnet_diagnostic.RS0030.severity = error
## StyleCop spacing rules
# Keywords should be spaced correctly

View File

@ -9,6 +9,7 @@
<PackageVersion Include="Menees.Analyzers" Version="3.2.2" />
<PackageVersion Include="Meziantou.Analyzer" Version="2.0.159" />
<PackageVersion Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.2" />

View File

@ -0,0 +1,11 @@
M:System.Convert.ToByte(System.String);use byte.{Try,}Parse
M:System.Convert.ToDecimal(System.String);use decimal.{Try,}Parse
M:System.Convert.ToDouble(System.String);use double.{Try,}Parse
M:System.Convert.ToInt16(System.String);use short.{Try,}Parse
M:System.Convert.ToInt32(System.String);use int.{Try,}Parse
M:System.Convert.ToInt64(System.String);use long.{Try,}Parse
M:System.Convert.ToSByte(System.String);use sbyte.{Try,}Parse
M:System.Convert.ToSingle(System.String);use float.{Try,}Parse
M:System.Convert.ToUInt16(System.String);use ushort.{Try,}Parse
M:System.Convert.ToUInt32(System.String);use uint.{Try,}Parse
M:System.Convert.ToUInt64(System.String);use ulong.{Try,}Parse

View File

@ -48,8 +48,8 @@ namespace BizHawk.Client.Common
if (Header.TryGetValue(HeaderKeys.CycleCount, out var numCyclesStr) && Header.TryGetValue(HeaderKeys.ClockRate, out var clockRateStr))
{
var numCycles = Convert.ToUInt64(numCyclesStr);
var clockRate = Convert.ToDouble(clockRateStr, CultureInfo.InvariantCulture);
var numCycles = ulong.Parse(numCyclesStr);
var clockRate = double.Parse(clockRateStr, CultureInfo.InvariantCulture);
numSeconds = numCycles / clockRate;
}
else
@ -68,8 +68,10 @@ namespace BizHawk.Client.Common
{
if (SystemID == VSystemID.Raw.Arcade && Header.TryGetValue(HeaderKeys.VsyncAttoseconds, out var vsyncAttoStr))
{
const decimal attosInSec = 1000000000000000000;
return (double)(attosInSec / Convert.ToUInt64(vsyncAttoStr));
const decimal attosInSec = 1_000_000_000_000_000_000.0M;
var m = attosInSec;
m /= ulong.Parse(vsyncAttoStr);
return checked((double) m);
}
else
{

View File

@ -422,7 +422,7 @@ namespace BizHawk.Client.EmuHawk
}
break;
case HeaderKeys.VsyncAttoseconds:
if (_emulator is MAME mame && mame.VsyncAttoseconds != Convert.ToInt64(v))
if (_emulator is MAME mame && mame.VsyncAttoseconds != long.Parse(v))
{
item.BackColor = Color.Pink;
item.ToolTipText = $"Expected: {v}\n Actual: {mame.VsyncAttoseconds}";

View File

@ -110,7 +110,7 @@ namespace BizHawk.Client.EmuHawk
return;
}
_values[PatternList.SelectedIndex] = ValueNum.Value.ToString(NumberFormatInfo.InvariantInfo);
_values[PatternList.SelectedIndex] = unchecked((int) ValueNum.Value).ToString(NumberFormatInfo.InvariantInfo);
UpdatePattern();
UpdateDisplay();
}
@ -217,7 +217,7 @@ namespace BizHawk.Client.EmuHawk
}
LagBox.Checked = _tastudio.AxisPatterns[index].SkipsLag;
ValueNum.Value = Convert.ToDecimal(_values[PatternList.SelectedIndex]);
ValueNum.Value = int.Parse(_values[PatternList.SelectedIndex]);
CountNum.Value = _counts[PatternList.SelectedIndex];
}
}
@ -267,7 +267,7 @@ namespace BizHawk.Client.EmuHawk
{
for (int c = 0; c < _counts[i]; c++)
{
p.Add((int) Convert.ToSingle(_values[i]));
p.Add(int.Parse(_values[i]));
}
}

View File

@ -1323,14 +1323,7 @@ namespace BizHawk.Client.EmuHawk
}
_axisTypedValue = _axisTypedValue.Substring(startIndex: 0, length: _axisTypedValue.Length - 1); // drop last char
if (_axisTypedValue == "" || _axisTypedValue == "-")
{
value = 0f;
}
else
{
value = Convert.ToSingle(_axisTypedValue);
}
if (!float.TryParse(_axisTypedValue, out value)) value = 0.0f;
}
else if (e.KeyCode == Keys.Enter)
{

View File

@ -241,6 +241,8 @@ namespace BizHawk.Emulation.Cores.Components.FairchildF8
}
}
private const string PFX_SCRATCHPAD_REG = "SPR";
public IDictionary<string, RegisterValue> GetCpuFlagsAndRegisters()
{
var res = new Dictionary<string, RegisterValue>
@ -266,7 +268,7 @@ namespace BizHawk.Emulation.Cores.Components.FairchildF8
for (int i = 0; i < 64; i++)
{
res.Add("SPR" + i, Regs[i]);
res.Add(PFX_SCRATCHPAD_REG + i, Regs[i]);
}
return res;
@ -274,9 +276,9 @@ namespace BizHawk.Emulation.Cores.Components.FairchildF8
public void SetCpuRegister(string register, int value)
{
if (register.StartsWithOrdinal("SPR"))
if (register.StartsWithOrdinal(PFX_SCRATCHPAD_REG))
{
var reg = Convert.ToInt32(register.Replace("SPR", ""));
var reg = int.Parse(register.Substring(startIndex: PFX_SCRATCHPAD_REG.Length));
if (reg > 63)
{

View File

@ -18,7 +18,9 @@
<RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" PrivateAssets="all" />
<Analyzer Include="$(MSBuildProjectDirectory)/../../References/BizHawk.SrcGen.ReflectionCache.dll" />
<AdditionalFiles Include="$(MSBuildThisFileDirectory)/BannedSymbols.BannedApiAnalyzers.txt" />
<Using Include="System" />
</ItemGroup>
<ItemGroup Condition=" '$(Configuration)' != 'Debug' ">